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:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

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 " <<numbertoguess<< "\n";
             cout<<"the number of tries is " <<retry<< "\n";
             break;
         }
     }   while (true);
    return 0;
}
int random()
{
    return rand()%100+1;
}


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!
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>
#include <cstdlib>
#include <ctime>

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<< "\n";
             cout<<"the number of tries is " <<retry<< "\n";
             break;
         }
         else if (tries<numbertoguess)
         {
             tries=random(tries,unknown)
         }
         else if (tries>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.


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
#include <iostream>
#include <cstdlib>
#include <ctime>

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 <<endl;
         if (computer_guess==number_to_guess)
         {
             cout << "The number the Computer had to guess was " << number_to_guess << endl;
             cout << "It took the computer " << num_attempts;
             if (num_attempts==1)
                cout << " guess!!";
             else
                cout << " guesses!!";
             return 0;
         }
         else if (computer_guess < number_to_guess)
             low_for_guess = computer_guess;
         else if (computer_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.