Homework x: Extra Credit Loops

Wednesday, March 14, 2012     


[Up]


NOTE: Due 3/28


Complete the following:

  1. Create a new file called hw_x.c
  2. NOTE: You will use loops inside loops for this program
  3. NOTE: It will probably make it easier to break the program into functions, but you don't have to
  4. You are going to create a program that will try to guess the user supplied secret number (don't program it to cheat!)
  5. You can start with your hw07 if you want, but you are not required to.
  6. The program will, when it starts, ask the user for a secret number (integer) between 0 and 30000
  7. If the user enters an invalid number, ask again until a valid number is entered
  8. The program will use a specific search strategy called a binary search to try to guess the number
  9. 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)
  10. Each time the program makes a guess, it guesses (the lowest possible value + highest possible value) / 2
  11. After each guess, the program determines if the number guessed was lower, higher, or equal to the secret number
  12. The program should print out each guess, and the result of that guess
  13. 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
  14. If the guess was higher than the secret number, the program changes its lowest possible value to be the guessed number
  15. If the guess was lower than the secret number, the program changes its highest possible value to be the guessed number
  16. If the guess equaled the secret number, then the program says it won
  17. 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
  18. If the user chooses to play again, ask for a new secret number and start a new binary search
  19. If the user chooses not to play again, exit from the program
  20. 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]%
    

  21. Create a script of you running your program (only once is required).
  22. 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