I need help with this C code: Random Numbers and Loops Description: For this ass
ID: 3665704 • Letter: I
Question
I need help with this C code:
Random Numbers and Loops Description: For this assignment, you will write a simple guessing game. The computer will randomly pick a number between 1 and 200 (inclusive), and the user will try to guess it within eight tries. Your program will use a random number generator (RNG) to pick the number between 1 and 200. Also, use the man page for random() for further information. Specifications: Your program will begin with a greeting message, and then ask the user to enter a random number seed. The program will use this number as a parameter to the random() call. The program will generate a random integer within the proper range. The program will then enter a loop. Within the loop, the program will prompt the user to enter a guess for the chosen number. The program will keep track of the number of guesses made and report to the user the number of guesses they have made so far. If the guess is too low or high, the program will state so. If the guess is correct, the computer will state this fact, and exit the loop and the program If the number of user guesses is equal to eight or more, the program will print a message stating that the user has lost. The program will then exit the loop, and then the program. Additional Specifications: The program will contain #define statements to give the upper and lower range for the possible values. The program will contain a #define statement to give the upper limit of the number of allowed guesses. You do not need to check the input of the user. For instance, if the user enters a value outside of the range of possible numbers, your program will properly check the value and treat it as a normal guess reporting if it is too low or high. Testing your completed program: After compiling, run your program several times using the same random number seed, to ensure that its behavior is consistent. Check to see that it exits if too many guesses are made, as well as stating the user found the correct value within the required number of guesses. Then try using different seeds to ensure it is working properlyExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN_RANGE 1
#define MAX_RANGE 200
#define NO_OF_GUESS 8
//Function creating a random number.
int getRandomNumber(){
int rangedNum;
srand(time(NULL));
rangedNum = abs(rand() % MAX_RANGE); // dividing to get random number in the range
//printf(" Random number is - %d ", rangedNum);
return rangedNum;
}
int main()
{
int rangedNum, count, guess;
printf("************************************************* ");
printf("************Welcome to the Guessing Game.********* ");
printf("************************************************* ");
rangedNum = getRandomNumber();
for(count = 1; ; count++){
printf("******************************************************* ");
if(count > 1){
printf("You have made %d guesses. Please play carefully. ", count-1);
}
if(count > (NO_OF_GUESS*0.75)){
printf("Ohhh! You have only %d gueses left to win this contest. Best of Luck! ", NO_OF_GUESS-count+1);
}else {
printf("You have %d gueses left to win this contest. Best of Luck! ", NO_OF_GUESS-count+1);
}
printf("Please enter your lucky number - ");
scanf("%d", &guess);
if(rangedNum == guess){
printf("******* Conratulation!!! you are the lucky winner. ");
break;
}
if(count >= NO_OF_GUESS){
printf("Ohhh! sorry. you are lost. Try again some other time. Thank you! ");
break;
}
}
return 0;
}