Created
November 16, 2012 23:17
-
-
Save kgleeson/4091808 to your computer and use it in GitHub Desktop.
Computer guess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(){ | |
int guess = 50; | |
int guess_attempts = 0; | |
int answer ; | |
int helper = guess / 2; | |
cout << "Enter your number: "; | |
cin >> answer; | |
while(guess != answer){ | |
if(guess > answer){ | |
guess -= helper; | |
} | |
else if(guess < answer){ | |
guess += helper; | |
} | |
if(helper > 1){ | |
helper /= 2; | |
} | |
cout << guess << endl; | |
guess_attempts++; | |
} | |
cout << "Guessed in " << guess_attempts << " attempts" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment