CSCI 240 Fall 2025

Assignment 4
Loops, Decision Statements, Symbolic Constants, and Random Number Generation
(100 points)


Due: Friday, September 26 on the autograder and Blackboard by 11:59 PM

Overview

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

For each set of numbers, find and display the smallest and largest numbers.

The cpp file that is submitted for grading must be named assign4.cpp.

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

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);

An integer variable can also be placed between the parenthesis and the value that the variable holds will be the seed.

srand(seed_value);

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 three 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 8:    num = rand() % 9;

To change the range to 1 through 9, simply add 1:    num = rand() % 9 + 1;

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

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

So, to get values within the range 4 - 19:       num = 4 + (rand() % (19 - 4 + 1));

Basic Program Logic

Ask the user to enter the seed value for the random number generator and use it with the srand() instruction.

Generate a random number between 1 and 50. 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:

When displaying the random numbers, make sure that there are exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayed.

Next, generate a random number between 1 and 30. 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:

As with the first set of numbers, there should be exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayed.

Finally, generate a random number between 5 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:

As with the other sets of numbers, there should be exactly 6 values displayed per line. The exception is the last line, which may have less than 6 values displayed.

Finally, display a table with the smallest and largest values for each set. It should resemble the following:

                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value     20004997     20951825    365145415
Largest value    2138131970   2106493143   1911556661

The "Set" labels are displayed in fields that are 27, 13, and 13 characters. There are exactly 53 dashes. With the exception of the largest value in Set 1, all the values are displayed in fields of 13 characters. The largest value in Set 1 is displayed in a field of 14 characters.

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 FIVE symbolic constants.

Three of the constants represent the maximum values for the sizes of the sets of numbers. The values are 30, 50, and 100.

Another constant represents the minimum value for the size of the third set. The value is 5.

The final required constant represents the maximum number of values to display per line. The value is 6.

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 be the final reminder in assignment write-ups.

  2. Add a #include for the cstdlib library at the beginning of the program

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

  4. The numbers in each group MUST be displayed in columns with the LAST digit of the values lined up.

  5. Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.

Output

Windows PC output 1

Seed Value? 30

First set of numbers: 37 values

         2491          107         3440        23380        31295         3435
        10003        14278        10010         1416        29422         9949
         5151        30942        11775        27052        21986        25150
        32292         2993         4799        13348         6968        30563
         5406        12777         4991        14602        21238        26938
        12100        25555         3728        24521        14272        19748
        29838

Second set of numbers: 5 values

         9534         4653        23339        22913        30845

Third set of numbers: 5 values

         1732         1794        17238         5218         6040



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value          107         4653         1732
Largest value         32292        30845        17238

Windows PC output 2

Seed Value? 7

First set of numbers: 12 values

        17422        15215         7040        15521         6516        30152
        11794        27727        20344         4818        28409        25042

Second set of numbers: 12 values

        12042         6215           63        24189        15812        15512
         2580         9185        28455         9472        22374        13198

Third set of numbers: 94 values

        19266        14140        11133        23453        17475           54
         2013         9945        11966         5234        10082        11741
        31288        23727        27103        17153         6711        32435
         7447        32719        31151        21227        11217         1985
        17085        23890        17194        26699        20119        19859
        25135        14876        20932        19058         1963        26530
         6280         3176        29246        15564        26531         2957
         9903         9235        21819        17596        12419        13499
        11798        23025        20687        30294        15116         4597
         3914        32139        31420        31550        24998        17941
         9087        13840        23841        28174          410        10729
         9815        13934         1464        10780         4954        27855
         6039        19900        13316         8819          257        32443
        13452        21639         1278         1107        20056        27049
        30536         3237        15122        10409        26623        31137
        24062         3212        14238        29989



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value         4818           63           54
Largest value         30152        28455        32719

Mac output 1

Seed Value? 30

First set of numbers: 11 values

   2031806529   1434861956   1631022329   2111013195   1221436278    883342673
    785853400    813664750     87589154   1084613083   1250890245

Second set of numbers: 13 values

     79807613   1296755963   1913420385    288796870    495951870   1079045083
     11311066   1126525326   1295322130   1437309271   1960856241    826795625
   1734873285

