Homework x: Extra Credit Loops
Wednesday, March 14, 2012[Up]
NOTE: Due 3/28
Complete the following:
- Create a new file called hw_x.c
- NOTE: You will use loops inside loops for this program
- NOTE: It will probably make it easier to break the program into functions, but you don't have to
- You are going to create a program that will try to guess the user supplied secret number (don't program it to cheat!)
- You can start with your hw07 if you want, but you are not required to.
- The program will, when it starts, ask the user for a secret number (integer) between 0 and 30000
- If the user enters an invalid number, ask again until a valid number is entered
- The program will use a specific search strategy called a binary search to try to guess the number
- In a binary search the program always knows the lowest possible value for the secret number (starts as zero) and the highest possible value for the secret number (starts at 30000)
- Each time the program makes a guess, it guesses (the lowest possible value + highest possible value) / 2
- After each guess, the program determines if the number guessed was lower, higher, or equal to the secret number
- The program should print out each guess, and the result of that guess
- NOTE: since the user input the secret number, the user doesn't need to say higher or lower or equal, the progrma will calculate the relationship of its current guess to the secret number
- If the guess was higher than the secret number, the program changes its lowest possible value to be the guessed number
- If the guess was lower than the secret number, the program changes its highest possible value to be the guessed number
- If the guess equaled the secret number, then the program says it won
- Once the program wins, it will ask the user if they want to play again, and will get a y/n response from the user
- If the user chooses to play again, ask for a new secret number and start a new binary search
- If the user chooses not to play again, exit from the program
- An example of the output from a possible game is given below, with the user input in bold
linux3[1]% ./hw_x Please input a secret integer between 0 and 30000: 27300 Now let me try to guess 15000? Too low 22500? Too low 26250? Too low 28125? Too high 27188? Too low 27656? Too high 27422? Too high 27305? Too high 27247? Too low 27276? Too low 27290? Too low 27297? Too low 27300? Got it! Do you want to play again y/n? y Please input a secret integer between 0 and 30000: 7500 Now let me try to guess 15000? Too high 7500? Got it! Do you want to play again y/n? n Thanks! linux3[1]%
- Create a script of you running your program (only once is required).
- Once you are done, use submit to submit two files, hw_x.c and typescript
linux3[1]% submit cs104_sheets hw_x hw_x.c typescript