In: Computer Science
1. [100] Create a C program for a number guessing game.
Here are the requirements:
a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max.
b. If the user guesses a number that is lower or higher than the number to be guessed then indicate that information to the user as a ‘low guess’ or a ‘high guess’ so that the user can get to the correct guess quicker.
c. The program keeps track on number of guesses taken by the user to get to the correct guess. This is user’s score.
d. Use a parameter for number of allowed guesses (set it some value, say 7) and your program congratulates users if user guesses the number correctly in number of guesses less than the number of allowed guesses. The program doesn't allow user to continue beyond the number of allowed guesses and tells user that the number wasn't guessed correctly in allowed attempts. You can print the number of allowed guesses at the beginning of the game.
e. Write out user name and number of guesses taken to a game report file. Each attempt at the game will add a new line to the report file.
Code for your program is provided below. It is explained thoroughly in the code comments. The program fullfills all your conditions. For Your better understanding i have provided the screenshot of code in IDE . I have also provided the output screenshot in the last.
##################################################################################
CODE-->>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
//declaration of function that generates random number
int generateRandomNumber(int,int);
int main()
{
//variables to store values
int num;
int userInput;
bool loop=true;
int track=0;
char name[30];
//seeding the random function with current time
srand(time(0));
//calling function to generate random number
num= generateRandomNumber(-100,100);
printf("---Guess The number between -100 and 100---");
printf("\n---Maximum allowed attempts are 7---");
printf("\n\nWhat is your Name: ");
fgets(name,sizeof(name),stdin); //input the name of user
int length=strlen(name); //finding the length of name
//fgets() takes the input and the last character is automatically "\n"
if(name[length-1]=='\n')
name[--length]=0; //removing the last character from name
while(loop) //loop to take the user guessed number
{
track++; //increasing the track
printf("\nWhat is your guess: ");
scanf("%d",&userInput); //take the guessed number
if(userInput==num) //if rightly guessed
{
printf("\nCongratulations!!! You guessed the Number %d",userInput);
printf("\nYour Score is: %d",track);
break; //break out of loop
}
else if(userInput<num) //if the guessed number is less than the actual number
{
printf("It is a low guess");
}
else //if the guessed number is higher than the actual number
{
printf("It is a high guess");
}
//if user have taken 7 guesses and all are false then we have to break out of loop
if(track==7)
{
loop=false; //set loop variable false
printf("\nSorry!!! You could not guess the number in Maximum allowed guesses");
track=0; //set tract to zero as the user could not guessed the number
}
}
FILE *inputFile; //creating pointer for file
inputFile=fopen("result.txt","a+"); //open the file in "a+" mode, it is a mode for append
fprintf(inputFile,"%s",name); //write name to file
fputc(' ',inputFile); //write space
fprintf(inputFile,"%d",track); //write the score
fprintf(inputFile,"\n"); //Write a new Line
fclose(inputFile); //close the file
return 0;
}
// definition of function that generates random number
int generateRandomNumber(int min,int max)
{
int num=(rand()%(max-min+1))+min; //finding random number in range min to max
return num; //return random number
}
##########################################################################
OUTPUT ON CONSOLE
OUTPUT IN FILE
##########################################################
CODE IN IDE