Question: Overview For this assignment, write a program that will generate three randomly sized groups of random numbers. For each group of random numbers, the program
Overview
For this assignment, write a program that will generate three randomly sized groups of random numbers.
For each group of random numbers, the program should also count:
- the number of values in the group that are multiples of 3
- the number of values in the group that are multiples of 7
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 group of numbers or even what the values are, a random number generator will be used to determine the size of a group and the actual values in the group.
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 5. 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 5.
Generate a random number between 2 and 20. This will be the number of values in the first group. Display the number of values in the group with an appropriate label.
In a for loop that executes exactly "number of values in the first group" number of times:
- generate a random number (no restrictions)
- display the random number
- check if the random number is a multiple of 3 and if it is, increment a counter of the number of multiples of 3
- check if the random number is a multiple of 7 and if it is, increment a counter of the number of multiples of 7
When displaying the random number, make sure that there are exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayer.
Next, generate a random number between 2 and 20. This will be the number of values in the second group. Display the number of values in the group with an appropriate label.
In a while loop that executes exactly "number of values in the second group" number of times:
- generate a random number (no restrictions)
- display the random number
- check if the random number is a multiple of 3 and if it is, increment a counter of the number of multiples of 3
- check if the random number is a multiple of 7 and if it is, increment a counter of the number of multiples of 7
As with the previous group, when displaying the random number, make sure that there are exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayer.
Finally, generate a random number between 2 and 20. This will be the number of values in the third group. Display the number of values in the group with an appropriate label.
In a do while loop that executes exactly "number of values in the third group" number of times:
- generate a random number (no restrictions)
- display the random number
- check if the random number is a multiple of 3 and if it is, increment a counter of the number of multiples of 3
- check if the random number is a multiple of 7 and if it is, increment a counter of the number of multiples of 7
As with the previous groups, when displaying the random number, make sure that there are exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayer.
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 3 symbolic constants.
Two of the constants should be for the minimum and maximum values for the size of the groups of numbers. The minimum group size is 2. The maximum group size is 20.
The other constant is left up to you to determine. Some possibilities are for the number of values displayed per line (6), the value 3 for testing for multiples of 3, the value 7 for testing for multiples of 7, or the seed value for the random number generator (5).
More symbolic constants may be added to the code if necessary.
Program Requirements
-
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.
-
To use the random number generator, add #include
at the beginning of the program -
The program MUST use 3 symbolic constants as described above.
-
Make sure that the copy of the program that is handed in uses srand(5); to set the seed value for the random number generator.
-
The numbers in each group MUST be displayed in columns with the LAST digit of the values lined up.
-
Hand in a copy of the source code (the CPP file) using Blackboard.
Output
Run 1 (using srand(5);) on Windows PC
There are 18 values in group 1 28693 12255 24449 27660 31430 23927 17649 27472 32640 5114 28321 13533 17476 426 4307 11963 20107 12150 There are 7 multiples of 3 in the group. There are 3 multiples of 7 in the group. There are 17 values in group 2 12517 10992 31193 28211 25936 23849 4352 19083 26922 3131 17451 7969 4959 16110 7253 14615 1122 There are 7 multiples of 3 in the group. There are 3 multiples of 7 in the group. There are 4 values in group 3 2583 13234 25386 26006 There are 2 multiples of 3 in the group. There are 1 multiples of 7 in the group.
Run 2 (using srand(5);) on a Mac
There are 19 values in group 1 1412376245 1670799424 629750996 1425577356 203572713 505137720 846803449 851438674 1446253957 1970338553 1282223531 332487872 371215210 574039935 1392645221 763960694 82658645 1969410553 717713060 There are 4 multiples of 3 in the group. There are 6 multiples of 7 in the group. There are 4 values in group 2 931714904 2029121251 1400551197 495713212 There are 1 multiples of 3 in the group. There are 0 multiples of 7 in the group. There are 7 values in group 3 987465495 578950449 171791786 1086525734 1184560897 1741588189 670583913 There are 3 multiples of 3 in the group. There are 2 multiples of 7 in the group.
Run 3 (using srand(5);) on CodeChef
There are 3 values in group 1 99788765 2131925610 171864072 There are 2 multiples of 3 in the group. There are 0 multiples of 7 in the group. There are 11 values in group 2 171035632 602511920 963050649 1069979073 1919854381 33661026 589806848 2086105860 475191198 894416410 550050020 There are 5 multiples of 3 in the group. There are 1 multiples of 7 in the group. There are 14 values in group 3 583787226 893281828 550277486 650366414 990569005 968873679 612872373 163967331 1764676460 2072834383 841258873 802348052 2082417967 1025141346 There are 5 multiples of 3 in the group. There are 2 multiples of 7 in the group.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
