Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Just wondering about an array
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Sun Mar 24, 2013 5:46 pm   Post subject: Just wondering about an array

Okay so... I was just looking at a tic tac toe game online at cppforschool.com, the array at the start that's like this:
c++:

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

I'm just wondering. Why is there another 'o' there? I tried compiling the code without it actually, and it was messed up. Why is it like that though?
The full code is like this:
c++:

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()
{
        int player = 1,i,choice;

        char mark;
        do
        {
                board();
                player=(player%2)?1:2;

                cout << "Player " << player << ", enter a number:  ";
                cin >> choice;

                mark=(player == 1) ? 'X' : 'O';

                if (choice == 1 && square[1] == '1')

                        square[1] = mark;
                else if (choice == 2 && square[2] == '2')

                        square[2] = mark;
                else if (choice == 3 && square[3] == '3')

                        square[3] = mark;
                else if (choice == 4 && square[4] == '4')

                        square[4] = mark;
                else if (choice == 5 && square[5] == '5')

                        square[5] = mark;
                else if (choice == 6 && square[6] == '6')

                        square[6] = mark;
                else if (choice == 7 && square[7] == '7')

                        square[7] = mark;
                else if (choice == 8 && square[8] == '8')

                        square[8] = mark;
                else if (choice == 9 && square[9] == '9')

                        square[9] = mark;
                else
                {
                        cout<<"Invalid move ";

                        player--;
                        cin.ignore();
                        cin.get();
                }
                i=checkwin();

                player++;
        }while(i==-1);
        board();
        if(i==1)

                cout<<"==>\aPlayer "<<--player<<" win ";
        else
                cout<<"==>\aGame draw";

        cin.ignore();
        cin.get();
        return 0;
}

/*********************************************

        FUNCTION TO RETURN GAME STATUS
        1 FOR GAME IS OVER WITH RESULT
        -1 FOR GAME IS IN PROGRESS
        O GAME IS OVER AND NO RESULT
**********************************************/


int checkwin()
{
        if (square[1] == square[2] && square[2] == square[3])

                return 1;
        else if (square[4] == square[5] && square[5] == square[6])

                return 1;
        else if (square[7] == square[8] && square[8] == square[9])

                return 1;
        else if (square[1] == square[4] && square[4] == square[7])

                return 1;
        else if (square[2] == square[5] && square[5] == square[8])

                return 1;
        else if (square[3] == square[6] && square[6] == square[9])

                return 1;
        else if (square[1] == square[5] && square[5] == square[9])

                return 1;
        else if (square[3] == square[5] && square[5] == square[7])

                return 1;
        else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
     && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')

                return 0;
        else
                return -1;
}


/*******************************************************************
     FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/



void board()
{
        system("cls");
        cout << "\n\n\tTic Tac Toe\n\n";

        cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
        cout << endl;

        cout << "     |     |     " << endl;
        cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

        cout << "     |     |     " << endl << endl;
}

/*******************************************************************
                                END OF PROJECT
********************************************************************/


That graph actually looks pretty good, I might use it for mine too Razz
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sun Mar 24, 2013 5:54 pm   Post subject: RE:Just wondering about an array

I'm assuming it's because he'd rather work with array values 1 to 9 rather than 0 to 8
Zren




PostPosted: Sun Mar 24, 2013 6:09 pm   Post subject: RE:Just wondering about an array

Looks like it's just used for padding the array to start from an index of 1 so they can easily get user input in a range more comfortable for a user.

Which is a horrible reason for this practice as you can just do cin >> input; choice = input - 1; and shift all the indexes down by one.

Depending on this hack will also prevent you from moving to multi-dimensional arrays.

Quote:

I tried compiling the code without it actually, and it was messed up.


What was the error?

Edit: Damn it I'm slow.
Cancer Sol




PostPosted: Sun Mar 24, 2013 7:42 pm   Post subject: Re: Just wondering about an array

