You are using a version without Ads of this website. Please, consider donating:

### Guess the number by the computer

Hi guys!
I'm reading jumping into c++ to learn this language. I'm now in chapter 9 where it asks: "Write a program that solves the guessing game from part 1. How many guesses does your program need?"

I've already done it the easy way. Here is the code:

 ``123456789101112131415161718192021222324252627282930`` ``````#include #include #include using namespace std; int random(); int main() { srand(time(NULL)); int numbertoguess=random(); int tries=0; int retry=0; do { retry++; tries=random(); if (tries==numbertoguess) { cout<<"the result is " <

The thing is I wanted to take it to the next level. I wanted to make the program think like a human. I wanted to change the interval with every try to make it smaller and smaller (like you would if you played this game). For example, if the number we need to find is 57 and the program tries 40, I want to change the random interval to 40-100. If the second try is 70, I want the interval to become 40-70. I don't know how I can keep one of the values and change the other. I came with this code, I just need to replace the `unknown`.
Keep in mind that I'm a beginner, so if it's really complicated tell me what I need to learn to achieve this! If you need more details to help me don't hesitate to tell me!
Thank you very much!
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include using namespace std; int random(int low,int high); int main() { srand(time(NULL)); int numbertoguess=random(1,100); int tries=random(1,100); int retry=0; do { retry++; if (tries==numbertoguess) { cout<<"the result is " <numbertoguess) { tries=random(unknown,tries) } } while (true); return 0; } int random(int low,int high) { return rand()%(high-low)+low; }``````
i personally would just have the program take half the distance between each guess

eg. guesses 50, to high, guesses 25, to low, guesses 38 etc

thats how i personally play that game.

but if you really want to have the computer guess something random in a range you could always do something like

x = lowest possible number
y = highest possible number

z = rand()% (y-x)

guess = x + z

that way you get a random number in that range.
That's what I'm trying to do. I want the computer to make the distance smaller every time he guesses a number. For example (using the numbers you gave in your reply) if 50 is too high, I want the distance to change from 1 to 50. After if 25 is too low, I want to change from 25 to 50. I just don't know how to change one of the values and keep the other. I think I just need to replace the "unknown" in my code.
you are implementing a bad version of the binary search algorithm.

i suggest you study the organised version:

http://en.wikipedia.org/wiki/Binary_search_algorithm
Here is the source code for what you asked - I am new to c++ also but not to programming. I learn best from example myself. If you have any questions concerning this code I will be happy to help with real answers. This is how I believe a beginner would accomplish this task. If anyone wants to point out beginner style options or alternative solutions to mine please feel free. There is always more then one way to skin a cat.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include using namespace std; //FUNCTION DECLARATIONS int get_random_num(int low,int high); int main() { int number_to_guess; int computer_guess; int num_attempts=0; int low_for_guess = 1; int high_for_guess = 100; srand(time(NULL)); number_to_guess=get_random_num(low_for_guess,high_for_guess); cout << number_to_guess << " <<---Dont Worry the Computer cant see this." << endl << endl; do { num_attempts++; computer_guess = get_random_num(low_for_guess,high_for_guess); cout << "Computer guess #" << num_attempts << " was " << computer_guess << " which is between " << low_for_guess << " and " << high_for_guess < number_to_guess) high_for_guess = computer_guess; } while (true); return 0; } // END MAIN int get_random_num(int low,int high) { return rand()%(high-low)+low; } ``````
Last edited on
 rand()%(high-low)+low

this seems like the low would cancel itself out
the +low is added after the rand function has been implemented.
@iamk2 Thank you very much! Your code is very simple! I can't understand why I didn't think of a simple solution like that! Thank you so much for helping me!
@rechard3 I'm checking the link you gave me right now!

Thanks everyone for helping me!
Have a great day!
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: