Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main()
- {
- int num;
- int guess;
- int height=100;
- int bottom=1;
- char answ='a';
- printf("Instruction: You have to insert a number from 1 to 100 and I have to guess it.\n"
- "But you have to help me saying me if my guess is higher typing 'h'\n"
- "or 'l' otherwise.\n\n");
- printf("Please insert a number:\n");
- num *scanf("%d", &num);
- srand(time(NULL)); // initialize random number generator
- guess = rand()%100 +1;
- while(guess != num)
- {
- printf("The secret number is Higher or Lower than my guess %i?\n",guess);
- answ=getchar();
- if (answ=='l')
- {
- height=guess;
- srand(time(NULL)); // initialize random number generator
- guess = rand()%(height-1-bottom) +(bottom+1);
- }
- if (answ=='h')
- {
- bottom=guess;
- srand(time(NULL)); // initialize random number generator
- guess = rand()%(bottom-height-1) +(bottom+1);
- }
- }
- printf("I found it! It's %i",guess);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement