471,831 Members | 1,379 Online

# Print a Matrix

Hi!!!!!!!, well i want to know how to print in screen a matrix like this:

Matrix[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

And the output must be like this:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Sep 19 '06 #1
2 39679
Hi!!!!!!!, well i want to know how to print in screen a matrix like this:

Matrix[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

And the output must be like this:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

hai
u can print the matrix by using two for loops here

for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
here depends on how u declare the array
Sep 19 '06 #2
dush
27
Hi

Here is the more general solution:

Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. using namespace std;
3.
4. int main (){
5.
6.   int Matrix[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
7.
8.   for (int i=0; i<sizeof Matrix/sizeof Matrix[0]; ++i)
9.   {
10.     for (int j=0; j<sizeof Matrix[0] /sizeof Matrix[0][0]; ++j)
11.       printf("%03d ", Matrix[i][j]);
12.     cout << '\n';
13.   }
14. return 0;
15. }
16.
17.
You can change type of elements in array (char, int, short ...) and number of rows and colums in matrix without editing the 'for' loop.
Sep 19 '06 #3