Bracketing Search

Im trying to do the beginner exercise called Bracketing Search.


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    int randomNumber;
    int guess, userInput;
    int numberOfGuesses = 0;
    bool Quit = false;
    bool QuitTwo = false;

    while(!QuitTwo)
    {
        cout << "Enter a number between 0 and 100 that the computer must guess" << endl;
        cin >> randomNumber;
        if(randomNumber >= 0 && randomNumber <= 100)
        {
            QuitTwo = true;
        }
    }

    srand(time(0));
    for(guess = 0; guess < 1; guess++)
    {
        guess = (rand()%100);
    }

    while(!Quit)
    {
        cout << "\nThe computers guess is " << guess << endl;
        cout << "\nIf the guess is too low press  (1)" << endl;
        cout << "If the guess is too high press (2)" << endl;
        cout << "Tf the guess is correct press  (3)" << endl;
        cin >> userInput;

        switch(userInput)
        {
            case 1:
                if(guess < randomNumber)
                {
                    guess++;
                    numberOfGuesses++;
                }
                break;
            case 2:
                if(guess > randomNumber)
                {
                    guess--;
                    numberOfGuesses++;
                }
                break;
            case 3:
                if(guess == randomNumber)
                {
                    numberOfGuesses++;

                    Quit = true;
                    if(Quit == true)
                    {
                        cout << "\nThe computer has guessed correct" << endl;
                        cout << "The right number is " << randomNumber << endl;
                        cout << "The computer used " << numberOfGuesses << " tries" << endl;
                    }
                }
                break;
            default:
                break;
        }
    }
    
    return 0;
}



My problem is that i don't know how to make the computer smarter :P
I need it to do something else than ++ and --, when it guesses wrong.
Would really appreciate some help.
I would create a function that initializes the generator of random numbers and returns another random number.
Look at a binary search, and apply that algorithm to this problem. Start in the middle at 50, and each time, cut the search ranges in half. So for 13:
50 Too High
25 Too High
12 Too Low
18 Too  High
15 Too High
13 Correct


In my case I always rounded down
Topic archived. No new replies allowed.