Tic tac toe confusion

Pages: 12345... 13
I see. This is indeed a novice mistake :)
If you hadn't modified my original snippet of code, you would have gotten correct results :]
@FBHSIE Exactly!

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.

Last edited on
@FBHSIE: Make sure to read my above post ^
Oh, I didn't even see there was a 3 page haha. Reading now. Give me a minute.
Alright! Progress is being made somewhat.

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.

If you hadn't modified my original snippet of code, you would have gotten correct results :]

I apologize.
1
2
3
4
5
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 
Last edited on
http://prntscr.com/bu8b56

Alright, I looked one up.

So, the first array gives the sub-array? Does that mean it gives the second array?

If so, why does is it the first dimension, and does that mean it forms the columns?

If the second dimension merely gives the element in the sub-array, why does it make a three by three square?

Again, I apologize for my silly questions. Thank you for sticking by and answering them.
@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?
Last edited on
@FBHISE

Did you notice the array subscripts start at 0 ? So the only valid subscripts for arr[3] are 0,1,2 or arr[0], arr[1], arr[2]

In a similar fashion for the 2d array, as Arslan7041 explained earlier. The last position in arr[3][3] is arr[2][2]

This has to do with how the array is stored in memory, and how the compiler calculates the position of items in the array.
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.

I'll be back!

Sure. Don't overwhelm yourself. Just let it all sink in and you'll understand everything soon enough.
Bonus : This thing may be very very useful =D

1.
std::string ticTac[3]; A line.

2.
std::string ticTac[3][3]; A perfect square.

3.
std::string ticTac[3][3][3]; A cube.
http://prntscr.com/buftnv

Good morning! I'm going to try to draw this step-by-step since it helps me. Is my first step correct? This is the first array of three?
> Is my first step correct?
Ok.
Be sure to read my bonus post above ^ :-)
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?
Also, if it helps, this is the part that's confusing me the most.

// 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

is confusing me the most. The rest makes sense.
Last edited on
Pages: 12345... 13