Question: Program 4 Loops, Decision Statements, Symbolic Constants, and Random Number Generation Due: Friday, February 14 by 11:59 PM Overview For this assignment, write a program

Program 4 Loops, Decision Statements, Symbolic Constants, and Random Number Generation Due: Friday, February 14 by 11:59 PM

Overview

For this assignment, write a program that will generate three randomly sized sets of random numbers.

Random Number Generation

In the first three programs, the user has been asked for input. This program will be different. Rather than asking the user how many values are in a set of numbers or even what the values are, a random number generator will be used to determine the size of a set and the actual values in the set.

To use the random number generator, first add a #include statement for the cstdlib library to the top of the program:

#include 

Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before actually getting a random number. A value of 1 (or any integer literal) will generate the same sequence of "random" numbers every time the program is executed. This can be useful for debugging:

srand(1);

To get a different series of random numbers each time the program is run, the actual time that the program is run can be passed as the seed value for the random number generator. This is done as follows:

srand(time(0));

If the time function is used, make sure to include the ctime library as well.

Note: the two srand instructions that are listed above are simple examples of how to use the instruction. In a program, ONLY ONE version will be used.

Now that the random number generator has been initialized, a random number can be generated by calling the rand function:

num = rand();

The above line of C++ code will generate a "random" integer between 0 and RAND_MAX and saves the value in an integer variable named num. RAND_MAX is a pre-defined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least 32,767.

Modulus division can be used to restrict the "random" integer to a smaller range:

num = rand() % 7;

will produce a value between 0 and 6. To change the range to 1 through 7, simply add 1:

num = rand() % 7 + 1;

To get random values that are within a specified range that starts at a value other than 0 or 1:

num = minimum_value + (rand() % (maximum_value - minimum_value + 1));

So, to get values within the range 3 - 17:

num = 3 + (rand() % (17 - 3 + 1));

Basic Program Logic

Initialize the random number generator using a seed value of 30. Note: other seed values may be used to produce different results. However, the version that is handed in for grading MUST use a seed value of 30.

Generate a random number between 1 and 500. This will be the number of values in the first set. Display the number of values in the set with an appropriate label.

In a for loop that executes exactly "number of values in the first set" number of times, generate a random number (no restrictions), and display the random number. Make sure that there are only 5 values displayed per line.

Generate a random number between 1 and 300. This will be the number of values in the second set. Display the number of values in the set with an appropriate label.

In a while loop that executes exactly "number of values in the second set" number of times, generate a random number (no restrictions), and display the random number. Make sure that there are only 7 values displayed per line.

Generate a random number between 1 and 100. This will be the number of values in the third set. Display the number of values in the set with an appropriate label.

In a do while loop that executes exactly "number of values in the third set" number of times, generate a random number (no restrictions), and display the random number. Make sure that there are only 8 values displayed per line.

Symbolic Constants

This program MUST use at least 6 symbolic constants.

Three of the constants should be for the maximum values for the sizes of the sets of numbers. The values should be 100, 300, and 500.

The other three constants should be for the maximum number of values to be displayed for each set of numbers. The values should be 5, 7, and 8.

More symbolic constants may be added to the code if necessary.

Program Requirements

  1. Include line documentation. There is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describes what the "chunk" of code does. This will also be a part of every program that is submitted for the remainder of the semester.

  2. To use the random number generator, add #include at the beginning of the program

  3. The program MUST use the 6 symbolic constants described above.

  4. Make sure that the copy of the program that is handed in uses srand(30); to set the seed value for the random number generator.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!