Question

In: Computer Science

Write a program that generates a random number between 1 and 100 and asks the user...

Write a program 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. Your program should also keep a count of the
number of guesses that the user makes. When the user correctly guesses the random number,
the program should display the number of guesses.
Style guidelines
#include directives must follow header comments, before the rest of the program.
Variable names:
--must be meaningful
--loop index names can be simple (i, j, k, etc)
--The initial letter should be lowercase, following words should be capitalized, no other caps or punctuation (ie: weightInPounds). This is called "camel case".
Named constants:
—use for most numeric literals (including array sizes). —name should be all capitals with underscores:

const double TAX_RATE = 0.0675;
—should occur near the top of the program (not inside functions).
Line length of source code should be no longer than 80 characters (no wrapping of lines).
Indentation:
--Use 2-4 spaces (but be consistent throughout your program). --Indent blocks, within blocks, etc.
--Use blank lines to separate sections.
Comments for variables:
All variable declarations should be commented as follows:
int rank; // numeric value for a card, A=1, J=11, Q=12, K=13

Comments for functions:
Function definitions should be commented to describe what it does, what the parameters are, and what the function returns (when appropriate). See the template and the example below. If the function body contains more than about five statements, there should be comments to describe the various sections of code in the function body.
Template:
//***********************************************************
// function name: short description of what the function does. //
// param-1 description of first parameter (if any)
// param-2 description of second parameter (if any)
// (remaining params, if any)
// returns: description of what function returns (if not void) //***********************************************************
Example:
//***********************************************************
// getBestPlayer: determines which player scored the most points
// p the array of player information
// size the number of players in the array
// returns the name of player who scored the most points
//***********************************************************
string getBestPlayer(Player p[], int size) {
// function body goes here
}
In-code comments:
DO NOT comment every line of code! In general, try to avoid using comments that describe WHAT the code is doing. These are redundant (we can just read the code). Comments that explain WHY the code is doing what it is doing are more helpful. Try to minimize in-code comments, and write readable code instead.
Follow these recognized good programming practices:
The grader may deduct for these issues:

1. Useappropriatedatatypes:

double populationSize; // you cannot have a fractional amount 
 // of people like 2008.55, use int


2. Avoidduplicatecode(don’tcopy,pasteandmodify):

if (monthlySales > 3000) {

cout << “Commission: $" << price * 0.25 << endl;

}

else {

cout << “Commission: $" << price * 0.29 << endl;
 }

better:

double rate;

if (monthlySales > 3000) {
 rate = 0.25;

}

else {

rate = 0.29;
cout << “Commission: $" << price * rate << endl;
3. Donotuseuninitializedvariables:

int total; //should be initialized to 0;
 for (..;..;..)

total = total + x; //on first use, total has garbage in it

4. Useanamedconstantforanarraysize:

const int SIZE = 100; //NOT: int SIZE;
 . . .

double myArray[SIZE];

5. Avoidoutofboundsarrayaccess:
 for example:

for (int i=0; i<=SIZE; i++) { // when i == SIZE it goes 

// beyond the end of the array

. . . myArray[i] . . .
 }

6. Donotuseglobalvariables(butglobalnamedconstantsaregood).

7. Usereferenceparametersonlywhennecessary.

Solutions

Expert Solution

NOTE: Change filename, author name and date in header comment

Program starts from here

/*H**********************************************************************

* FILENAME: randomnum_guess.cpp   AUTHOR: yourname DATE: 19 March 2019

*DESCRIPTION: The program generates a random number and then ask user to guess it.

**********************************************************************H*/
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
    srand((unsigned) time(0)); //This is used so that rand() generate differnt random number each time the program runs
  
    int randomNumber=0; //Stores the random number generated
    int userInput=0;     //Stores the input given by user
    int guess=0;        //Keep track of number of guesses
  
    randomNumber = (rand() % 100) + 1;
    cout<<"Guess what is the number?: ";
    cin>>userInput;
    guess++;
  
    while(userInput!=randomNumber)
    {
        if(userInput>randomNumber)
        cout<<"Too high, try again!\n";
        else
        cout<<"Too low,try again!\n";
        cin>>userInput;
        guess++;
    }
  
    cout<<"Congrats! You guessed the number in "<<guess<<" guesses.";
    return 0;
}

Code snippet:

Outputs

Please give your feedback


Related Solutions

In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number 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. 18. Enhance the program that you wrote for Programming...
C++. Write a program that generates a random number between 5 and 20 and asks the...
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 the user makes until...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
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....
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
Write a program using a Scanner that asks the user for a number n between 1...
Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with 2n - 1 rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT