Read Matrix from file, give it to main - c++

I have a fairly simple program. It reads a matrix from a file that looks like this
4 4
a b c d
a b c d
a b c d
a b c d
and outputs it in the console. The program is working as intended:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int n, m;
ifstream myfile;
myfile.open ("matrix.txt");
myfile >> n >> m;
char mat[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
myfile >> mat[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << mat[i][j];
}
cout << endl;
}
return 0;
}
Now this program is going to grow a lot, so I want to start using headerfiles and declare the functions external so that my main looks more like
int main() {
readMat();
printMat();
}
My Problem is, I don't really know how to do this right. Where should I declare the matrix so that both functions can "see" it? I can't declare it globally as char mat[n][m] because I only know n and m inside readMat().
Or is my whole design flawed and there is much better way to do this?
I would be grateful for every little tip.
I have not worked with multiple files in c++ yet.

You cannot use an array like that, when not just multiple translation units, but multiple functions are involved.
Any function needs to know the exact data types it receives as parameters, and this must be declared at compile time.
Here, you don't know the type of the array until runtime. array[10] and array[15] are different types. Don't be fooled by the fact their name might be the same. It's not the name that matters, but the type, like int [10] or int [11].
You have two basic options:
Use templates.
Convert your array to a std::vector<std::vector<int>>. Then you can declare the array (now a vector) globally, and/or pass it as a parameter to your multiple functions.

Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
namespace mynamespace {
template <class T>
class Matrix {
private:
int m_rows;
int m_columns;
T** m_data;
public:
Matrix(); // Default Constructor - Null or Empty Matrix
Matrix( const int rows, const int columns ); // Empty Matrix With Defined Size
Matrix( const int rows, const int columns, const T** data ); // Defined Matrix
T** getData() const;
}; // Matrix
#include "Matrix.inl"
} // mynamespace
#endif // MATRIX_H
Matrix.inl
// Matrix Constructor and Function Implementations Here
Matrix.cpp
#include "Matrix.h"
Main.cpp
#include <string>
#include <iostream>
#include <fstream>
#include "Matrix.h"
using namespace mynamespace;
void readMatrix( std::fstream& fin, const std::string& strFilename, Matrix& matrixIn );
void printMatrix( const Matrix& matrix );
int main() {
std::fstream fileIn; // Prefer to not use "using namespace std;"
std::string strFilename( "Matrix.txt" );
Matrix<type> mat(); // Default Empty Matrix To Be Filled In
readMatrix( fileIn, strFilename, mat );
printMatrix( mat );
return 0;
} // main
void readMatrix( std::fstream& fin, const std::string& strFilename, Matrix& mat ) {
// Do Work Here To Populate Matrix Object
} // readMatrix
void printMatrix( Matrix& mat ) {
// Do Work Here To Print Matrix Being Passed In
} // printMatrix
With this pseudo code, the design here allows any type of matrix to be constructed. The matrix is an object of a template type. The implementation of generating a matrix by reading in data from a file, and the implementation of displaying or printing that matrix are independent of the class itself. This allows for the matrix class to be as generic as possible without having to rely on any other libraries. The functions that do the work are stand alone functions that accept a matrix object. A modification may be needed in your text or binary file to work with this construct.
matrix.txt
type
4 4
a b c d
a b c d
a b c d
a b c d
The only problem with this, is it should already be obvious: you need to define what type of data this matrix will hold when instantiating it. Yet you do not know the type until after you read the file in. So this code does have a bug in it that would need to be address. However, the overall design method used here is to only show how to keep similar code objects contained with in their own class object while separating independent code blocks that does work on them. Now other class objects may have a file reading and print methods, but normally Vector (math & not containers) and Matrix classes usually don't.

Related

Double declaration and initialization of vector in a class, C++?

