Printing the elements of a matrix

closed account (1TpXSL3A)
hi guys im trying to write a code that reads from a text file the values Aij i,j= 1,2,3 for a matrix (3x3)
I'm trying to write a code where the elements are printed as
A[1][1]:1
all the way to
A[3][3] :9


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>
using namespace std;
 
int main ()
{
   int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9}};
                       
   for ( int i = 1; i < 4; i++ )
      for ( int j = 1; j < 4; j++ )
      {
         cout << "a[" << i << "][" << j << "]: ";
         cout << a[i][j]<< endl;
      }
	  
	  getch(); 
   return 0;
}


this isn't giving me what i want when i try to run it - please help!

thanks
Array indices start at 0 and go to size - 1.
So your for loops should be
9
10
11
12
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    {
        // ... 
closed account (1TpXSL3A)
is there any way i can make it print out as
[1][1] :1
and not [0][1] :1 though?

or is this something that i can't avoid
Simply add 1 at the time you print out the indexes.
 
cout << "a[" << i+1 << "][" << j+1 << "]: ";
Topic archived. No new replies allowed.