@FBHSIE: The picture you drew is not the 0th sub-array. The 0th sub-array would be 3 squares lined up HORIZONTALLY, not vertically.
Then the 1st sub-array is another horizontal line of 3 blocks. And finally the 2nd sub-array is another horizontal line of 3 blocks. Remember that there is NO 3rd sub-array, as array subscripts start at 0.
arr[1][2] would mean go to the 1st sub-array. Now that you are in the 1st sub-array, go to the 2nd subscript in that sub-array. So arr[1][2] is the bolded one below:
Basically, in arr[1][2], the first subscript gives you the row #, and second subscript gives you the column #. So treat it like a normal table, except both rows and columns start at 0 and NOT 1.
So one more time:
arr[1][2] = go to row 1 and column 2.
cout << "\t" simply prints a tab i.e. it prints a few spaces. Like how you would press the tab key on your keyboard. He has it in is code to shift the square over by a few spaces.
The nested for-loop goes through the entire 2D array, from top to bottom and left to right. The outer loop goes through the sub-arrays and the inner loop goes through each element in the sub-array.
Make a [3][3] 2D array filled with random numbers (not on code, just on a paper), and then trace the nested for-loop on a piece of paper and you'll see what I mean. By tracing I mean simulate the code. See how i and j change and how the array is printed.
Two questions, why did he initialize row and column to 0 and not 1?
Wouldn't there be three sub-arrays if he initialized rows to 0? Explain to me why that didn't happen.
Also, I'm confused about what bertha[row][column] exactly did, but I understood why he had to do the rest! Including the " " part.
Can you explain how bertha[row][column] was responsible for printing the numbers (at least I'm pretty sure it was as he implied it thus why he needed " " to separate the numbers).
row and column need to be initialized to 0 for the same reason i and j were initialized to 0: because array subscripts start at 0. This is something that has been repeated many times on this thread.
'row' and 'column' are the index counters, they are used in the loop to go through the array.
Remember that row and column are just numbers/coordinates used to access some element in the array. So when he did cout << bertha[row][column] he is printing out the number at that row and that column. 'row' and 'column' will be continuously changing as they are incremented by the for-loop, thus every element in the array will be printed out.
Now do this:
Make a [3][3] 2D array filled with random numbers (not on code, just on a paper), and then trace the nested for-loop below on a piece of paper and you'll see what I mean. By tracing I mean simulate the code. See how i and j change and how the array is printed.
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
}
It makes sense now finally! Alright, I understand this piece of code. Does this mean we finally got the board printed?
I made a code with just numbers with and without the cout<< endl; statement and examined how the things like \t had an effect and how ticTac[][] was affecting the placement of the numbers.
Now I understand why you said the first dimension makes the sub-arrays while the other one names the subscript.
123 456 789
123 456 789
All this is [2][3]
Because there are two sub arrays and three subscripts for each row.
the cout << endl; is needed to make sure each set of rows and columns is grouped on the next line when looped again.
If you understand it now, we can move on. Just make a function called printBoard() which takes the array as a parameter and put that piece of code in the function.
If you call the function, it should simply print out 9 asterisks (*) in the form of a square.
Don't use cin to make changes to an element in the board directly.
So : cin >> ticTac[][COLS];
You need : int position;
You input the variable.
When you get the position info, you place a 'X' (if the player is X), or an 'O' (if the player is O)
Hint : A 3x3 array has 9 elements, meaning there are 9 positions.
Sample output :
* * *
* * *
* * *
Player X : Where do you want to place a 'X' to? : 4
* * *
X * *
* * *
Player O : Where do you want to place an 'O' to? : 2
* O *
X * *
* * *
etc
void userInput(string ticTac[][COLS])
{
int positionOne;
int positionTwo;
cout << "Where would you like to go player X?" << endl;
cin >> positionOne;
cout << "Where would you like to go player X?" << endl;
cin >> positionTwo;
}
This is not the correct way to enter the position. According to the OP's teachers instructions, the user should be entering a row and column #. The 'X' or 'O' should be put at that position. In other words, we need two inputs from the user to get the position.