Write a program in c++ to generate a random number between 1 - 100, and then display which quartile the number falls in.First quartile is 1 - 25Second quartile is 26 - 50Third quartile is 51 - 75Fourth quartile is 76 - 100The following program shows how to get a random integer between 1 - 100. Use the code as an example for your own program.SAMPLE PROGRAM// This program displays a random number between 1 - 100#include <iostream> // for input and output#include <cstdlib>  // for rand() and srand()#include <ctime>    // for time()using namespace std;int main(){    //constantsconst int MIN_VALUE = 1;const int MAX_VALUE = 100;//get system time and seed random number generatorunsigned seed = time(0);srand(seed);//get random number between 1 - 100int num = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;cout << num << endl;return 0;}Your program should:contain header comments as shown in classdisplay a "hello" messagegenerate a random number between 1 - 100display the number back to the userdetermine which quartile the number falls in (using any combination of if, else, and else if)display the quartile back to the userdisplay a "goodbye" message before exiting the program

Question
Asked Jul 17, 2019

Write a program in c++ to generate a random number between 1 - 100, and then display which quartile the number falls in.

  • First quartile is 1 - 25
  • Second quartile is 26 - 50
  • Third quartile is 51 - 75
  • Fourth quartile is 76 - 100

The following program shows how to get a random integer between 1 - 100. Use the code as an example for your own program.


SAMPLE PROGRAM
// This program displays a random number between 1 - 100
#include <iostream> // for input and output
#include <cstdlib>  // for rand() and srand()
#include <ctime>    // for time()
using namespace std;

int main()
{
    //constants
const int MIN_VALUE = 1;
const int MAX_VALUE = 100;

//get system time and seed random number generator
unsigned seed = time(0);
srand(seed);

//get random number between 1 - 100
int num = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
cout << num << endl;

return 0;
}

Your program should:

  • contain header comments as shown in class
  • display a "hello" message
  • generate a random number between 1 - 100
  • display the number back to the user
  • determine which quartile the number falls in (using any combination of if, else, and else if)
  • display the quartile back to the user
  • display a "goodbye" message before exiting the program

 

check_circle

Expert Answer

Step 1

Program plan:

  • Include the required header files into the program.
  • Define the main function.
    • Declare the required constant variables
    • Display the hello message to the user.
    • Get the system time and seed random number generator.
    • Get the random number between 1 to 100.
    • Display the random number to the user.
    • Check the “num” value is less than or equal to 25.
      • Display the “num” is belongs to first quartile.
    • Check the “num” value is greater than 25 and less than or equal to 50.
      • Display the “num” is belongs to second quartile.
    • Check the “num” value is greater than 50 and less than or equal to 75.
      • Display the “num” is belongs to third quartile.
    • Check the “num” value is greater than 75 and less than or equal to 100.
      • Display the “num” is belongs to fourth quartile.
    • Finally display the goodbye message to the user
Step 2

Program:

// include the required header files

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

 

//definition of main function

int main()

{

//declare the constant variables

const int MIN_VALUE = 1;

const int MAX_VALUE = 100;

 

//display the hello message

cout << "HELLO! WELCOME TO THE PROGRAM\n\n";

 

//get system time and seed random number generator

unsigned seed = time(0);

srand(seed);

 

//get random number between 1 - 100

int num = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

 

//display the random number

cout << "The random number is: " << num << endl;

 

//check the condition

if (num <= 25)

{

  //display the statement

  cout << "The number " << num << " belongs to first quartile 1 - 25\n";

}

 

//check the condition

else if (num > 25 && num <= 50)

{

  //display the statement

  cout << "The number " << num << " belongs to second quartile 26 - 50\n";

}

 

//check the condition

else if (num > 50 && num <= 75)

{

  //display the statement

  cout << "The number " << num << " belongs to third quartile 51 - 75\n";

}

 

//check the condition

else if (num > 75 && num <= 100)

{

  //display the statement

  cout << "The number " << num << " belongs to fourth quartile 76 - 100\n";

}

 

//display the goodbye message

cout << "\nGOODBYE!";

 

//return statement

return 0;

}

 

Step 3

Program Screenshot #1:...

/ include the required header files
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std
1/definition of main function
int main
{
1/declare the constant variables
const int MIN VALUE 1;
const int MAX_VALUE 100;
/display the hello message
cout << "HELLO! WELCOME TO THE PROGRAM\n\n";
get system time and seed random number generator
unsigned seed time(o);
srand(seed);
/get random number between 1 180
int num (rand) (MAX_VALUE - MIN_VALUE + 1)) MIN VALUE;
/display the random number
"The random number is: " <num < endl;
cout
1/check the condition
if (num <25)
display the statement
cout << "The number " <num <<* belongs to first quartile 1
25\n";
help_outline

Image Transcriptionclose

/ include the required header files #include <iostream> #include <cstdlib> #include <ctime> using namespace std 1/definition of main function int main { 1/declare the constant variables const int MIN VALUE 1; const int MAX_VALUE 100; /display the hello message cout << "HELLO! WELCOME TO THE PROGRAM\n\n"; get system time and seed random number generator unsigned seed time(o); srand(seed); /get random number between 1 180 int num (rand) (MAX_VALUE - MIN_VALUE + 1)) MIN VALUE; /display the random number "The random number is: " <num < endl; cout 1/check the condition if (num <25) display the statement cout << "The number " <num <<* belongs to first quartile 1 25\n";

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: (Latest Revision: Apr 01, 2019) MST Problems First MST Problem:Suppose G=(V,E) is a connected undire...

A: Given (Table):According to the kruskal’s algorithm:Arrange every edge in increasing order of their w...

question_answer

Q: Visual Basic. Write an expression that evaluates to true if the value of the integer variable number...

A: In visual basic we use the Mod operator to check the remainder of a division.

question_answer

Q: One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of ...

A: The algorithm for the program is as follows:Accept input string as the square feetCalculate the numb...

question_answer

Q: Programming in C

A: We use the bsearch function from the stdlib.h and define a compare function to return the result of ...

question_answer

Q: How become a ggod programmer?

A: Good programmer:Becoming programmer is not a big thing; it is all about learning the logics of the p...

question_answer

Q: Write a recursive function that computes the length of a list. For example, (mylen ‘(1 2 3 4))should...

A: The syntax for given function call (mylen’(1 2 3 4)) is of Lisp programming.For lisp program:Create ...

question_answer

Q: what strategies that would attract buyers to a company's e-commerce web site

A: Strategies to attract buyers to the company e-commerce website:Offer limited time specials to new cu...

question_answer

Q: example:"apple-&gt;orange-&gt;banana-&gt;pear"-&gt;NULLreturn "orange"Thanks!

A: ApproachIn the function longest(), we'll declare an integer named highestLength and assign 0 to it.N...

question_answer

Q: Analyze the following code: Code 1: int number = 45;boolean even; if (number % 2 == 0) even = true;e...

A: First lets discuss about the first question. 1) In the first code we are declaring two variables num...