Good night dear all
I have a question, look i working with classes and in many cases i am using vectors of vector (2D vectors), my code runs quite well. However, i am a bit confused, look in my header file i declared a vector of vectors in my protected variables, then in my cpp file in the constructors section i declared again the vector of vectors, but this time giving the needed sizes and having "0" in all the elements. However, when i tried to use this vector of vectors in my member function, it seems that not dimension was delcared and not "0" values, if i use .size() the output is "0" and i was expecting 3.
However, when i declare again the vectors of vectors in the member (see commented line in the cpp file) function the code gives the 3 and the full matrix which is a 3 X 3, composed by "0".
Why is that? using the constructor is to bassically give the values of the variables.
See the next code, the commented line on the cpp file is on which i have declare again the vector.
the header file is:
#pragma once
#include <iostream>
#include <vector>
class Matrix
{
private:
const int m_nRows;
const int m_nCols;
protected:
std::vector <std::vector <double>> MATRIX;
public:
Matrix(int rows, int cols);
~Matrix();
void getMatrix();
};
The cpp file is:
#include "Matrix.h"
Matrix::Matrix(int rows, int cols)
: m_nRows(rows),
m_nCols(cols)
{
std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
}
Matrix::~Matrix()
{
}
void Matrix::getMatrix()
{
//std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
std::cout << MATRIX.size() << std::endl;
for (auto& columns : MATRIX)
{
for (auto& element : columns)
{
std::cout << element << " ";
}
std::cout << "\n";
}
}
The main file is:
#include <iostream>
#include <vector>
#include "Matrix.h"
int main() {
int rows = 3;
int cols = 3;
Matrix SmallMatrix(rows, cols);
SmallMatrix.getMatrix();
system("pause>0");
}
In your constructor:
Matrix::Matrix(int rows, int cols)
: m_nRows(rows),
m_nCols(cols)
{
std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
}
you define a brand new variable with the name MATRIX, which is totally distinct from the member variable Matrix::MATRIX.
To initialize the Matrix::MATRIX member variable you should do it in the member initializer list, jut like for the m_nRows and m_nCols variables:
Matrix::Matrix(int rows, int cols)
: m_nRows(rows),
m_nCols(cols),
MATRIX(m_nRows, std::vector<double>(m_nCols, 0))
{
}
You are declaring another variable named MATRIX in the constructor. You have to instead use resize() on the MATRIX member declared in the class. The initialization of that MATRIX can be like this:
MATRIX.resize(m_nRows);
for (int i =0; i<m_nRows; i++){
MATRIX[i].resize(m_nCols, 0);
}

I am trying to create an array with an already specificed maximum size using a class but the array does not seem to be created

I am trying to create an array in my UnsortedList class. I specified to create an array in the header file, and I also specified the MAX_SIZE, which is equal to 10. However, whenever I create my object of the class, the default constructor does not create that array with the MAX_SIZE. I am unsure what I am doing wrong. I also get an error saying "stack around the variable 'myList' was corrupted". Also, just as a side note, can I initialize the array values when the default constructor is called, instead of creating a function to do it?
"UnsortedList.h" header file:
#pragma once
class UnsortedList {
public:
UnsortedList();
bool IsFull(); //Determines whether the list is full or not (returns T or F)
int GetLength(); //Gets the length of the list
void SetListValues();
private:
int length;
const int MAX_ITEMS = 10;
int numbers[];
};
"UnsortedList.cpp" file:
#pragma once
#include "UnsortedList.h"
#include <fstream>
#include <iostream>
using namespace std;
UnsortedList::UnsortedList() {
length = 0; //sets length to 0
numbers[MAX_ITEMS]; //sets array maximum size to MAX_ITEMS (10 as indicated in UnsortedList.h)
}
bool UnsortedList::IsFull() {
return (length == MAX_ITEMS);
}
int UnsortedList::GetLength() {
return length;
}
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
while (!inFile.eof()) {
inFile >> numbers[x];
x++;
}
}
"main.cpp" file:
#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;
int main() {
UnsortedList myList;
myList.SetListValues();
return 0;
}
I recommend you use std::array or std::vector, but if you must use C arrays, then your definition in the header needs correcting:
class UnsortedList {
// ...
const static int MAX_ITEMS = 10;
int numbers[MAX_ITEMS];
};
You can remove the corresponding line in the constructor. The file reading method also needs correcting:
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
int read_value;
// x < MAX_ITEMS to avoid out of bounds access
while (x != MAX_ITEMS && inFile >> read_value)
{
numbers[x++] = read_value;
length++; // I assume you also want to increment the length at this point?
}
}
Edit: As noted by #πάνταῥεῖ, there is no good reason to use C style arrays when the standard provides std::array. Not much changes, it is declared as:
std::array<int, MAX_ITEMS> numbers;
You can use operator[] as with the C array. This is preferable as it provides a richer API and can be used like other C++ containers, i.e. with STL algorithms.

using an abstract class from another header file in c++

I have 3 files
dimentions.h:
namespace mtm {
class Dimensions {
int row, col;
//some code
};
}
IntMatrix.h:
#include "dimentions.h"
namespace mtm
{
class intMatrix
{
private:
int** data;
int col;
int row;
public:
intMatrix(Dimensions dims, int num=0);
//some code
};
//the line bellow is where I get the error
intMatrix(Dimensions dims, int num=0): col(dims.getCol()),row(dims.getRow()) ,data(new int*[dims.getRow()])
{
for(int i=0;i<dims.getCol())
{
data[i](new int[dims.getCol]);
}
for(int i=0;i<dims.getRow();i++)
{
for(int j=0;j<dims.getCol();j++)
{
data[i][j]=num;
}
}
}
}
the compiler says: expected ‘)’ before ‘dims’
and when I put the mouse at dims, vs code says: " error-type mtm::dims"
but dims is not a type it is a vriable.
IntMatrix.cpp:
#include "IntMatrix.h"
using namespace mtm;
//some code
in IntMatrix.cpp the problem is that it doesn't recognize what Dimentions is , however it does recognize what intMatrix is.
Welcome to StackOverflow!
Compiler messages are sometimes misleading, and in your case you have an error because you are repeating the default value in your implementation. This is probably the error the compiler is complaining about.
EDIT
As pointer out by drescherjm, you also forgot the add the class name to the constructor
The correct definition should be :
intMatrix::intMatrix(Dimensions dims, int num): ...
Please let me know if you still have the error after that.
In your C++ source code the constructor must be defined as
intMatrix::intMatrix(Dimensions dims, int num) ....
So you have two mistakes: you have to add intMatrix and delete =0
But do not ever write a matrix class like this. This is terrible C++ code. You should never need to call new directly. And your data, in this case, is laid down very cache unfriendly

