Edited by Bacterius, 21 October 2012 - 07:14 PM.
#21 Crossbones+ - Reputation: 3558
Posted 21 October 2012 - 07:14 PM
#22 Members - Reputation: 246
Posted 22 October 2012 - 07:50 AM
How can there be 9 spaces in an array of size 8? That makes no sense.
If we have for example
int banana[5];
There is in fact 6 spaces.
banana[0] = 1; banana[1] = 2; banana[2] = 3; banana[3] = 4; banana[4] = 5; banana[5] = 6;
Arrays start off at zero, so in an 8 space array there is 9 spaces.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
I love the word Clicky
#23 Members - Reputation: 246
Posted 22 October 2012 - 07:52 AM
Otherwise, you can change the < sign to <= .
This was the confusion. Sorry.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
I love the word Clicky
#24 Members - Reputation: 5882
Posted 22 October 2012 - 08:12 AM
How can there be 9 spaces in an array of size 8? That makes no sense.
If we have for exampleint banana[5];
There is in fact 6 spaces.banana[0] = 1; banana[1] = 2; banana[2] = 3; banana[3] = 4; banana[4] = 5; banana[5] = 6;
Arrays start off at zero, so in an 8 space array there is 9 spaces.
No, that's not how it works. Array indices do start at zero, so in an array of size 8, the last valid entry has index 7.
#25 Members - Reputation: 187
Posted 22 October 2012 - 10:01 AM
How can there be 9 spaces in an array of size 8? That makes no sense.
If we have for exampleint banana[5];
There is in fact 6 spaces.banana[0] = 1; banana[1] = 2; banana[2] = 3; banana[3] = 4; banana[4] = 5; banana[5] = 6;
Arrays start off at zero, so in an 8 space array there is 9 spaces.
See Alvaro's response. But please try to take advice onboard , or at least make sure you are correct before you end up giving wrong advice. I probably should have explained it when I said it. Alas, No harm done
Edited by dAND3h, 22 October 2012 - 10:03 AM.
#26 Members - Reputation: 246
Posted 22 October 2012 - 10:18 PM
No, that's not how it works. Array indices do start at zero, so in an array of size 8, the last valid entry has index 7.
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
I love the word Clicky
#27 Crossbones+ - Reputation: 3558
Posted 22 October 2012 - 10:23 PM
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control."
An array[n] only has valid indices between 0 inclusive and n exclusive.
Edited by Bacterius, 22 October 2012 - 10:39 PM.
#28 Members - Reputation: 246
Posted 22 October 2012 - 11:59 PM
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control."
An array[n] only has valid indices between 0 inclusive and n exclusive.
Well you learn something new everyday
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
I love the word Clicky
#29 Senior Moderators - Reputation: 3113
Posted 23 October 2012 - 12:05 AM
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control."
An array[n] only has valid indices between 0 inclusive and n exclusive.
Well you learn something new everydayI'm not sure why this wasn't mentioned in my C++ book but thank you
Get a better C++ book then, because whatever one you have probably should just be burned.
It should also be noted that the valid range of pointers to an array A with a size n is A to A + n inclusive. The main thing is that the dereferencable range of A is A to A + n exclusive.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX
#30 Members - Reputation: 246
Posted 23 October 2012 - 12:16 AM
Get a better C++ book then, because whatever one you have probably should just be burned.
Beginning C++ Game Programming by Michael Dawson. Burn away
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
I love the word Clicky
#31 Members - Reputation: 98
Posted 23 October 2012 - 02:04 AM
I have tried fisherYates shuffle but it still does not work.here is my code
void Computer::move_player_O()
{
srand(time(NULL));
const int length=10;
int array[length];
for(int t=1;t<=9;t++)
array[t]=t;
fisherYates(array,length);
player_O=array[t];
t++;
}
void Computer::check_player_O()
{
board[player_O]='O';
}
int array[length]; this will not work try
int *array=new int[length];
and after you have done working
delete[] array;
#32 Members - Reputation: 5882
Posted 23 October 2012 - 03:49 AM
I have tried fisherYates shuffle but it still does not work.here is my code
[...]
const int length=10;
int array[length];
[...]
int array[length]; this will not work try
Why wouldn't it work? `length' is a compile-time constant and using it as an array length is perfectly kosher.
You only need to do that if `length' is not known at compile time. Even in that case, some compilers (e.g., gcc) do support variable-length arrays. VLAs are part of the C99 standard and they almost made it to the C++11 standard.int *array=new int[length];
and after you have done working
delete[] array;






