### 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

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include 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 : "<
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.