Question
This needs to be answered in C++ Language. The starter code is below. -----------------------------------------------------------------------------STARTER CODE---------------------------------------------------------------------------------------------------- #include #include #include using namespace std; /** * Returns the
This needs to be answered in C++ Language. The starter code is below.
-----------------------------------------------------------------------------STARTER CODE----------------------------------------------------------------------------------------------------
#include
#include
#include
using namespace std;
/**
* Returns the middle (rounded down) of a range of numbers.
*/
int midRange(int min, int max);
int main()
{
int min, max; // the min and max value the answer can be
// Seed the random number generator using the current time.
srand(static_cast
// Get the range of random numbers from the user
getNumberRange(min, max);
// Give the function creating the random numbers the range or values.
// min-1 is used as the first parameter, because it will never the a
// Correct guess.
checkGuess(min - 1, min, max);
// Guess 1000 numbers and write the number of guesses to a file
generateRunStats(min, max);
// Display message to user when finished.
cout
return 0;
}
int midRange(int min, int max)
{
// The middle value is always the average of the min and max.
return (max + min) / 2;
}
DESCRIPTION This problem explores static variables, random numbers, and provides more practice with default parameters. I am asking you to do things here in a specific way in order for you to get good practice with multiple functions that have static variables. Pay attention to the details in this program specification. Program Overview Using the given starter code, create a program in which the computer tries to guess what a set of random numbers are. The user will enter the range from which random numbers will be chosen. A function will generate and "remember" a random number based on the min and max possible numbers. This function will also accept a guess as a parameter and let the calling function know if this guess is too larger, too small or the correct answer. The program will guess until getting the right answer. After the number is correctly guessed, the number of guesses the computer took will be written to a file and a new random number will be guessed. The program will continue until correctly guessing 1000 random numbers. C Detailed Instructions Use the attached file named GuessANumber.cpp as your starting point. Do NOT modify main() or midRange(). Add the following functions to complete the program: getNumberRange: This function prompts the user to enter the min and max values for the range of random numbers. The function outputs an error message and prompts for new values while the min is greater than or equal to the max. The min and max should be stored in reference parameters, so main() can receive their updated values. Hint. A recursive call to getNumberRange() for invalid ranges is easier than using a loop to implement. checkGuess: This function needs 3 parameters: a guess, the min, and the max. The min and max parameters should have the default values of 0 and 0, so that you only need to pass 1 parameter to this function after the first time it is called. The function will generate a new random number whenever the current number is correctly guessed. It will return any positive number if the guess is too big, any negative number if the guess is too small, and zero for correct guesses. Review the Guessing Game video for a refresher on using pseudo-random numbers. This function is going to need 2 static const variables to hold onto the range and min value based on the first time the function is called. The first static const variable should hold the range of possible values (max - min + 1). The second static const variable should store the min value (give it a unique but meaningful name). Additionally, you will need a non-constant static variable to hold the current random number. The random number should not change between function calls until it is correctly guessed. . guessUntilFound: This function needs two pass-by-value parameters to receive the min and max values the user provided. This function will try to guess the current random number until getting the right answer. Here is the strategy it should employ. Always guess the very middle out of the possible values. Use the provided midRange() function to find the middle value. If the guess is too small, update the min to be one more than the current guess. Likewise, if the guess is too larger, update the max to be one less than the current guess. If the guess is correct, return the number of tries it took to guess the number. This function should NOT assume that checkGuess() will return specific negitive or positive values. generateRunStats: This function also needs to know the min and max values given by the user. The function should call guessUntil Found 1000 times. The function should also save the number of tries it took to correctly guess each random number to a file named runlog.txt. Each count should be on its own line of the text file. Validating the Program's Output After running the program, look at the outputted numbers and verify that the numbers are reasonable. The number of guesses should always be less than or equal to log (range) if you round up. For example, if you guess a number between 1 and 10 inclusive, the program will take between 1 to 4 guesses (log2(10) ~ 3.3, which rounds up to 4). Likewise, if there is a range of 100, then the answer should be found in at most 7 guesses (log2(100) 6.6, which rounds up to 7). The reason for this relationship is because the range of possible answers is halved after every incorrect guess. Sample Output (user input is in yellow) Enter the range of possible random answers. Min value: 100 Max value: 1 Input error: The minimum must be less than the max Enter the range of possible random answers. Min value: 1 Min value: 100 Finished! The number of guesses taken by the computer for each random number is stored in 'runlog.txt'. DESCRIPTION This problem explores static variables, random numbers, and provides more practice with default parameters. I am asking you to do things here in a specific way in order for you to get good practice with multiple functions that have static variables. Pay attention to the details in this program specification. Program Overview Using the given starter code, create a program in which the computer tries to guess what a set of random numbers are. The user will enter the range from which random numbers will be chosen. A function will generate and "remember" a random number based on the min and max possible numbers. This function will also accept a guess as a parameter and let the calling function know if this guess is too larger, too small or the correct answer. The program will guess until getting the right answer. After the number is correctly guessed, the number of guesses the computer took will be written to a file and a new random number will be guessed. The program will continue until correctly guessing 1000 random numbers. C Detailed Instructions Use the attached file named GuessANumber.cpp as your starting point. Do NOT modify main() or midRange(). Add the following functions to complete the program: getNumberRange: This function prompts the user to enter the min and max values for the range of random numbers. The function outputs an error message and prompts for new values while the min is greater than or equal to the max. The min and max should be stored in reference parameters, so main() can receive their updated values. Hint. A recursive call to getNumberRange() for invalid ranges is easier than using a loop to implement. checkGuess: This function needs 3 parameters: a guess, the min, and the max. The min and max parameters should have the default values of 0 and 0, so that you only need to pass 1 parameter to this function after the first time it is called. The function will generate a new random number whenever the current number is correctly guessed. It will return any positive number if the guess is too big, any negative number if the guess is too small, and zero for correct guesses. Review the Guessing Game video for a refresher on using pseudo-random numbers. This function is going to need 2 static const variables to hold onto the range and min value based on the first time the function is called. The first static const variable should hold the range of possible values (max - min + 1). The second static const variable should store the min value (give it a unique but meaningful name). Additionally, you will need a non-constant static variable to hold the current random number. The random number should not change between function calls until it is correctly guessed. . guessUntilFound: This function needs two pass-by-value parameters to receive the min and max values the user provided. This function will try to guess the current random number until getting the right answer. Here is the strategy it should employ. Always guess the very middle out of the possible values. Use the provided midRange() function to find the middle value. If the guess is too small, update the min to be one more than the current guess. Likewise, if the guess is too larger, update the max to be one less than the current guess. If the guess is correct, return the number of tries it took to guess the number. This function should NOT assume that checkGuess() will return specific negitive or positive values. generateRunStats: This function also needs to know the min and max values given by the user. The function should call guessUntil Found 1000 times. The function should also save the number of tries it took to correctly guess each random number to a file named runlog.txt. Each count should be on its own line of the text file. Validating the Program's Output After running the program, look at the outputted numbers and verify that the numbers are reasonable. The number of guesses should always be less than or equal to log (range) if you round up. For example, if you guess a number between 1 and 10 inclusive, the program will take between 1 to 4 guesses (log2(10) ~ 3.3, which rounds up to 4). Likewise, if there is a range of 100, then the answer should be found in at most 7 guesses (log2(100) 6.6, which rounds up to 7). The reason for this relationship is because the range of possible answers is halved after every incorrect guess. Sample Output (user input is in yellow) Enter the range of possible random answers. Min value: 100 Max value: 1 Input error: The minimum must be less than the max Enter the range of possible random answers. Min value: 1 Min value: 100 Finished! The number of guesses taken by the computer for each random number is stored in 'runlog.txtStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started