Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview For this assignment, write a program that will generate three sets of random numbers. Random Number Generation In the first three programs, the user

Overview
For this assignment, write a program that will generate three sets of random numbers.
Random Number Generation
In the first three programs, the user was 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 it an integer value (known as a seed value). This should only be done ONE time and it must be done BEFORE actually generating 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 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.
To generate a value between 0 and 11: num = rand()%12;
To change the range to 1 through 12, simply add 1: num = rand()%12+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 8-17: num =8+(rand()%(17-8+1));
To convert a random integer value to a random double value: double_num = minimum_value +(rand()/(RAND_MAX /(maximum_value - minimum_value)));
where minimum_value and maximum_value are both double values. So, to get values within the range 0.5-230.5: double_num =0.5+(rand()/(RAND_MAX /(230.5-0.5)));
Basic Program Logic
Initialize the random number generator using a seed value of 33. 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 33.
The first set of numbers should have exactly 45 values. Display the number of values in the first set with a label.
In a for loop that executes exactly forty-five times:
generate a random number (no restrictions)
display the random number *
* when displaying the random numbers, make sure that there are exactly 7 values displayed per line. The exception is the last line, which may have less than 7 values displayed.
Next, generate a random number between 1 and 75. This will be the number of values in the second set. Display the number of values in the set with a label.
In a while loop that executes exactly "number of values in the second set" number of times:
generate a random number (no restrictions)
display the random number **
** again, make sure that there are exactly 7 values displayed per line.
Finally, generate a random number between 1 and 50. This will be the number of values in the third set. Display the number of values in the set with a label.
In a do while loop that executes exactly "number of values in the third set" number of times:
generate a random double number between 0.0 and 300.0
display the random number with 1 digit after the decimal point ***
*** again, make sure that there are exactly 7 values displayed per line.
Note: when writing this program, it is important that the steps that involve the random number generator are executed in the sequence that they're listed above. This is because the random number generator simply generates a sequence of values and if those values are not processed in the same order as above, the results will not match the expected results that are listed in the Output section below.
Symbolic Constants
This program MUST use at least 6 symbolic constants.
The first constant represents the size of the first set of values (the one generated by the for loop). It should have an integer value of 45.
The second constant represents the maximum size of the second set of values (the one generated by the while loop). It should have an integer value of 75.
The

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago