Bracketing Search help

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
psudo random numbers

Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.

★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.

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
52
53
54
55
56
57
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
    int a, b, c, d=1, e, f, g=0;
    srand(time(NULL));
    cout << "Set the limit :";
    cin >> b;
    cout << "Input a number for the computer to guess :";
    cin >> c;
    do
    {
        a = (1 + rand() % b);
        d++;
        cout << a << endl;
        while (a != c)
        {

            cout << "1: Too low."<< endl;
            cout << "2: Too high." << endl;
            cin >> e;
            cout << endl;
            switch (e)
            {
            case 1:
                a = (a + (rand() % (b-a+1)));
                cout << a << endl;
                g=a;
                d++;
                break;
            case 2:
                if (g==a)
                {
                    a = (g + rand() % (b-g+1));
                    cout << a << endl;
                }
                else
                {
                    a = (rand() % (a+1));
                    cout << a << endl;
                }
                d++;
                break;
            default:
                cout << "Invalid input."<<endl;
            }
        }
    }
    while (a != c);
    cout << "The computer guessed: "<<d<<" times.";
    return 0;
}


The problem is:
How do I store the numbers so the computer won't guess a number higher/lower than the stored numbers?

Thanks!
Last edited on
Please use variable names that provide meaning. While reading your code, I have to decipher the cryptic alphabet of variables. It would be much easier if you had variables such as guessed_input, secret_number, upper_limit, etc.

How do I store the numbers so the computer won't guess a number higher/lower than the stored numbers?

Take a look at your random number generation formula a = (1 + rand() % b);
The integer modulus gives you a number in the range [0, divisor-1]. The +1 fixes the range to be [1, divisor]
The range you are interested in is [lower, upper] where lower is changed every time the computer guesses too low and upper is changed every time the computer guess too high.
Take a moment to count how many numbers there are when you start at 1 and go to 10. 10 right? 10 - 1 + 1 = 10
In general last - first + 1 is the formula for counting how many integers are in [first, last].

So we know that rand() % something will be [0, something-1]
Now try to incorporate that into creating a new random formula:
(upper - lower + 1) is the number of integers in [lower, upper]
rand() % (upper - lower + 1) will be in the range of [0, upper-lower+1-1]
lower + (rand() % (upper - lower + 1)) is the range [lower, upper]

Try to imagine if I had used the alphabet for variables during that explanation, and picture how much longer that it would take you to understand what I was trying to say. The letters u and l could have been fine for upper and lower in this situation, but in general people use longer names unless it is a math formula or a for loop counter.
Last edited on
Topic archived. No new replies allowed.