In: Computer Science
// 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.
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.