Advertisement
Guest User

Number Guessing Name

a guest
Apr 15th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. int num;
  10. int guess;
  11. int height=100;
  12. int bottom=1;
  13. char answ='a';
  14.  
  15. printf("Instruction: You have to insert a number from 1 to 100 and I have to guess it.\n"
  16. "But you have to help me saying me if my guess is higher typing 'h'\n"
  17. "or 'l' otherwise.\n\n");
  18.  
  19. printf("Please insert a number:\n");
  20. num *scanf("%d", &num);
  21.  
  22. srand(time(NULL)); // initialize random number generator
  23. guess = rand()%100 +1;
  24.  
  25. while(guess != num)
  26. {
  27. printf("The secret number is Higher or Lower than my guess %i?\n",guess);
  28.  
  29. answ=getchar();
  30.  
  31. if (answ=='l')
  32. {
  33. height=guess;
  34. srand(time(NULL)); // initialize random number generator
  35. guess = rand()%(height-1-bottom) +(bottom+1);
  36. }
  37.  
  38. if (answ=='h')
  39. {
  40. bottom=guess;
  41. srand(time(NULL)); // initialize random number generator
  42. guess = rand()%(bottom-height-1) +(bottom+1);
  43. }
  44.  
  45. }
  46.  
  47. printf("I found it! It's %i",guess);
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement