Question

In: Computer Science

// FILE: guess.cxx // Demostrates a guessing game function that's used as a time analysis example....

// FILE: guess.cxx
// Demostrates a guessing game function that's used as a time analysis example.

#include <cassert> // Provides assert
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
using namespace std; // Allows all Standard Library items to be used

// Prototype for the function used in this demonstration program
void guess_game(int n);
// Precondition: n > 0.
// Postcondition: The user has been asked to think of a number between 1 and n.
// The function asks a series of questions, until the number is found.


int main( )
{
guess_game(100);
return EXIT_SUCCESS;
}

void guess_game(int n)
// Library facilities used: cassert, iostream
{
int guess;
char answer;

assert(n >= 1);

cout << "Think of a whole number from 1 to " << n << "." << endl;
answer = 'N';
for (guess = n; (guess > 0) && (answer != 'Y') && (answer != 'y'); --guess)
{
cout << "Is your number " << guess << "?" << endl;
cout << "Please answer Y or N, and press return: ";
cin >> answer;
}

if ((answer == 'Y') || (answer == 'y'))
cout << "I knew it all along." << endl;
else
cout << "I think you are cheating!" << endl;
}

1) The source code must be well structured and include relevant comments like on the top lines version, date, and a brief description of what the program does.

2) . As you make changes you can add one line description of the changes and change the version # or add a version #
like version 1.0 and 1.1

3) Write a short report (not to exceed 100 words) that describes the work done, the input, and output when executing the program.

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Part 1 and 2:

// Demostrates a guessing game function that's used as a time analysis example.

#include <cassert> // Provides assert
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
using namespace std; // Allows all Standard Library items to be used

// Prototype for the function used in this demonstration program
void guess_game(int n);
// Precondition: n > 0.
// Postcondition: The user has been asked to think of a number between 1 and n.
// The function asks a series of questions, until the number is found.

/*-------------------------------------------------------
<v0>   <date:01 Sept 2019>
<description>: This is the main entry point of the program.
<input>:None
<output>:None

<v1>   <date:02 Sept 2019>
<changes>: Added functionality to call guess_game function by passing an integer value.
---------------------------------------------------------*/
int main( )
{
   //Calling function guess_game with value 100.
   guess_game(-100);
   //Returns successful execution of a program
   return EXIT_SUCCESS;
}//end of main

/*-------------------------------------------------------
<v0>   <date:01 Sept 2019>
<description>: This function in the main game. It asks user repeatedly for a number between 1 to INPUT value untill user enters correct value.
It starts with INPUT and then decrement one by one and ask user whether number is correct.
<input>:An Integer greater than 0
<output>: Print message is guess is found

<v1>   <date:02 Sept 2019>
<changes>: Added functionality to take user input.

<v2>   <date:02 Sept 2019>
<changes>: Added functionality to guess the number.
---------------------------------------------------------*/
void guess_game(int n)
{
   //This variable will be used for knowing whether guess is correct.
   int guess;
   //This variable stores user input whether guess is correct or not
   char answer;

   //We are checking whether precondition is satisfied.
   assert(n >= 1);
   //Asking user to think about a number between 1 to n
   cout << "Think of a whole number from 1 to " << n << "." << endl;
   //Initialising answer with N i.e. guess is not correct
   answer = 'N';
   //Looping from n untill guess is found and guess is between range i.e. 1 to n.
   //decrementing guess by 1 after every round.
   for (guess = n; (guess > 0) && (answer != 'Y') && (answer != 'y'); --guess)
   {
       // Asking user whether the calculated guess is user guess.
       cout << "Is your number " << guess << "?" << endl;
       //User have to reply with Y or N, Y for guess is correct and N for guess is incorrect
       cout << "Please answer Y or N, and press return: ";
       //Taking user input
       cin >> answer;
   }//end of for loop
   //Loop termination will occur in two cases
   // 1: Either the user guess is found. If guess is found then print message of success.
   // or
   // 2: guess goes below 1. Then print that user is cheating.
   if ((answer == 'Y') || (answer == 'y'))
       cout << "I knew it all along." << endl;//printing success message
   else
       cout << "I think you are cheating!" << endl;//printing cheating message
}//end of function guess_game

-------------------------------------------------------------------

part 3:

This is a simple game program. It starts with main calling a function providing the maximum number for a range. In this function, the user has to think about a number in their mind and it will ask one by one decrementing from the input of this function. So function will show a number and ask the user whether this is the number they thought about. If not then the program will decrement the shown number by 1 and ask again until the shown number is correct or it goes below the range. If it goes below the range then print that user is cheating else print success message.


Related Solutions

guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Which of the following is a post-translational modification that's oftentimes used to regulate protein function within...
Which of the following is a post-translational modification that's oftentimes used to regulate protein function within intracellular signaling cascades, allosteric interaction or ubiquitination?
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
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...
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...
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!”.
We are going to develop a guessing game where the user has to guess a secret...
We are going to develop a guessing game where the user has to guess a secret number. We will start from a simple task and refine our code step by step. Task is to develop a program that Generates a random integer number in the range of [1-10] as the secret number Asks for user to input an integer in the range of [1-10] Print out the secret number and a user's input. Steps Please download this sample code, using...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT