By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

Print a Matrix

P: 51
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
Share this Question
Share on Google+
2 Replies


P: 11
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

P: 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.