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;
}