Advertisement
Guest User

Exercise

a guest
Aug 20th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. /*Modify the guessing program of Listing 8.4 so that it uses a more intelligent guessing strategy.
  2. For example, have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct.
  3. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75.
  4. If that guess is high, let the next guess be halfway between 75 and 50, and so on. Using this binary search strategy,
  5. the program quickly zeros in on the correct answer, at least if the user does not cheat.
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12.     const int MIN = 1, MAX = 100;
  13.     int calc_guess = 50, num_of_guess = 1, guess = 50;
  14.     int diff_resp = 1;
  15.     char choice;
  16.  
  17.     printf("Think of a number, 1-100, and I'll guess what it is.\nJust let me know if I'm too high or too low.\n");
  18.     printf("When you have thought of your number, hit enter.\n");
  19.    
  20.     while (getchar() != '\n')
  21.         continue;
  22.  
  23.     printf("\nMy first guess is 50.\n");
  24.     printf("Enter the letter that represents your choice.\n\n");
  25.     printf("a) Too High   b) Too Low   c) Correct\n");
  26.  
  27.     while ((choice = getchar()) != 'c') {
  28.  
  29.         //Input Validation
  30.         if (choice == '\n')
  31.             continue;
  32.  
  33.         if ((choice != 'a') && (choice != 'b')) {
  34.             printf("\nPlease enter a valid option.\n");
  35.             printf("a) Too High   b) Too Low   c) Correct\n");
  36.             continue;
  37.         }
  38.  
  39.         //Approximation Algorithm
  40.         if (num_of_guess < 4) {
  41.  
  42.             if (choice == 'a')
  43.                 guess -= calc_guess / (num_of_guess * 2);
  44.  
  45.             if (choice == 'b')
  46.                 guess += calc_guess / (num_of_guess * 2);
  47.  
  48.             ++num_of_guess;
  49.         }
  50.  
  51.         //Fine tune to answer
  52.         else {
  53.  
  54.             if (choice == 'a')
  55.                 --guess;
  56.  
  57.             else
  58.                 ++guess;
  59.         }
  60.  
  61.         //Boundary Enforcer
  62.         if (guess < MIN)
  63.             guess = MIN;
  64.  
  65.         if (guess > MAX)
  66.             guess = MAX;
  67.  
  68.         //Response Variety
  69.         if (diff_resp == 1)
  70.             printf("\nFine, but what about %d?\n", guess);
  71.         else
  72.             printf("\nWell then, is it %d?\n", guess);
  73.  
  74.         printf("a) Too High   b) Too Low   c) Correct\n");
  75.  
  76.         diff_resp = -diff_resp;
  77.     }
  78.  
  79.     printf("\nHurray! I WIN! HAHAHAHA YOU LOSE! LOSER.\n");
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement