//numberHunt.cxx //By Sp 07 CS1300 Class //6 February 2007 //Program guesses a number that the user is thinking of #include #include using namespace std; int main() { int low, high, mid; char ans; bool flag; cout << "Think of a number between 0 and 1,000,000..." << endl; //Prime the loop low = 0; high = 1000000; flag = false; while(!flag) { //Invariant: low <= your number <= high mid = (low + high)/2; cout << "Is your number equal to " << mid << " (y/n)? "; cin >> ans; if(ans == 'y' || ans == 'Y') { flag = true; } else { cout << "Is your number less than " << mid << " (y/n)? "; cin >> ans; if(ans == 'y' || ans == 'Y') { high = mid - 1; } else { low = mid + 1; } } //Invariant: low <= your number <= high } cout << "Your number is "<< mid << endl; cout << "I knew that all along!" << endl; return EXIT_SUCCESS; }