/* CSC15 Lab 7 Due Wednesday 4/4/2001 at BEGINNING of Class The lab asks you to design an algorithm around a while loop, and using a random number generator. The basic form of a while loop is similar to an if statement: while ( condition ) { // code repeated while condition is true } The while loop executes its code if the boolean "condition" is true. After executing its code, it rechecks the condition, and executes the code again if it remains true. If condition is false, nothing is executed. If condition is true, then something inside the body of the while loop must eventually make the condition false. Otherwise the loop will run forever. int i; // loop index i = 0; while (i < 7) { cout << "i is now " << i << endl; i = i+1; } This above loop will execute 7 times, printing out the value of i from 0 to 6. IT'S IMPORTANT THAT YOU UNDERSTAND THAT IT PRINTS I FROM 0 TO 6, NOT 0 TO 7. When i reaches 7 the boolean condition becomes false, and so the body of the loop is not executed again. The condition is checked BEFORE each iteration of the loop. For this lab, you are to write a program where the user thinks of a number from 0 to 1023, and the computer must guess it. It's important to device the algorithm first, so you should sketch out the code on paper. The first guess for the computer should obviously be 500 - halfway between the minimum and maximum possible numbers. The user answers if this guess is correct, less than the correct answer, or larger than the correct answer. The computer makes the next guess based on this response. The loop stops when the computer has guessed the correct answer. Let's say the computer's first guess is 511 and you answered that the real number is larger, that means the possible answer is somewhere between 511 and 1023. The computer's next guess should be halfway between 511 and 1023. If the user answered that it's smaller, than the next guess should be halfway between 0 and 511. So the general strategy is to use two variables to keep track of the range of possible values - the minimum and the maximum. Initially, minimum is 0 and the maximum is 1023. You figure out the rest. Here's a possible sample sesssion of your program: computer: think of a number between 0 and 1023 and I'll try to guess it. computer: is the number (l)ess than, (g)reater than, or (e)qual to 511? user: l computer: is the number (l)ess than, (g)reater than, or (e)qual to 255? user: g computer: is the number (l)ess than, (g)reater than, or (e)qual to 383? user: g .... computer: is the number (l)ess than, (g)reater than, or (e)qual to 455? user: e computer: I guessed it in 9 tries! I'm really smart! Your program must: A. terminate when the computer guesses the right number B. terminate when the minimum gets too close to the maximum (which means the user cheated and misled the computer). The computer should print out a message of indignation. C. use an additional variable to keep track of how many guesses the computer had to make before getting it right. D. If there are 1000 possible numbers, it'll take the computer about 10 tries to guess the right number. If there are 500 possible numbers, it should take 9 tries. If there are 2000 possible numbers, it should take about 11 tries. Everytime the number of possibilities double, the number of times the computer may have to guess increases by one. Try to come up with a mathematical formula to describe this behavior. That is, suppose there are n possible number, how many guesses (in terms of n) should the computer have to make? Hint: to be precise, the computer should guess at most 3 times if there are 8 possible numbers. What to hand in: 1. paper writeup of code. 2. source code, well commented. 3. two sample sessions, one standard, one with the user misleading the computer. Copy and paste the output to the end of your source file. as comments. The answer to part D should be included in comments too. */