Answered step by step
Verified Expert Solution
Link Copied!

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 

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 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.

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 6 - 22: num = 6 + (rand() % (22 - 6 + 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 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 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 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

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 the 6 symbolic constants described above. Make sure to follow the programming convention of capitalizing the names of constants.

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.

The numbers in each set 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(12);) on Windows PC

Set 1 -- 30 values 77 5628 6232 29052 1558 26150 12947 29926 11981 22371 4078 28629 4665 2229 24699 27370 3081 18012 24965 2064 26890 21054 5225 11777 29853 2956 22439 3341 31337 14755 Set 2 -- 65 values 24855 4173 32304 292 5344 15512 12952 1868 10888 19581 13463 32652 3409 28353 26151 14598 12455 26295 25763 26040 8285 27502 15148 4945 26170 1833 5196 9794 26804 2831 11993 2839 9979 27428 6684 4616 30265 5752 32051 10443 9240 8095 28084 26285 8838 18784 6547 7905 8373 19377 18502 27928 13669 25828 30502 28754 32357 2843 5401 10227 22871 20993 8558 10009 6581 Set 3 -- 87 values 39.08811 14.20026 75.05417 65.71551 28.70876 20.87466 92.68166 7.11081 0.00916 85.52507 67.95251 58.98312 55.28123 55.23850 47.74316 66.31062 52.77261 25.62334 84.13038 6.10981 11.68859 38.34346 4.33363 90.12421 66.59139 30.37812 25.38835 33.32011 24.34767 75.70421 91.63793 53.89264 26.74642 9.94293 63.23130 20.26124 71.88940 78.69503 33.71685 10.81576 97.50053 0.06714 4.85549 1.96539 79.29014 82.14972 96.79250 50.13276 47.45933 85.93097 21.68950 83.30638 74.52010 74.41633 98.99594 67.82434 37.49199 38.45637 40.39125 65.93829 7.67846 39.96399 82.64412 49.83978 71.09287 63.16111 96.37745 87.76513 32.64565 14.43525 48.99747 67.77551 7.29698 61.47343 49.82147 74.88327 51.20396 52.24464 55.53758 87.09067 36.05152 4.54726 64.19263 6.03656 19.68749 6.63472 75.72253 

Run 2 (using srand(12);) on a Mac

Set 1 -- 30 values 201684 1242219341 144448053 1081905661 844405278 1347567970 1212330528 314341360 325465900 464532391 1292838692 500356098 2086461081 890916504 1377695844 765368154 115518748 198380748 1290611492 1722511344 21113401 518128852 145426979 354845767 330718250 693949314 222433541 1818977807 2130287204 889674844 Set 2 -- 45 values 314341089 320911203 1223151204 1788816544 2016080655 1270586219 165196965 1916518831 824771264 2071176310 1697807947 1442947540 86479209 1757120291 1873100940 1244717207 1323892622 585231387 508818049 425067189 1573635601 1832433202 641844387 668253428 2143374233 1800039253 1657589882 1955277890 1560730836 1837896194 116554110 419840706 1778965347 1803253495 1992264001 416040783 190685249 801378619 1900499196 18221694 1309333184 697892679 2074059686 766584498 1231259533 Set 3 -- 45 values 60.10078 13.81711 24.24546 93.38076 50.43845 19.04124 26.14636 41.89656 55.45210 83.46942 70.49440 99.34945 66.28666 79.83385 67.58620 21.21830 15.92844 9.33630 15.17787 94.49692 9.81277 23.18073 98.47354 44.76477 61.50084 44.65485 13.99324 84.46397 85.98577 62.88687 39.66451 41.33981 98.17614 46.43291 97.95118 65.50062 68.88498 49.81915 10.44754 91.72103 55.30182 57.65196 56.51271 9.18840 29.50578 

Run 3 (using srand(12);) on onlinegdb

Set 1 -- 30 values 1687063760 945274514 247215794 1768547008 1478315323 279012855 668221829 1109214873 1378701697 1054489464 183234778 889447869 614195557 188313374 783912568 1031552104 1373252507 279657667 1668983087 1921919260 1759304062 1292320448 811737180 1921180114 423068838 398561512 2033031419 1667964866 68619930 1463763437 Set 2 -- 34 values 1755683690 261554303 1985455503 1376747050 1739869627 116984710 2044968879 701600852 1495686407 951974696 884835630 237650629 1566170253 1073149004 1021563197 450238709 298917864 1301220864 2119221797 73353476 913041278 1264058597 885090656 686737745 1687127435 1283652168 572285516 1207608654 1352272098 2036048953 798364714 960472141 150119609 636336569 Set 3 -- 44 values 88.00948 35.07926 4.06153 20.68032 4.72758 48.39131 61.88369 15.79405 21.32180 11.85608 63.36429 42.28767 25.77553 23.95711 40.97162 29.19132 66.47391 99.83394 70.40656 98.45263 78.39693 30.18127 25.10175 34.63059 93.15135 19.91267 71.80735 37.87682 26.90316 1.43908 46.71207 14.91264 36.51833 50.77360 35.59296 41.24592 99.16491 97.47664 57.03997 20.48671 9.33273 20.40426 62.77438 35.10826

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

Step: 3

blur-text-image

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

Why is it important to match sources and methods of recruitment?

Answered: 1 week ago