I thought that's only if the arrays were integers, but I guess I just learnt something new xD
Thanks guys.
@Zren It wasn't an error, it was just that the board's numbers were all over the place Razz

Quote:

Depending on this hack will also prevent you from moving to multi-dimensional arrays.

What do you mean by that? Doing what the sample tic tac toe code showed won't let me use multi-dimensional arrays properly?

Edit: Wouldn't that just add an extra variable that's not needed? Or is it supposed to do something like make the code easier to read?
Insectoid




PostPosted: Sun Mar 24, 2013 7:53 pm   Post subject: RE:Just wondering about an array

It would add an entire array that isn't needed. Not really a world-ending issue but it's really inefficient.
Zren




PostPosted: Sun Mar 24, 2013 7:57 pm   Post subject: Re: Just wondering about an array

Cancer Sol @ Sun Mar 24, 2013 7:42 pm wrote:
I thought that's only if the arrays were integers, but I guess I just learnt something new xD
Quote:

Depending on this hack will also prevent you from moving to multi-dimensional arrays.

What do you mean by that? Doing what the sample tic tac toe code showed won't let me use multi-dimensional arrays properly?


Erm. Now that I think about it, you can still use it. The padding would just use up a lot more memory (than just one extra unit).

In a single-dimensional array. You waste just the first index.
In a two dimensional (2d) array, you waste the entire first row and column.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

In the above example 10x10 array, you're wasting the 19 (x 4 Bytes if using a typical integer data type) units of memory at both the column at index 0 and the row at index 0. That's almost a 1/5th of extra memory usage for this hack.

Edit, not again. ;_;
Raknarg




PostPosted: Sun Mar 24, 2013 8:02 pm   Post subject: RE:Just wondering about an array

I'm guessing it only makes a difference if you use a lot of arrays then
DemonWasp




PostPosted: Mon Mar 25, 2013 1:20 am   Post subject: RE:Just wondering about an array

Given as the language in question is C++, you can also abuse pointers:

code:

char square_setup[9] = {'1','2','3','4','5','6','7','8','9'};
char *square = square_setup - 1;

// Now square[1] == square_setup[0] == '1'
// and square[9] == square_setup[8] == '9'


That said, this is some pretty dodgy-looking C++ code (both my snippet and the OP snippet). I would not recommend that example as something to learn from.

Edit: correcting my own code snippet, I hope. I haven't actually tried compiling it because Windows.
Sponsor
Sponsor
Sponsor
sponsor
Cancer Sol




PostPosted: Mon Mar 25, 2013 7:19 pm   Post subject: Re: RE:Just wondering about an array

DemonWasp @ 3/25/2013, 1:20 am wrote:
Given as the language in question is C++, you can also abuse pointers:

code:

char square_setup[9] = {'1','2','3','4','5','6','7','8','9'};
char *square = square_setup - 1;

// Now square[1] == square_setup[0] == '1'
// and square[9] == square_setup[8] == '9'


That said, this is some pretty dodgy-looking C++ code (both my snippet and the OP snippet). I would not recommend that example as something to learn from.

Edit: correcting my own code snippet, I hope. I haven't actually tried compiling it because Windows.

I didn't learn pointers yet Razz
I just learnt how to do structures, but I still have to practice that.
It's a lot better than the one I made before though Razz (Mine was really confusing, even to me)

@Zren what do you mean by "hack" though?

And btw, is it when I use more arrays, or do it wrong with multi-dimensional arrays, it will affect the amount of memory the computer will require to use? Sorry about my grammar and everything though, lol.
Insectoid




PostPosted: Mon Mar 25, 2013 8:20 pm   Post subject: RE:Just wondering about an array

Pointers are a big deal in C++.

Anyway, a lot of the time, 'hacking' refers to things that work, but that you really shouldn't do because of potential side-effects and...well, it's like a band-aid for your code.

As for arrays, well, using lots of arrays takes up ram equal to the sum of the sizes of the arrays. A multi-dimensional array takes up the product of the size of each dimension.
Cancer Sol




