Problem with Multi-Dimensional Arrays

Hi Guys,

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

Here's the code for the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream.h>
#include <stdlib.h>

// The Matrix class
class 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 matrix
void 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 matrices
void 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.

Thanks,

Farhan
Hello,

the way I see it, the line

 
Matrix *ptr1


creates an object of the class Matrix. But since you do not have a custom-made constuctor, the array m[] [] can not be initialized correctly.

What you should do is the following:

Change
 
 void setRowsAndCols...


into
 
Matrix ();

This will introduce the constructor

and then change
1
2
3
4
5
6
7
 void setRowsAndCols()
   {
      cout<<"Enter the number of rows: ";
      cin>>rowSize;
      cout<<"Enter the number of columns: ";
      cin>>colSize;
   }


into the following outside the class-defniniton
1
2
3
4
5
6
7
8
 Matrix::Matrix
   {
      cout<<"Enter the number of rows: ";
      cin>>rowSize;
      cout<<"Enter the number of columns: ";
      cin>>colSize;
      m[rowSize][colSize] = {0};
   };


int main
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int main()
{
clrscr();

const int week = 2;
const int working_days = 4;
double salary[week] [working_days];
double sum = 0;
int w, wd;

cout<<"Computing the salary of a Worker for 2 Weeks"<<endl;
cout<<endl;

for (w=1; w<=week; w++)
{
	cout<<"Week "<<" "<<(w)<<": "<<endl;
	for (wd=1; wd<=working_days; wd++)
	{
	cout<<"Day "<<" "<<(wd)<<"= "<<" ";
	cin>>salary[week] [working_days];
	sum = sum + salary[week] [working_days];
	}
	cout<<endl;
}
{
cout<<"The Salary of a Worker for 2 Weeks is: "<<sum<<endl;
}
getch();
}
Last edited on
Hey Guys,

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:

parse error before '{'

Am I missing something here?

Thanks
Last edited on
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?

Thanks
Really int m[][] is a problem.Only one dimension can be without size.My solution with pointers is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Dim2Array 
{
private:
    int* m_pnCells;
    const int  m_nRows;
    const int  m_nCols;

public:
    Dim2Array( int nRows, int nCols ) : m_nRows( nRows ), m_nCols(nCols)
    {
        int nLen = (m_nRows * m_nCols);
        m_pnCells = new int[ nLen ];
    
        //zero them 
        for( int i = 0; i < nLen; ++i ) 
        {
            m_pnCells[i] = 0;
        }
    }

    ~Dim2Array() 
    {
        delete[] m_pnCells;
        m_pnCells = NULL;
    }

public:
    int* operator[]( int i )
    {
        return m_pnCells + ( m_nCols * i );
    }


};



And now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class matrix
{ 
private :
   Dim2Array a;

public:  
    matrix::matrix( int n=1,int m=1 ) : a(n,m)
    {
    }
     
    matrix::~matrix() 
    {
    }
    
    // Some functions ........

}


Best Regards
int m[][] is causing the problems


yeah, it is really a problem... if you still want to use that one, better put
something inside the brackets,... use an identifier and initialize it..
Topic archived. No new replies allowed.