How to declare a global 2d 3d 4d ... array (heap version) variable that could be used in the entire program?

class1.cpp
int a=10; int b=5; int c=2;
//for this array[a][b][c]
int*** array=new int**[a];
for(int i =0; i<a; i++)
{
array[i] = new int*[b];
for(int k =0; k<b; k++)
{
array[i][k] = new int[c];
}
}
how can i use this array in other .cpp files ?
Instead of manually allocating an array you should at least use a std::vector. What you would do is have a header file that contains
extern std::vector<std::vector<std::vector<int>>> data;
that you will include in all the cpp files you wish to share the vector with and then in a single cpp file have
std::vector<std::vector<std::vector<int>>> data = std::vector<std::vector<std::vector<int>(a, std::vector<std::vector<int>>(b, std::vector<int>(c)));
and now you will have a global object that is shared and it has a managed lifetime.
You shouldn't really use a nested vector though. It can scatter the data in memory so it isn't very cache friendly. You should use a class with a single dimension vector and pretend that it has multiple dimensions using math. A very basic example of that would look like
class matrix
{
std::vector<int> data;
int row; // this really isn't needed as data.size() will give you rows
int col;
int depth;
public:
matrix(int x, int y, int z) : data(x * y * z), row(x), col(y), depth(z) {}
int& operator()(int x, int y, int z) { return data[x + (y * col) + (z * col * depth)]; }
};
and then the header file would be
extern matrix data;
and a single cpp file would contain
matrix data(a, b, c);
Prefer std::array or std::vector to raw arrays. You had constant dimensions so use std::array.
Declare it in header file:
// header.h
#pragma once // or use multiple inclusion guards with preprocessor
#include <array>
const int a = 10;
const int b = 5;
const int c = 2;
using Array3D = std::array<std::array<std::array<int,c>,b>,a>;
extern Array3D array3d; // extern indicates it is global
Define it in cpp file:
// class1.cpp
#include "header.h"
Array3D array3d;
Then include the header wherever you want to use it.
// main.cpp
#include "header.h"
int main()
{
array3d[3][2][1] = 42;
}
I am not sure I have understood what exactly you mean but simply :
class1 obj1;
obj1.array[i][j][k] // assuming you make the array public and already initialized in the constructor(and dont forget to delete it in the destructor)

Making an array class so they act like vectors

I have to make a class that will make arrays act like vectors. When I try and pass the class into the method into my main I get an error telling me that "[" and "]" are incorrect operators. I was wondering if I'm just completely doing this wrong or if it's just a simple mistake. Help is greatly appreciated. Here is my header file:
#ifndef PROGRAM5HEADER_H
#ifndef PROGRAM5HEADER_H
#define PROGRAM5HEADER_H
#include <string>
using namespace std;
class FloatArray
{
int *rep;
int _size;
public:
FloatArray(int sz=100):_size(sz)
{
rep=new int[sz];
}
~FloatArray()
{
delete [] rep;
}
int size() const
{
return _size;
}
FloatArray(const FloatArray& x)
{
copy(x);
}
void copy(const FloatArray& x)
{
_size == x.size();
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
};
#endif
and here is my main program
#include <iostream>
#include <string>
#include <cstdlib>
#include "program5header.h"
#include <cmath>
using namespace std;
int meanstd(FloatArray x, int& std)
{
int sx=0,sx2=0,mean;
for(int i=0;i<x.size();i++)
{
sx+=x[i];
sx2+=x[i]*x[i];
}
mean=sx/x.size();
std=sqrt(sx2/x.size()-mean*mean);
return mean;
}
int main()
{ int f;
cout<<"How big of an array would you like: "<<endl;
cin>>f;
FloatArray x(f);
}
There are a lot of issues with a lot of your implementation, I'd suggest doing some research on the subject. I'll touch on a few.
Firstly, you should make your FloatArray a templated class and allow for different types other than just int.
When you initialize a FloatArray x and then try to access it's underlying array through "[]" you are actually invoking the following:
x.operator[](...)
You haven't defined the '[]' operator on your FloatArray class so you are getting an error.
You need something similar to this:
int FloatArray.operator[](int index) {
assert(index < _size);
return _rep[index]
}
Your copy isn't doing what you want, it's not copying the size over to "this". It should look something similar to this:
void copy(const FloatArray& x)
{
_size = x._size;
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
However I would suggest not having a copy method and instead implement everything in your copy constructor.