2D Basic Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio>

int main()
{
	int i, j;
   int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

   for(i=0; i<=2; i++)
   {
   	for(j=0; j<=3; j++)
      	cout<<"a["<<i<<"]["<<j<<"] = "<<a[i][j]<<"\t";
         cout<<"\n";
   }
   getch();
   return 0;
}


Hi, can I know how to read code in line 7? I mean "1" is at a[0][0]? "2" is at a[0][1]? why can't "2" at a[1][0]? How is the row and column define in this c++ actually? Thanks...
How about you try this instead?
int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
This is how you would normally do it.
Thanks...So is that 1,2,3,4 is 1st row and 5,6,7,8 si 2nd row? Am I right?
and also a[row][column]? Is this correct? TQ
It's technically just an array of arrays. If you wan to call it row and column, that is up to you. It just depends on how you plan to write and read to it.

In your implementation: yes, i represents your rows and j represents your columns. You could just as easily transpose this to make it opposite by doing this:

1
2
3
4
5
6
   for(j=0; j<=3; j++)
   {
   	for(i=0; i<=2; i++)
      	cout<<"a["<<i<<"]["<<j<<"] = "<<a[i][j]<<"\t";
         cout<<"\n";
   }
Thanks...is that mean, int a[3][4] in line 7 control the for loop? I mean if I use my own code above and how to change int a[3][4] in line 7 to make the output like the one u give(Stewbond)? Can I change to something like int a[4][3]?

And is there any default from C++, I mean left is row and right is column? TQ...
Topic archived. No new replies allowed.