guessing number

i made half of the program but i can not make the computer guessing system more efficient can you make it solve by only if computer takes less than 7 guesses and solves it

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
#include <iostream >
#include<conio.h>
#include<ctime>
using namespace std;
int main()
{
	int count=0;
srand(time(0));
    int s_num=rand()%100+1;
	int user_num;
	cout<<"Enter the guessing number\n";
	cin>>user_num;
	while(user_num!=s_num)
	{
		if(s_num>user_num)
		{
			cout<<"Computer guessed : "<<s_num;
			cout<<" TOO high \n";
			/*s_num=s_num/2;*/
			s_num=rand()%s_num+1;
		}
		else if(s_num<user_num)
		{
			
			cout<<"Computer guessed : "<<s_num;
			cout<<"too low\n";
			s_num=rand()%(100+1)-s_num;
		}
		
		s_num=rand()%100+1;
		cout<<"Computer guessed : "<<s_num;
		count++;

	}
	cout<<"you took :"<<count<<"turns to guess\n";
getch();
}
Read up on the concept behind binary search - it works the same way here ;)
Can it be done efficiently without using array
Yes - pretend the array is all the numbers between min and max. They're already sorted (unless you count numbers out of order like a crazy person...).
Last edited on
I am confused lol i think i need to do something with the range of rand number for example if given number is 65 and computer guesses 45 , i should set the range from 45 to 100 and never go lower than the number already guessed . i face problem in the how to put 45 to 100 in rand genrator range area
Start with min and max at 0 and 100. Guess at (min+max)/2, then depending on if it's too high or too low, change min or change max. It's just binary search but without an array ;)
I tried that made still it doesnt guess under 7 counts
Even with the most effective binary search, it cannot be done in under 7 guesses, but it can always be done in 7 guesses exactly. Are you sure <=7 guesses is not what is required?

Yes if its exactly 7 its still ok but it gives an average of 28 counts may be i am implementing the min and max logic in a wrong way could you modify the code to give count 7 or under 7
Topic archived. No new replies allowed.