-
show matrix
Code:
#include <stdio.h>
#include <stdlib.h>
int show_matrix(int column, int row, int matrix[column][row]);
int main(void)
{
int m = 2;
int matrix[m][m] = { {1,2}, {1,2} };
show_matrix(m,m,matrix);
return 0;
}
int show_matrix(int column,int row,int matrix[column][row])
{
int m,n;
for( m = 0; m < column; m++ )
{
printf("\n");
for(n = 0; n < row; n++)
{
printf("%3d",matrix[m][n]);
}
}
when i compile this,it can run,but it gives me a lot of warning,can someone give me any solution?
-
How about fixing your warnings? Oh, and unless you're compiling this as C99, you can't have your array done that way, which is probably what it's complaining about.
Quzah.
-
so,without c99,is it possible to use 2d variable array?
-
No. Simulate it with dynamic memory allocation. A quick forum search on the topic should yield lots of code for you to play with.
Quzah.
-
thanks quzah,i really appreciate ur help...:)
-
I hav made some modifications:
Code:
#include <stdio.h>
#include <stdlib.h>
#define m 2
int show_matrix(int,int,int matrix[m][m]);
int main(void)
{
int matrix[m][m] = { {1,2}, {1,2} };
show_matrix(m,m,matrix);
return 0;
}
int show_matrix(int row,int coloum,int matrix[m][m])
{
int c,r;
for(r=0;r<row;r++)
{
printf("\n");
for(c = 0; c < coloum; c++)
{
printf("%3d",matrix[r][c]);
}
}
return 0;
}
In the function declaration u only need to specify the data type except for a matrix.
It is a good practice to declare m as a constant using #define.
and ur row-coloum convension is wrong. The outer loop should vary row and inner loop must vary coloum.
Finally try to code it more simple.