Now lets take a look at the nested for-loop, shall we:
1 2 3 4 5 6 7 8 9 10
for (int i = 0; i < ROWS; i++)
{
cout << '\t';
for (int j = 0; j < COLS; j++)
{
cout << ticTac[i][j] << ' ';
}
cout << endl; // btw, this is what I meant when I said move this line down
// notice its OUTSIDE the inner loop, but in the outer loop
}
To understand this, you need to understand the following: When we declare an array like arr[3][3], we are actually make 3 arrays each of size 3. I've said this before, but you didn't seem to understand, but here's that picture again to clearly illustrate what I mean:
1 2 3 4 5 6 7 8 9
arr[3][3] =
{
{[0][0], [0][1], [0][2]}, // arr[0] i.e. the 0th array
{[1][0], [1][1], [1][2]}, // arr[1] i.e. the 1st array
{[2][0], [2][1], [2][2]} // arr[2] i.e. the 2nd array
// there is no 3rd array as that would be out of bounds
// notice that each of the 3 arrays have 3 elements in them, making is a 3 by 3
};
^ look at the above carefully, study it, and understand it. Once you have that down, the nested for loop will be easy to understand.
We can take advantage of this "array of arrays" idea to print out a multi-dimensional array. Here's the nested for loop again:
1 2 3 4 5 6 7 8 9 10 11 12 13
// this outer loop iterates through the 3 big sub- arrays
for (int i = 0; i < ROWS; i++)
{
cout << '\t'; //prints tab
// this inner loop iterates through each element in the i-th sub-array
for (int j = 0; j < COLS; j++)
{
cout << ticTac[i][j] << ' '; // goes to the i-th sub-array, then to the j-th
// element in that sub-array, and prints it out
}
cout << endl;
// moves to a new line after every row (i.e. sub-array) is printed out
}
Trace the above code with a pen and paper, and see if you understand it then.
When we declare an array like arr[3][3], we are actually make 3 arrays each of size 3.
Since the for-loop doesn't make sense to me yet, I'm going to focus on this piece of code for now.
1 2 3 4 5 6 7 8 9
arr[3][3] =
{
{[0][0], [0][1], [0][2]}, // arr[0] i.e. the 0th array
{[1][0], [1][1], [1][2]}, // arr[1] i.e. the 1st array
{[2][0], [2][1], [2][2]} // arr[2] i.e. the 2nd array
// there is no 3rd array as that would be out of bounds
// notice that each of the 3 arrays have 3 elements in them, making is a 3 by 3
};
I tend to have trouble with wording--even if something tends to be very obvious to the average person--is there another way you can say this sentence?
When we declare an array like arr[3][3], we are actually make 3 arrays each of size 3.
arr[3]; // creates an array of size 3
arr[3][3]; // creates 3 sub-arrays, each of which are of size 3 (draw a 3 by 3 square)
// first dimension gives the sub-array
// second dimension gives the element in that sub-array
// so cout << arr[1][2] will go to the 1st subarray, and print the 2nd element in it
@FBHISE: Brother, are you reading my posts carefully? Im not sure how much clearer I can be. Go ahead and re-read my posts, and look at the diagram I made as well which illustrates how a 2D array is structured.
Your teacher gave you an assignment involving 2D arrays without teaching you about 2D arrays first?
I'm trying! I am trying. Don't forget that please. I'm just not doing too well with my word comprehension tonight, but I'll always get there. If this is as clear as you can be, don't sweat it!
Let me study everything you've posted and said more, and I'm sure that along with TheIdeasMan's post will help make something click. If it's that obvious, perhaps some longer observation will help me.
Thanks for continuing to help me despite not being halfway there! I don't think I'd be able to actually understand if it wasn't for you guys. To confirm, what I just drew, that is the first dimension right? It's an array of size three?
Three square blocks or three subscripts of 0, 1, 2?