Need working c code for a program that plays a guessing game with user. [ Must i
ID: 3534482 • Letter: N
Question
Need working c code for a program that plays a guessing game with user.
[ Must include : <stdio.h> along with rand() and srand() ]
Sample of game as follows:
Welcome to the game of guess it!
I will choose a number between 1 and 100.
You will try to guess that number. If you guess wrong, I will tell you if guessed too high or too low.
You have 6 tries to get the number.
ok, i am thinking of a number. Try to guess it.
Your guess? 45
Too high!
Your guess? 11
Too low!
Your guess? 110
Ilegal guess. Your guess must be between 1 and 100.
Try again. Your guess? -20
Ilegal guess. Your guess must be between 1 and 100.
Try again. Your guess? 22
*****correct******
want to play again? y
Ok I am thinking of a number . Try to guess it .
........
Want to play again? n
Goodbye, it was fun.
Hope to play Guess it with you again soon.
Explanation / Answer
using rand() and srand().
I dont think anybody has used both of them. I have used both rand and srand()
check the code..
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int stime;
long ltime;
int thisRandomNumber;
int userinput;
/* get the current time and then send the random number */
ltime= time(NULL);
stime= (unsigned) ltime/2;
srand(stime);
//calculate a random number between 1 and 100 */
thisRandomNumber= rand() 0 + 1;
printf("Guess the random computer generated number: ");
scanf("%d",&userinput);
if (userinput == thisRandomNumber){
printf("That's the number! ");
}
else if (userinput > thisRandomNumber){
printf("Your guess is higher than the number. ");
}
else if(userinput < thisRandomNumber){
printf("Your guess is lower than the number. ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.