Sign in to follow this  

adding to memory question

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing an excersize in a c++ book that I have which is to reverse the roles of a simple "guess my number game" i.e I am trying to program the computer to guess a number I input. I could probably easily write a program in which the computer randomly guesses a number between 1 and 100 every time but I am trying to add more user interaction by allow the user to say "higher" or "lower" after each guess. Now, say the number to guess is 35 and the computers first guess is 40 (the user says lower) then the next guess is 30 (the user inputs higher) I am struggling to get the computer to remember the 40 as well as the 30 so that the formula for the next guess can generate a random number between 30 and 40. Any help appreciated Cheers P.S I will post some of my code later, (at work atm)

Share this post


Link to post
Share on other sites
What you have to do is clip the range of numbers that it could possibly be.

For example:

0 <= X <= 100

"i guess 30"
"sorry, its higher"

new range:

30 <= X <= 100

"i guess 70"
"sorry its lower"

30 <= X <= 70

And so forth.

The best way of having the computer guess is to find the middle number of the range and have the computer guess that. For example the computer would guess 50 to halve the range (or get it right if it is 50). The guess half way between the new min and max.

Note that using the halving method for guess is the optimal method, not the most human-like, if that is what you are trying to achieve. Though it is likely that anyone with a brain would take the same approach.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
I undertsand that but the code I have written is in a do loop while

guess != thenumber;

So everytime the loop runs, it loses the last number in the guessing sequence from the memory.

So when it is between 30 and 70 say, it will guess between 1 and 70 still, not 30 and 70.

Share this post


Link to post
Share on other sites
Store the variables outside of the loop. For example:
unsigned int min( 0 );
unsigned int max( 100 );

unsigned int numberToGuess( 35 );

bool keepTrying( true );

while ( keepTrying )
{
.. GuessNumber ..
if number is correct then
keepTrying =false;
else
if number is too low
increase min to guessed number
else
decrease max to guessed number
}



Dave

Share this post


Link to post
Share on other sites
You know, if you want to get anywhere coding you really need to experiment and figure things out for yourself, here is a perfect example where you could do that.

Just trying what you asked would have taken a tiny amount of time in comparison to typing and waiting for a reply to your post.

I therefore suggest you try, and once you try, if it does/doesnt work carry on reading/asking to understand why.

Basically aslong as you declare a variable in the same scope/level as the function or higher you can access it, anything in a lower scope/level you can not.

I will try and demonstrate this below...


void Function( void )
{ //top level scope
bool a = true;

while( 0 )
{ //second level scope

//a is acessable here as it is defined at a higher level

bool b = false;

} //end of second level scope

// a is still accesable here as we are back to the top level scope
// b is not accessable here as it is defined at a lower scope.

}


I hope this is clear.

Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by chipmeisterc
You know, if you want to get anywhere coding you really need to experiment and figure things out for yourself, here is a perfect example where you could do that.


While in general I think that this is a great philosophy there are some serious pitfalls if you try doing this with C++. For example


int a[5];

for(int i=1; i<=5; i++)
a[i] = 0;

for(int i=1; i<=5; i++)
std::cout<<a[i]<<'\n';



This will probably compile and work as expected on most compilers. Except that it's not valid C++ code but undefined behavior. Unfortunately because it is undefined compilers will often "do the right thing" and only fail under certain difficult to reproduce conditions. This is a trivial example but undefined behavior is everywhere in C++ - one of the reasons that I discourage it as a beginners language.

Share this post


Link to post
Share on other sites

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this