Third set of numbers: 81 values

   1326219047   1030750716     76703463    664914441   1859594546   1876019831
    910394363    177074066   1818976167   2102723724   1486734236   1570071607
   2061928160    882973481   1023294397   1459885203   1289939846   1171575257
    387785056   2038051194   1162247908    389336644    198303299   2136409796
    712863532    286115711    530869144   1670633570   2137210112   1278872662
   1996491058    593227431   1754343443    299773191    291385275   1049601765
   1226187897   1286908267   1759434532   2113843781   1550454946    929704724
    456280696     45554235   1125849313    670989874    890181921   1916461245
   2004407009    492629774   1079152433   1815542516    227926189   1792115922
   1634151879   1022268870   1403722090    101820688   1903320204    186262916
   1637155533   2112557767   1411254118   2138563758    407280867   1139148680
    855151755   1574980561    814855805    778297716    526818935    170763964
    991790556    255806678     80575852   1323646954    751256605   1313399522
    321358741    154987782   2129471910



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value     87589154     11311066     45554235
Largest value    2111013195   1960856241   2138563758

Mac output 2

Seed Value? 7

First set of numbers: 50 values

   1977326743    621132276    452154665   1566311569   1143995257    707192808
   1615021558   1621510873   1165762081   1469983786   1365616214   1753973209
    519701294    803655909   1520206580   1499041701    115722103   1468684586
   1004798284   1980842827   1733897595    263789375   1101778217   1982488685
   1478545590   1382451693   1240027358   1958495418   1950632757    799391797
    720236547   1797810937    713504869    327648435    639176137    920132265
    633235808   2012754171   1196944453   1566100122   1885172822    125891516
    587317117   1203943807   1092642215    905042008    414356755   1951997711
    117853558    784826772

Second set of numbers: 21 values

   1426833309   1985021961   1105642382    355516783    871065927    609013490
    782664828    910426321    714192172   1141731721   1318648902    500858874
   1946682725    983197030   1853303192   1363931856   1362255714   1108624531
   1084371145   1479605573   2017716798

Third set of numbers: 6 values

   1520916114    539277757   1260271559    752881752    709957740    840593448



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value    115722103    355516783    539277757
Largest value    2012754171   2017716798   1520916114

onlinegdb output 1

Seed Value? 30

First set of numbers: 34 values

   1867792571   1191308030   1240413721   2134708252   1278462954   1717909034
   1758326472   1352639282   1081373099   1602041298   1751411239   2138131970
   1091011354   1501253366    509120985    847569113   1867770807    506396569
    626734747   1291883239   1192531857     20004997    110005129    650685849
    622588459    418531975    566621729   1437564493    640633144   1846148136
   1931415026    360942067    889972519   1024345099

Second set of numbers: 22 values

     20951825    594770485   2106493143   1373591107   1676143584   1561050794
    977518698   1666791906    504578500    331288416     28429244   1352147613
     51575575    534825813   1978882360   1343458815   1727357671   1998887357
   1453463944    230559872    473992168   1871995919

Third set of numbers: 7 values

   1911556661    365145415    495846090   1695488040    726087482   1385818609
    572349491



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value     20004997     20951825    365145415
Largest value    2138131970   2106493143   1911556661

onlinegdb output 2

Seed Value? 7

First set of numbers: 28 values

   1863967299   1272579899    461085871     21961325   1105564443   2138782586
     68574097   1291851600    118852153   1131251315    191929321   1641615331
   1751255526   1909053865    351969720    462792178   1691535746   1693715570
    143120736    928880504    114632963    732145788   1094870504     34412749
    561287884    781032961   1630849392     77144257

Second set of numbers: 21 values

    389883213   1122762934   2019460530   1662463112   1583848806   2041421855
    620543907   1575147744   2109995953   1912395508   1693999897   1093763620
   2104324829   1188131580    697535498   1865895047   1540101300   1160327677
   1409947145   1086333222   1303448413

Third set of numbers: 70 values

   1200966186   2035594201   1286214505   1235378935    449398437   2067247466
    718744680    526542695     75257048   1108627893   1649305629   2094717578
    623607358   1085670787   1988655786   1244151265    513334883   1951168091
   1009063125     59851133    897448063    965904307   1247982713   1594983561
    684315706    640600366    607827590   2094262851   1726933588   1911276003
    138123204    780416126   1799386556   1424337709   2015795062    101301346
   1344101527    587056094    627844041   1419358575   1695683987    129666022
   1366592506    171807697   1215336810   1207764644   1415958963   1728671693
   1011449087    277538440   1788522826   1908897150   1243442747    889021892
   1356397063   1927758453   1529622258   1964224654   1874537656   1109072198
   1728017009   2012660860   1889488325   1379919918   1289514921   1757799739
   1481221264    486132800    197372185   2109065305



                      Set 1        Set 2        Set 3
-----------------------------------------------------
Smallest value     21961325    389883213     59851133
Largest value    2138782586   2109995953   2109065305