# Print matrice with one loop?

This is a discussion on Print matrice with one loop? within the C Programming forums, part of the General Programming Boards category; How can I print matrix with one loop . . . Code: void printMat(const int mat[][N], int n) { int ...

1. ## Print matrice with one loop?

How can I print matrix with one loop . . .

Code:
```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("");
}
}```
Thanx

2. Why do you want to do that? It would obfuscate the clear logic, in my opinion.

3. 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?

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, ...```

4. Code:
```for( x = 0; x < rows; x++ )
printrow( matrix[ x ] );```
There you go.

Quzah.