n-dimensional arrays

@irritated
why this compile error:

void build_matrx ( double a [][MAX] ){

int sizeRow,sizeCol;
double element;

cout << "Enter the number of rows you want in the first matrix." << endl;
cin >> sizeRow;
cout << "Enter the number of columns in the first matrix." << endl;
cin >> sizeCol;

for ( int i = 0; i < sizeRow; i++ )
{

for ( int j = 0; j < sizeCol; j++ )
{
cout << "Enter element " << i << "," << j << endl;
cin >> element;
a [i][j] = element;
}
}
}
//*******************MAIN*****************************************************
int main ()
{
double matrx1 [MAX][MAX] = {0};
double matrx2 [MAX][MAX] = {0};

on this line --> build_matrx ( matrx1 [][MAX] );

receiving this error: expected primary-expression before ']' token
You don't need to supply the [][MAX] when calling the function. The function is expecting a variable of type double[][MAX]. matrix1 is exactly that. Just call "build_matrx(matrx1);".

Also, for the love of Stroustrup, do the extra effort and type the 'i' in matrix! ò_ó
Thanks Gaminic. I'm aware that this is not the most efficient way to implement a matrix object. I'm workin on it though.

Thx again
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
 void build_Matrx ( double [][MAX], int &sizeRow, int &sizeCol ){

     //int sizeRow,sizeCol;
     double element;
     double matrx1 [MAX][MAX];

     cout << "Enter the number of rows you want in the first matrix." << endl;
     cin >> sizeRow;
     cout << "Enter the number of columns in the first matrix." << endl;
     cin >> sizeCol;

     for ( int i = 0; i < sizeRow; i++ )
         {

          for ( int j = 0; j < sizeCol; j++ )
               {
                  cout << "Enter element " << i << "," << j << endl;
                  cin >> element;
                  matrx1 [i][j] = element;
               }
          }
}
void printMatrx ( double a [][MAX], int &b, int &c ){

    for( int n = 0; n < b; n++)
		{
	   for(int y = 0; y < c; y++)
			{
				cout <<  setw(6) << a [n][y];
				if (y == ( b - 1 ))
	   					cout << endl << endl;
			}
		}

}
//*******************MAIN*****************************************************
int main ()
{
     double matrx1 [][MAX] = {0};
     //double matrx2 [][MAX] = {0};
     int sizeRow, sizeCol;


          build_Matrx ( matrx1, sizeRow, sizeCol );
          printMatrx( matrx1, sizeRow, sizeCol );


The output is:

Enter the number of rows you want in the first matrix.
2
Enter the number of columns in the first matrix.
2
Enter element 0,0
1
Enter element 0,1
2
Enter element 1,0
3
Enter element 1,1
4
0 0

4.24399e-3144.13173e-306

Not quite sure what I'm doing wrong.

Last edited on
[edit] Ignore this post; missed something big. Read vlad's post below!
Last edited on
The local array matrix1 in your function build_Matrix

void build_Matrx ( double [][MAX], int &sizeRow, int &sizeCol ){

//int sizeRow,sizeCol;
double element;
double matrx1 [MAX][MAX];

has nothing common with the array matrix1 defined in the main.
You even do not use the first parameter of the function. I think that your task is to use the aaray defined in main, pass it to build_Matrix and fiil it there.
Topic archived. No new replies allowed.