In this lab we will be doing a bit of garden variety AI. Programs like these have been around for as long as there have been electronic computers. Before that, this sort of logic was implied in many contexts. We are building a program that can guess a number between 1 and 20 in base 2 log(20) time. See the program you will be implementing on pages 186-187 in the C++ book. Happy to say starter code is will help you begin implementing this integer guesser.

TODO 0 Before running, make sure you have settled upon values for both high and low.

#include< windows.h>
#include< iostream>
#include< cmath>

using namespace std;

int main()
{
// TODO 3 Generalize to support any reasonable range of values
int high, low, guess, count, ceiling;
bool done(false);
char answer;
const char YES('Y'), NO('N');

ceiling = (int) (ceil(log((double)high)) + 1);
cout << "Think of a number between " << low << " and " << high
<< " and I will guess it inn" << ceiling
<< " or fewer trys. Just answer y(es) or n(o) to my questions.n"
<< "Are you thinking of a number? " << endl;
cin >> answer;
switch (toupper(answer))
{
case YES:
while (!done && high >= low)
{
guess = (high + low) / 2;
cout << "Are you thinking of " << guess << '?' << endl;
cin >> answer;
switch (toupper(answer))
{
case YES:
cout << "I guessed it in " << count << " trys." << endl;
if (count > ceiling) cout << "Good pick.." << endl;
done = true;
break;
case NO:
++count;
cout << "Is " << guess << " larger?" << endl;
cin >> answer;
if (toupper(answer) == YES)
{
// TODO 1 How should either high or low be set?
}
else
{
// TODO 2 How should either low of high be set?
}
break; //case NO
default:
cout << "Don't support " << answer << endl;
done = true;
} //end inner switch
} //end while
break;
case NO:
cout << "OK..Goodbye. " << endl;
break;
default:
cout << "Dont support " << answer << endl;
}//end outer switch
Sleep(4000);
return 0;
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.