I am having a little problem with using multidimensional arrays. I am trying to create a matrix program here which does the following:
1) Get the number of rows and number of columns of the first matrix from the user 2) Ask the user to input the values in the matrix created 3) Get the number of rows and number of columns of the second matrix from the user 4) Ask the user to input the values in the matrix created 5) Add the matrices and give a result
#include <iostream.h>#include <stdlib.h>// The Matrix classclass Matrix
{
private:
int m[][];
int colSize;
int rowSize;
public:
void setRowsAndCols()
{
cout<<"Enter the number of rows: ";
cin>>rowSize;
cout<<"Enter the number of columns: ";
cin>>colSize;
}
void setVal(int row, int col, int val)
{
m[row][col] = val;
}
int getVal(int row, int col)
{
return m[row][col];
}
void setMatrix();
int getColSize()
{
return colSize;
}
int getRowSize()
{
return rowSize;
}
};
// Member function to set a matrixvoid Matrix::setMatrix()
{
if ((colSize != 0) && (rowSize != 0))
{
for (int x = 0; x <= rowSize-1; x++)
{
for (int y = 0; y <= colSize-1; y++)
{
int val;
cout<<"val"<<x<<y<<":";
cin>>val;
setVal(x,y,val);
}
}
}
else
{
cout<<"Set the matrix size first"<<endl;
setRowsAndCols();
}
}
// Independent function to add two matricesvoid addMatrices(Matrix *matrixPtr1, Matrix *matrixPtr2)
{
int m1RowSize, m1ColSize, m2RowSize, m2ColSize;
m1RowSize = matrixPtr1->getRowSize();
m2RowSize = matrixPtr2->getRowSize();
m1ColSize = matrixPtr1->getColSize();
m2ColSize = matrixPtr2->getColSize();
if ((m1RowSize != m2RowSize) || (m1ColSize != m2ColSize))
{
cout<<"ERROR: The matrices dimensions are not equal"<<endl;
}
else
{
int val1, val2, sum;
Matrix *resMatrixPtr = new Matrix;
for (int x = 0; x <= m1RowSize-1; x++)
{
for (int y = 0; y <= m1ColSize-1; y++)
{
val1 = matrixPtr1->getVal(x,y);
val2 = matrixPtr1->getVal(x,y);
cout<<"val1: "<<val1<<endl;
cout<<"val2: "<<val2<<endl;
sum = val1 + val2;
cout<<"the sum: "<<sum<<endl;
resMatrixPtr->setVal(x,y,sum);
}
}
cout<<"The resultant matrix value: ";
cout<<resMatrixPtr->getVal(1,2);
cout<<endl;
delete resMatrixPtr;
resMatrixPtr = NULL;
}
}
int main()
{
Matrix *ptr1 = new Matrix;
ptr1->setRowsAndCols();
ptr1->setMatrix();
Matrix *ptr2 = new Matrix;
ptr2->setRowsAndCols();
ptr2->setMatrix();
addMatrices(ptr1, ptr2);
delete ptr1;
ptr1 = NULL;
delete ptr2;
ptr2 = NULL;
system("PAUSE");
return 0;
}
Now the problem is that when the user starts entering the values in after setting the matrix sizes, the values being entered overwrite the matrix row and column sizes instead of populating the array. I tried to use the == operator instead of = in line 21, and that helped in setting the matrices properly but the sum wasn't correct.
It would be awfully nice of you if you could go through that and tell me where I am going wrong. All I know is that I might be making some mistake with the usage of the multidimensional arrays, but I don't know what it is.
You need to define the size of array at compile time. int m[50][50]; or define a size globally #deifine ARRAY_SIZE 50 then in the class int m [ARRAY_SIZE][ARRAY_SIZE] Also as said in the above post initialize m[][] to an initail value (0 0r -1).
ive made a program similar to yours but it was way simpler... The problem was also the incorrect sum that it prints but I have already corrected it. This is not a complex program but i hope you will see something that might help you:
Thanks for the suggestions. Setting the ARRAY_SIZE to initialize the matrix really helped, but I don't want to fix the array size in the beginning, that should be set when the user specifies the rowSize and colSize.
int main, I tried adding the constructor, but I am getting the following error for the m[rowSize][colSize] = {0}; line:
I just removed the curly brackets and it didn't give the error. But the program is behaving in the same way. I think the int m[][] is causing the problems. Do I have to fill something in that too?
Can this multi-dimensional array thingy be dealt with the help of pointers?