# Thread: Computer guess the number ...

1. ## Computer guess the number ...

It is up to the computer to guess in seven tries the number the user is thinking of. On each move, the user must indicate whether the number tried is higher or lower than the number he/she thought of. Build your program so that the computer always wins.
My code:
Code:
```#include <time.h>
#include<stdio.h>
#include <stdlib.h>

int main(void) {

int numberGuess = 0;
int low = 1;
int high = 100;
int computerGuess = 0;

printf("Enter a number, 1 - 100: ");
scanf("%d", &numberGuess);

while (computerGuess != numberGuess)
{

computerGuess = ((high - low)/2 + low);
printf("%d ", computerGuess);

if (numberGuess > computerGuess)
{
printf("Your guess was to low \n");
low = computerGuess+1;
}
else if (numberGuess < computerGuess)
{
printf("Your guess was to high \n");
high = computerGuess-1;
}
else if (numberGuess == computerGuess)
{
printf("Yess!! you got it!\n");
}
}

return 0;
}``` 2. I need help to fix the code. 3. What is the problem?
Does it crash?
Does it allow more than 7 tries?
Doesn't it guess it at all?

One problem I see is that you set high to 100, but the user can enter sth. like 55. 4. I can't tell the computer the number. I have to tell the computer low or high. And the program always has to win. 5. One observation:  6. Statement:

1. write a program that implements a game in which the objective of the player is to guess, in 7 tries, a random integer number between 1 and 100, generated by the computer. In each move the computer must inform if the number entered is below or above the number that you want to guess. Is there a winning strategy?

2. Let's reverse the roles: the computer must guess in seven tries the number the user is thinking of. In each move, the user must indicate if the number tried is higher or lower than the number he thought of. Build your program in such a way that the computer always wins. 7. The 1 is done! Now i need to fix the code for the statement number 2, and i can't. 8. Notice for the rage from 1 to 100, 7 tries will always "win", since  9. So now how can i give the calls to the program to go high or low? 10. Almost the same code:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
int numberGuess;
int computerGuess;
int count;
int low = 1;
int high = 100;

do
{
printf ( "Enter a number, 1 - 100: " );

if ( ( count = scanf ( "%d", &numberGuess ) ) == 1 )
if ( numberGuess >= 1 && numberGuess <= 100 )
break;

puts ( "\nValue out of range. Try Again." );
} while ( 1 );

count = 0;

// NOTE: For the range 1..100 this loop will iterate
//       7 times or less, always.
while ( 1 )
{
computerGuess = ( high + low ) / 2;

printf ( "Guess #%d: % 3d ", ++count, computerGuess );

// Found! exit loop.
if ( numberGuess == computerGuess )
{
puts ( "OK" );
break;
}

if ( numberGuess > computerGuess )
{
puts ( "MORE!" );
low = computerGuess;
}
else
{
puts ( "LESS!" );
high = computerGuess;
}
}

return EXIT_SUCCESS;
}``` 11. The problem is: I can't choose the number, the number is on my mind! I only can write LESS or MORE. Screenshot by Lightshot 12. "Me" need to be me writing.

For Example:

Computer: 50
Me: LESS
Computer: 54
Me: OK

You win ... 13. Originally Posted by puctw The problem is: I can't choose the number, the number is on my mind! I only can write LESS or MORE. Screenshot by Lightshot
Well... As far as I know, the telepathic interface was not invented yet. Popular pages Recent additions #### Tags for this Thread

game, help and suggestions, help and support, help c program, help in c 