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

50.1K

Verified Solution

Question

Programming

Write a program that generates a random number between 1 and100 and asks the user to
guess what the number is. If the user’s guess is higher thanthe random number, the program
should display “Too high, try again.” If the user’s guess islower than the random number, the
program should display “Too low, try again.” The programshould use a loop that repeats until
the user correctly guesses the random number. Your programshould also keep a count of the
number of guesses that the user makes. When the user correctlyguesses the random number,
the program should display the number of guesses.
Style guidelines
#include directives must follow header comments, before therest 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 wordsshould be capitalized, no other caps or punctuation (ie:weightInPounds). This is called \"camel case\".
Named constants:
—use for most numeric literals (including array sizes). —nameshould be all capitals with underscores:

const double TAX_RATE = 0.0675;
—should occur near the top of the program (not insidefunctions).
Line length of source code should be no longer than 80characters (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 asfollows:
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 itdoes, what the parameters are, and what the function returns (whenappropriate). See the template and the example below. If thefunction body contains more than about five statements, thereshould be comments to describe the various sections of code in thefunction 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 mostpoints
// 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 avoidusing comments that describe WHAT the code is doing. These areredundant (we can just read the code). Comments that explain WHYthe code is doing what it is doing are more helpful. Try tominimize 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 <
}

else {

cout << “Commission: $\" << price * 0.29 <
better:

double rate;

if (monthlySales > 3000) {
 rate = 0.25;

}

else {

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

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

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

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.

Answer & Explanation Solved by verified expert
3.8 Ratings (395 Votes)
NOTE Change filename author name and date in headercommentProgram starts from hereH FILENAME randomnumguesscpp AUTHOR    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students