Question

In: Computer Science

1. [100] Create a C program for a number guessing game. Here are the requirements: a....

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.

Solutions

Expert Solution

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


Related Solutions

C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10....
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10. Have the user guess the number. If it is too high, tell the user to guess lower - it it is too low, tell the user to guess higher. Continue until she guesses the correct number. - Part 2. Allow the user to play a new game after they have guessed correctly.   Part 3. Ask for a different name and keep track of how...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT