I finally solved the guessing machine!

So i finally solved the bracketing search beginner excercise!
And, to learn c++ better, i usually come here to ask for more
efficient ways of writing the program, after i have made an attempt myself.

Here is my 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cmath>
#include<string>

using namespace std;

int main() {

	int maximum = 100;
	int minimum = 0;
	int guess;
	int secretnumber;
	int count = 1;
	
	string i;
	
	cout << "Enter a number between 1 and 100, that the computer will guess." << endl;
	cin >> secretnumber;
	
	do {

		guess = (( maximum + minimum ) / 2 );
		
		if ( guess == secretnumber )
			break;

		cout << "Is the secret number higher or lower than " << guess << "?" << endl;
		count ++;
		cin >> i;

		if ( i == "higher" )
			minimum = ( guess + 1 );
		else if ( i == "lower" )
			maximum = (guess - 1);
		else { cout << "That's not a valid answer. Try again." << endl;
		break; }
		
	}

	while ( guess != secretnumber);

	cout << "The number is " << guess << " and i solved it in " << count << " guesses!" << endl;

	system("pause");
return 0;
}


Any ways those who are more experienced would have solved this?
I'd love to know. Thanks.
Last edited on
If there is no one who can come up with a more efficient way, i'll take it as a compliment, haha.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main(void) {
    int num, compGuess;
    int max = 100, min = 0, count = 1;
    std::cout << "Enter a number between 0 and 100: ";
    std::cin >> num;
    while(compGuess != num && count < 8) {
        compGuess = (max + min) / 2;
        std::cout << compGuess << " is <h>igher, <l>ower or <c>orrect? ";
        char ch;
        std::cin >> ch;
        (ch == 'h'?max:min) = compGuess;
        ++count;
    }
    std::cout << "The nunber is " << num << " and it was solved in " << count-1 << " guesses!\n";
    return 0;
}

My only complaint is the system call towards the end of main.
Last edited on
1
2
3
4
5
6
7
#include <algorithm>
#include <thematrix.h>

int main(void){
    auto oneLiner = std::do_comp_guess(numbers.begin(),numbers.end(),TheArchitectFunc(numbers));
   return 0;
}



//End humor
Could you elaborate xhtmlx?
Obviously im new to this... :)

Also, i disagree on using chars and <l>ower etc. when strings are more user friendly, and sometimes easier to work with. Though you are correct, it is more efficient, which was what i originally asked for.

Last edited on
It only replaces your first two if statements. Using the tenary operator is just like an if-else chain.
1
2
3
4
if(ch == 'h')
      max = compGuess;
else
      min = compGuess;

I didn't even bother having error checking in such a simple program.

Last edited on
if you want comp to guess 100, max should be 101, because of integer division.

suppose you set min = 0, max = 100

number: 100
50 (100/2) ? higher
75 (150/2) ? higher
87 (175/2) ? higher
93 (187/2) ? higher
96 (193/2) ? higher
98 (196/2) ? higher
99 (198/2) ? higher
99 (199/2) ? higher
99 (199/2) ? higher
99 (199/2) ? higher
...
and on and on and on... even if you set limit count < 8 the computer will not be able to guess the correct answer 100.

Last edited on
Thank you tnt, i did not think about this.
Html, that does look more clean, and efficient.

Thank you all :)
Topic archived. No new replies allowed.