How can I print matrix with one loop . . .
ThanxCode:void printMat(const int mat[][N], int n) { int i, j; for(i = 0; i < n; i++) { for(j = 0; j < N; j++) printf("%3d ", mat[i][j]); puts(""); } }
How can I print matrix with one loop . . .
ThanxCode:void printMat(const int mat[][N], int n) { int i, j; for(i = 0; i < n; i++) { for(j = 0; j < N; j++) printf("%3d ", mat[i][j]); puts(""); } }
Why do you want to do that? It would obfuscate the clear logic, in my opinion.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Just use common sense:
In a 3 x 1 matrix, how many elements are there? OK, how about a 3 x 2? a 3 x 3?
Seeing the pattern yet?
instead of
Code:for(i = 0; i < rowNum; i++) for(j = 0; j < colNum; j++) printf("%3d", matrix[i][j]); //You would use: element = rowNum x colNum; for(i = 0; i < (well, you know the rest, ...
There you go.Code:for( x = 0; x < rows; x++ ) printrow( matrix[ x ] );
Quzah.
Hope is the first step on the road to disappointment.