468,491 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

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

Thaks for your help
Sep 19 '06 #1
2 39470
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

Thaks for your help
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Ronen Kfir | last post: by
12 posts views Thread by Balaji.V | last post: by
2 posts views Thread by DarrenWeber | last post: by
reply views Thread by DarrenWeber | last post: by
18 posts views Thread by Hypnotik | last post: by
Thekid
1 post views Thread by Thekid | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.