A PROGRAM FOR COMPUTER TO GUESS A NUMBER

// A PROGRAM TO GUESS THE NUMBER BY COMPUTER
#include <iostream>
using namespace std;

int main () {
constexpr int MAX = 100;
constexpr int MIN = 1;
int high = MAX, low = MIN, middle;
char answer;
cout << "Guess the number between " << MIN << " and " << MAX << ": \n";
while (low){
middle = (high + low)/2;
if (low == middle){
cout << "is your number is equal to " << middle << " ? enter (y/n): ";
cin >> answer;
if (answer == 'n'){
cout << "the number is " << high;
break;
}
else {
cout << "the number is " << low;
break;
}
}

cout << "is your number is <= " << middle << "? enter (y/n): ";
cin >> answer;
if (answer == 'y')
high = middle;
else low = middle;


}
return 0;

}

The above code takes 7 questions to guess number 1 and 8 questions to guess other numbers. If i remove, the "equal to" question, i am able to guess numbers 2 to 100 in seven questions, whereas program not able to guess number `1. How to make this program to guess the numbers with in or max seven questions?. This is a text book exercise. Thanks in advance
I recently wrote something similar, The formula could be a bit more efficient, but it can guess numbers in the millions from around 40-50 goes. It handles hundreds pretty well too. Again, the formula isn't the best, but it's pretty efficient, even though it loops through a few numbers.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int randRange(int low, int high)
{
    return rand() % (high - low) + low;
}

void clearScreen()
{
    for (int loop = 0; loop < 100; loop++)
    {
        cout << endl;
    }
}

int main()
{
    
    srand(time(NULL));
    int maxRange = 100;
    cout << "Please enter the max range: ";
    cin >> maxRange;
    int guessNumber = randRange(0, maxRange);
    int userInputGuess = 0;
    int userGuessCount = 0;
    int guessType = 1;
    int compGuessNumber = 0;
    int addmin = maxRange;
    
    while (1)
    {
        if (guessType == 1)
        {
                compGuessNumber += addmin;
        }
        if (guessType == 2)
        {
            if (compGuessNumber == 0 || compGuessNumber == 1 || addmin == 0 || addmin == 1)
            {
                addmin = 2;
            }
            addmin /= 2;
            compGuessNumber -= addmin;
            
        }
        if (guessType == 3)
        {
            compGuessNumber -= addmin;
        }
        
        if (compGuessNumber == guessNumber)
        {
            userGuessCount++;
            cout << "Contragulations!!!! You got it right. It only took " << userGuessCount << " Times!";
            break;
        }
        
        if (compGuessNumber < guessNumber)
        {
            if (guessType == 2)
            {
                guessType = 3;
            }
            if (guessType == 1)
            {
                userGuessCount++;
                guessType = 1;
                cout << compGuessNumber << endl;
            }
            if (guessType == 3)
            {
                guessType = 1;
            }
            
        }
        
        if (compGuessNumber > guessNumber)
        {
            userGuessCount++;
            guessType = 2;
            cout << compGuessNumber << endl;
        }
    }
}


I didn't comment it, so if you have any questions, feel free to ask.

EDIT: The actual formula is between line 37 - 55

EDIT2: Compliled it again, it can guess numbers within 30-40 goes when the number is at max int range

EDIT3: It probably would have looked better and performed better if I used functions, but I was really lazy when I made it

EDIT4: (I should probably revise my comments before writing them) I originally made this program as a sort of "game". I finished it then changed it to what it is now, that's why it has odd text output as well as coding

EDIT5: (sigh)

output:
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
Please enter the max range: 9999999999
2147483647
2147483647
1610612736
1610612736
1610612736
1610612736
1577058305
1560281090
1560281090
1556086787
1553989636
1553989636
1553465349
1553465349
1553334278
1553268743
1553235976
1553235976
1553235976
1553235976
1553235976
1553234953
1553234953
1553234698
1553234698
1553234698
1553234698
1553234698
1553234698
1553234695
1553234694
1553234693
Contragulations!!!! You got it right. It only took 33 Times!


1
2
3
4
5
6
7
8
9
Please enter the max range: 100
100
100
75
75
75
75
74
Contragulations!!!! You got it right. It only took 8 Times! 


It loops through a few numbers(not sure why), but it's efficient and works, so i'm happy with it :D
Last edited on
When doing problems like this it's very useful to be clear about whether the number can be equal to the high and/or low boundaries. In your case, the number can be equal to either one. That means you can adjust the code slightly:
1
2
3
4
5
6
cout << "is your number <= " << middle << "? enter (y/n): "; // slight change for proper English
cin >> answer;
if (answer == 'y')
    high = middle;
else
    low = middle+1;  // note the "+1" 
@dhayden, Thank you sir. It resolved my issue.
Topic archived. No new replies allowed.