Tic tac toe confusion

Pages: 1234... 13
Ah! I know why. The function prototype declares the existence of an upcoming function, and anything inside the parentheses are things--already existing--that are going to be passed. Therefore, if what's in the parentheses doesn't already exist, the program won't recognize it in the computer's memory.

Alright, moving on! Let's see what's next. Making the board!

Suggestions or examples on how to do this?
> Making the board!
You have already created and initialised the board.

> Making the board!
Can you elaborate more on this?
Oh, I mean displaying the contents of the board array.
> I mean displaying the contents of the board array.

1
2
3
4
5
6
for (int i = 0; i < ROWS; i++)
 {
     cout << '\t';
     for (int j = 0; j < COLS; j++)
        cout << ticTac[i][j]; cout << endl;
 }
This will look better :

1
2
3
4
5
6
7
for (int i = 0; i < ROWS; i++)
 {
     cout << '\t';
     for (int j = 0; j < COLS; j++)
        cout << ticTac[i][j] << ' '; 
     cout << endl;
 }
Could you explain what you're doing? : )
Could you explain what you meant by that? : )
Oh! Well, I don't really understand what you're doing with that small piece of code HAHA.

For future understanding and what so not, I want to make sure I can understand, so EVENTUALLY I won't have to use this forum as much after I get out of my college class.
Have you learned for-loop yet?
Is today the first time you have learned array?
> Oh! Well, I don't really understand what you're doing with that small piece of code HAHA.
You are not even trying. Yes, experiment. Just experiment.

Understanding this piece of code is your job. C++ programming is just all about practice you know :)
Yep! I've never used them in this sense before, though.

I wanted to see what it looked like though, and it gave me a weird output.

http://prntscr.com/bu7oz8
@Closed Account: He's a beginner. It doesn't help to just spoon feed the code to him. Either give him a pseudocde or explain the code you provided.

@FBHSIE: Do you know how to print out a normal (1 dimensional array)? If you do, understanding how to print a 2D array is not too far off.
Last edited on
Do i know how to print out one? Not yet. We've unfortunately had no programming exercises having to do with printing a 1D array.

Have I used one successfully in a program? Yes. This is a recent program I did without anyone's help. This is currently my comfortable range of using arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//Ashton Dreiling
//Number analysis program exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//function prototypes
void findHighestNumber (int numbers[], int SIZE, int &highest);
void findLowestNumber (int numbers[], int SIZE, int &lowest);
int getTotal(int numbers[], int SIZE);
void getAverage(int numbers[], int SIZE, double &average);

//global constants
const int SIZE=20;
const int ONE_FOR_CAL=1;
const int ZERO_FOR_CAL=0;

int main()
{
    //some variables
    int highest=ZERO_FOR_CAL;
    int lowest=ZERO_FOR_CAL;
    int sum=ZERO_FOR_CAL;
    double  average=ZERO_FOR_CAL;


    //array numbers
    int numbers[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

    //calling functions
    findHighestNumber(numbers, SIZE, highest);
    findLowestNumber(numbers, SIZE, lowest);
    sum=getTotal(numbers, SIZE);
    getAverage(numbers, SIZE, average);

    cout << "The highest number is " << highest << endl;
    cout << "The lowest number is " << lowest << endl;
    cout << "The sum is " << sum << endl;
    cout << "The average is " << average << endl;

    system("Pause");

    return 0;
}//end main


//beginning of functions for calculations
void findHighestNumber(int numbers[], int SIZE, int &highest)
{
    int index;

    highest=numbers[ZERO_FOR_CAL];

    for (index=1; index<=SIZE-ONE_FOR_CAL; ++index)
    {
        if (numbers[index]>highest)
        {
            highest=(numbers[index]);
        }// end if then statement for findHighestNumber

    }//end for loop for findHighestNumber
}//end findHighestNumber

void findLowestNumber(int numbers[], int SIZE, int &lowest)
{
    int index;
    lowest=numbers[ZERO_FOR_CAL];

    for (index=1; index<=SIZE-ONE_FOR_CAL; ++index)
    {
        if (numbers[index]<lowest)
        {
            lowest=(numbers[index]);
        }// end if then statement for findLowestNumber
    }//end for loop for findLowestNumber

}//end findLowestNumber

int getTotal(int numbers[], int SIZE)
{
    int index;

    int total=ZERO_FOR_CAL;

    for (index=0; index < SIZE; index ++)
    {
        total=(total+numbers[index]);
    }// end for loop for getTotal

    return total;
}//end getTotal

void getAverage(int numbers[], int SIZE, double &average)
{
    double total=ZERO_FOR_CAL;
    int index;

    for(index=0; index<=SIZE-ONE_FOR_CAL; index ++)
    {
        total=(total+numbers[index]);
    }// end for loop for getAverage

    average=(total/SIZE);

}// end getAverage
//end of functions for calculations 


I believe what closed account is doing is http://prntscr.com/bu7rf1
this was in my book.

However, I'm a tad bit confused about what's happening with nested for loops. They've always confused me, so I've avoided using them at all costs in previous programs since I've been trying to do more myself. Perhaps it will be inevitable for this exercise though, so if someone could attempt to explain, that might be helpful! Or, if we could continue this without using them, that'd be nice to.

Whatever help you can offer I appreciate and won't turn down.
Last edited on
> I wanted to see what it looked like though, and it gave me a weird output.
I think you at the very least know where to put this piece of code to.
Let us know your full code to better diagnose your problem.
I put it in the function as the original purpose of this function was to print the board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);


int main()
{
    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
            cout << endl;
        }
    }

}
Last edited on
I can you've looped through an array before. Printing out an array is just as simple. You simply loop through it, and cout each element. So take a look at this:

1
2
3
4
5
6
7
const int SIZE = 5;
int arr[SIZE] = {10, 20, 30, 40 , 50} // create an array and intitialize it with these numbers

for(int i = 0; i < SIZE; i++) // loop through the array
{
     cout << arr[i] << " " // print out the i-th element, followed by space
}


This will print:
10  20  30  40  50


Can you understand this snippet of code?
Last edited on
@FBHSIE: In your previous post, move line 33 to immediately after line 34.
There is one possibility. But I can't say it to you :)

Anyway, try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Model board : " << endl;
for (int i = 0; i < ROWS; i++)
 {
     cout << '\t';
     for (int j = 0; j < COLS; j++)
        cout << '*' << ' '; 
     cout << endl;
 }

cout << "My board : " << endl;
for (int i = 0; i < ROWS; i++)
 {
     cout << '\t';
     for (int j = 0; j < COLS; j++)
        cout << ticTac[i][j][0] << ' '; 
     cout << endl;
 }
I believe I do! Let me explain and tell me if I'm correct.

After the array is initialized, a for loop is formed.

i is our counter holder that will hold the number of times the for loop loops.

If it's less than 5, it'll loop, but if it's more than 5, it'll stop, putting it perfectly at 5 times exactly.

As it's looping, inside the code, for each loop, the array print[i] causing each of the five values to be printed.

By putting arr[i] you're saying for the array--considering this will loop five times--let i represent each subscript that'll print each element.

If it was meant to loop six times for example, I believe this would cause an error, as each subscript has to have an element for this to work in this loop.

@FBHSIE: In your previous post, move line 33 to immediately after line 34.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
            
            cout << endl;
        }
    }

}


Like this or am I misinterpreting?
Pages: 1234... 13