Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program 4 Loops, Decision Statements, Symbolic Constants, and Random Number Generation (100 points) Due: Friday, February 17 on Blackboard by 11:59 PM Overview For this
Program 4 Loops, Decision Statements, Symbolic Constants, and Random Number Generation (100 points) Due: Friday, February 17 on Blackboard by 11:59 PM 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 (1) i 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))i 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 7: num =rand()&8; To change the range to 1 through 8 , simply add 1 : num = rand ()&8+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 622 : num =6+( rand () s (226+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.5230.5 : double_num =0.5+ (rand ()/( RNND_MNX /(230.50.5))); Basic Program Logic Initialize the random number generator using a seed value of 12 . 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 12 . The first set of numbers should have exactly 30 values. Display the number of values in the first set with a label. In a for loop that executes exactly thirty 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 whille 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 155 . 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 100.0 - display the random number with 5 digits 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 30 . 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 third constant represents the maximum size of the third set of values (the one generated by the do while loop). It should have an integer value of 155 . The fourth constant represents the minimum random double value. It should have a value of 0 (make sure to use 0.0 if using \#define to create the constant). The fifth constant represents the maximum random double value. It should have a value of 100 (make sure to use 100.0 if using \#define to create the constant). The sixth constant represents the number of values to display on a line of output. It should have an integer value of 7 . 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 cstdlib > at the beginning of the program 3. The program MUST use the 6 symbolic constants described above. Make sure to follow the programming convention of capitalizing the names of constants. 4. Make sure that the copy of the program that is handed in uses srand(12); to set the seed value for the random number generator. 5. The numbers in each set MUST be displayed in columns with the LAST digit of the values lined up. 6. Hand in a copy of the source code (the CPP file) using Blackboard. Run 1 (using srand(12);) on Windows PC Run 3 (using srand(12);) on onlinegdb
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started