PostPosted: Tue Mar 26, 2013 7:44 am   Post subject: Re: RE:Just wondering about an array

Insectoid @ 3/25/2013, 8:20 pm wrote:
Pointers are a big deal in C++.

Anyway, a lot of the time, 'hacking' refers to things that work, but that you really shouldn't do because of potential side-effects and...well, it's like a band-aid for your code.

As for arrays, well, using lots of arrays takes up ram equal to the sum of the sizes of the arrays. A multi-dimensional array takes up the product of the size of each dimension.


code:

char square[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

How much bytes of RAM would that take up compared to
code:

char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10'};

Let's just say the cpu is the same speed for testing both of those Smile
Not sure if the cpu speed will affect how much RAM it will use though, I'm not that good with computers :/
DemonWasp




PostPosted: Tue Mar 26, 2013 12:59 pm   Post subject: RE:Just wondering about an array

Well first, '10' is not a character, and neither of those declarations is valid: you have 10 entries given in an array of size [9] and 11 entries given for an array of size [10].

Bluntly, an array of nine characters will take 9 bytes, and an array of ten characters will take 10 bytes. Realistically, neither of those matters at all because even tiny embedded computers have megabytes to gigabytes of RAM now.

CPU speed does not affect RAM size. CPU speed and RAM speed are "linked" in some architectures because of how the memory access bus is implemented (this includes AMD and Intel CPUs, among many others).

The question of how much RAM is actually consumed turns out to be pretty complicated, too. I'm not sure about the exact C++ semantics, but I believe that allocations of the form type name[size] = {...} at the global scope are allocated on the stack, which has different implications for memory consumption than allocating on the heap (which are way too complicated to get into because they depend on operating system, CPU type, etc).

In short, an allocation of 10 bytes on the stack may consume 10 bytes (array itself) plus 0 or 4 or 8 bytes for the pointer (depending on whether that value can be 'baked' into the instructions issued and pointer size). On the heap, it might consume 10 bytes (array) plus 0, 4, or 8 (per pointer you use to refer to it) plus a small "header" used by malloc/free or new/delete for record-keeping, plus it may "consume" some memory by forcing subsequent allocations to use a different memory block, etc. The interactions are very complicated.
Cancer Sol




PostPosted: Tue Mar 26, 2013 4:11 pm   Post subject: Re: RE:Just wondering about an array

DemonWasp @ 3/26/2013, 12:59 pm wrote:
Well first, '10' is not a character, and neither of those declarations is valid: you have 10 entries given in an array of size [9] and 11 entries given for an array of size [10].

Bluntly, an array of nine characters will take 9 bytes, and an array of ten characters will take 10 bytes. Realistically, neither of those matters at all because even tiny embedded computers have megabytes to gigabytes of RAM now.

CPU speed does not affect RAM size. CPU speed and RAM speed are "linked" in some architectures because of how the memory access bus is implemented (this includes AMD and Intel CPUs, among many others).

The question of how much RAM is actually consumed turns out to be pretty complicated, too. I'm not sure about the exact C++ semantics, but I believe that allocations of the form type name[size] = {...} at the global scope are allocated on the stack, which has different implications for memory consumption than allocating on the heap (which are way too complicated to get into because they depend on operating system, CPU type, etc).

In short, an allocation of 10 bytes on the stack may consume 10 bytes (array itself) plus 0 or 4 or 8 bytes for the pointer (depending on whether that value can be 'baked' into the instructions issued and pointer size). On the heap, it might consume 10 bytes (array) plus 0, 4, or 8 (per pointer you use to refer to it) plus a small "header" used by malloc/free or new/delete for record-keeping, plus it may "consume" some memory by forcing subsequent allocations to use a different memory block, etc. The interactions are very complicated.


Oh whoops, I forgot Razz
I would have to put it as a string if I wanted to use 10 right?
I still don't really get what the example needs to be changed though :/
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: