Bracketing Search Exercise help!(4 star)

Spoiler code in here don't look if you haven't solved this yourself and you want it to remain challenging.

I'm trying to figure out where I messed up on logic (i seem to always get <7 computer tries BUT the computer can't solve the problem if I enter 100 as the answer to be guessed)

E:scratch that I just got an 8 );

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
//Bracketing search
//Exercise 5 star 4
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    srand((unsigned)time(0));
    int answer;
    cout << "Enter a number(1-100) as the answer: ";
    cin >> answer;
    int counter = 1;
    int upper_limit = 100;
    int lower_limit = 1;
    //gaurantee a guess in range [1,100]
    int computer_guess = rand()%100+1;
    while(computer_guess != answer)
    {
        //restrict range to [lower_limit,upper_limit]
        if(computer_guess > answer)
        {
            upper_limit = computer_guess;
        }
        if(computer_guess < answer)
        {
            lower_limit = computer_guess;
        }
        //get new number w/ max value of [lower_limit,upper_limit]/2
        //i.e. if the computer guess 88 and answer is 50
        //new random number will be (1+88)+1/2 AKA 45
        computer_guess = rand()%(upper_limit+lower_limit)/2;

        //evaluate our newest number
        //if the new number is less than the midpoint of our range
        if(computer_guess < (upper_limit+lower_limit)/2)
        {
            //add the difference so our guess is exactly half of our range
            computer_guess += ((upper_limit+lower_limit)/2-computer_guess);
            //if upper_limit+lower_limit is odd add 1 to our computer_guess
            if((upper_limit+lower_limit)%2==1)
                computer_guess+=1;
        }
        else if(computer_guess > (upper_limit+lower_limit)/2)
        {
            //subtract the difference so our guess is exactly half of our range
            computer_guess -= ((computer_guess)-(upper_limit+lower_limit)/2);
            //if upper_limit+lower_limit is odd subt 1 from our computer_guess
            if((upper_limit+lower_limit)%2==1)
                computer_guess-=1;
        }

        //increment the guesses counter
        ++counter;

    }
    cout << "It took the computer " << counter << " tries to guess the correct answer.";

    return 0;
}
Last edited on
Try;
1
2
3
4
5
6
7
computer_guess = (top + min) / 2;
if(computer_guess > answer)
    min = computer_guess + 1;
else if(computer_guess < answer)
    top = computer_guess - 1;
else
    break;


In your while-loop, sorry for the indentation - but I'm writing this directly in the forum and it doesn't accept tab :P

At least I tried to make decent indentation ;)

Note: that is highly untested, so no guarantees that it will work without some poking.

Edit;
Someone, do correct me, but... isn't this called "binary search"? ^^(or at least what I tried to make without testing it)
Last edited on
yeah it's a binary search, thanks i'll give it a try in a few.
Does it work?
Is this solution really correct? The instructions say that the user must secretly decide the number and the user must tell it if it's too high or too low, not enter the answer.
(modifying means editing it a bit, not making an entirely new code, right?)

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 to output how many guesses it took the user to correctly guess the right 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.

★★★★ Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
Is this solution really correct? The instructions say that the user must secretly decide the number and the user must tell it if it's too high or too low, not enter the answer.
(modifying means editing it a bit, not making an entirely new code, right?)


And that's why you use binary search.

You enter a number between 1-100 and then the program uses binary search to find the number you entered = program found the number you were thinking of in less than 7 guesses.

Or you're supposed to tell the computer if it was too high or too low, but you can edit the binary search so it works that way as well. But I'm lazy and prefer to just enter the number once instead of answering "higher" or "lower" like 6 times ;)

But the program does what it is supposed to do. More or less, if it's like you said then it'll take about 1-2 min to edit it to function that way.
Okay... it's actually good to be lazy, that way you automize things more. ^^

Can you take a look at my dungeon crawl? http://cplusplus.com/forum/beginner/74616/

I get two trap moves if I use getch(), but only one when I use std::cin>>char.
Last edited on
Topic archived. No new replies allowed.