I wrote this function for displaying a matrix, but it won't compile..
What am I doing wrong?
Code:void display(int A[] [], int m, int n) { for (i =0; i< m; i++) { for (j=0; j < n; j++) cout << " " << A[i][j]; cout << "\n";} }
I wrote this function for displaying a matrix, but it won't compile..
What am I doing wrong?
Code:void display(int A[] [], int m, int n) { for (i =0; i< m; i++) { for (j=0; j < n; j++) cout << " " << A[i][j]; cout << "\n";} }
You are not reading your error messages, which tell you that i and j are undeclared.
There's absolutely no good reason to use global variables i and j for that piece of code. You may have made other things in that code global too, which makes it impossible to diagnose what's wrong just from that snippet. What are the compiler's error messages?
Edit: From gcc:
error: declaration of ‘A’ as multidimensional array must have bounds for all dimensions except the first
I didn't notice this as I normally use pointer syntax instead of array syntax for function arguments. Is this basically your error message?
Last edited by robatino; 07-07-2007 at 10:00 AM.
Story time:
Once there was a young coder. He was writing some code to display a cute little matrix. But he had a problem. He forgot to put this in his code:
Oh Dear!!! The compiler got very angry with the young coder. It spit out error messages like crazy but the young coder didn't see them... :-( So he went to a forum where a wise old man told him his problem. Still the young coder didn't listen. He stared at his code for several more minutes... Everyone begged him to look at the error messages and tell him what they were because they couldn't believe that even a young coder would use global variables for i and j.Code:int i, j;
To be continued...
Don't quote me on that... ...seriously
> int A[] []
All the minor dimensions of a multi-dimensional array need a size.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Story Time Continued:
The young coder thought all hope was lost. But then, Salem, the wisest of them all, reminded the young coder that when passing multi-dimensional arrays to functions, all minor dimensions need a size. The young coder had a new problem. He told, "I don't know the dimensions!". How can I pass the array to my function without knowing the dimensions?
To be continued...
Don't quote me on that... ...seriously
You could "flatten" it to a 1D thing -
And you're laughing.Code:void print ( int *pnum, int x, int y ) { for ( int j=0; j<y; j++, printf("\n") ) { for ( int i=0; i<x; i++ ) { printf( "%d ", *pnum ); *pnum++; } } } int main( void ) { int test22[2][2] = {{9, 8}, {7, 6}}; int test23[2][3] = {{9, 8, 7}, {6, 5, 4}}; int test33[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; int test24[2][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}}; printf( "Printing 2x2 matrix\n" ); print( &test22[0][0], 2, 2 ); printf( "\nPrinting 2x3 matrix\n" ); print( &test23[0][0], 3, 2 ); printf( "\nPrinting 3x3 matrix\n" ); print( &test33[0][0], 3, 3 ); printf( "\nPrinting 2x4 matrix\n" ); print( &test24[0][0], 4, 2 ); return 0; }