CSCI240 Program 4 Loops, Decision Statements, Symbolic Constants, and Random Number Generation

$30.00

Category: Tags: , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

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

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

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

Generate a random number between 1 and 25. 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, generate a random number (no restrictions), and display the random number. Make sure that there are only 5 values displayed per line.

Generate a random number between 1 and 50. 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, generate a random number (no restrictions), and display the random number. Make sure that there are only 6 values displayed per line.

Generate a random number between 1 and 250. 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, generate a random number (no restrictions), and display the random number. Make sure that there are only 7 values displayed per line.

Symbolic Constants

This program MUST use at least 6 symbolic constants.

Three of the constants should be for the maximum values for the sizes of the sets of numbers. The values should be 25, 50, and 250.

The other three constants should be for the maximum number of values to be displayed for each set of numbers. The values should be 5, 6, and 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.
  4. Make sure that the copy of the program that is handed in uses srand(17); 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.

Output

Run 1 (using srand(17);) on Windows PC

There are 20 numbers in the first set of numbers.

       26602         30017         18297         20363         13015
       28509         15290         29003         24399          3339
       28849         17055         19424          4588         15756
        6098         11834          1351         21383         18431


There are 6 numbers in the second set of numbers.

       14763         14082          4564         25482         30678         20183


There are 16 numbers in the third set of values.

       18376         20694         32234          8292         29828         23406         31490
       25791         15822         24763         23255         15434         32590         15383
         108         24271

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

There are 20 numbers in the first set of numbers.

   507111939    1815247477    1711656657     122498987    1551140683
  1717468248    1161144809    1176904574    1910786348    1115693598
  1782579629     271465306    1262131714    1951735779      10529728
   879479442     281039393    1112538398     292740757     208867622


There are 7 numbers in the second set of numbers.

    27064583    1755396964     826431462    2056836685    1209899036     250444609
   154595343


There are 79 numbers in the third set of values.

  1021029944    2055929278     991495116    1732797539    1102501006    1245501526    1621040173
  1844641769    1820283491     452598075     428768851    1510443072     612519917    1733124948
   162813128     506076018    1584392406      85944842    1365948710     899782540      65307606
   260790425      90549448    1446150460     230864474    1783748036     581528932     558682627
   980407305      63551704     814116569    1238860146    1668516157     909588173    1659824265
   853847325    1126262021    1164922289     240501524     544890214    1099555890    1139060795
  1525552207    1148681516      12252882    1923241309    2140309366    1828427112    2030966461
   200740962     156538897     281774304     579285693    1511270400    1632519731    1508044845
  1107708021     712973104    2127692315     227048361    2070846255     445540856    2077173350
  1558327818     105078314     819665564      11538643     656444671    1242090858     132517919
   288122694    2049977720    1895391219      67798135    1316922035    1542176263    1376316598
  1206700749     183926175

Run 3 (using srand(17);) on CodeChef

There are 16 numbers in the first set of numbers.

     3978157     263514239    1969574147    1833982879     488658959  
   231688945    1043863911    1421669753    1942003127    1343955001  
   461983965     602354579     726141576    1746455982    1641023978  
  1153484208  


There are 28 numbers in the second set of numbers.

  1559964282    1484758023     360805106    1295207561     808154853    1578628148  
  1764616483     134664533    1026318808     487190350    2084406199     494281178  
   767599596    1164840817     498259336    1031113836     986931316     184758567  
  1519772795    1218620261    1228622478     793958901    1013139741     425093831  
  1255942866    1615494320    1151235407     854915200  


There are 151 numbers in the third set of values.

   157235967    1800402877     521515284    1641993990      13724335    1816722845     302665195  
  1592352484    1433855680     437329729     471187644    1921046030     374252280     965468822  
   541161979    1539093097    1463728158    1572275815     378540765    1648486725     944564962  
  1597161027     729625555    1738523863     462817120    1154719386     846983081    2078311440  
   158471146    1701898282    1039862443     315707113    1354817511    1561377727    1957701104  
  1368541847    1230616925     112882651     813410683     516988957     550212380    1284598327  
   290551340     924464661     102583501     831713319     316074110    1566311660     256505486  
   694614876    1067314737    1201070448     144292255    1796940293     792110664     607109375  
   804176031    1639093745     537937167     962647177    1193508379    1577799610    1278354291  
   400842243     991693690    1088571747    1769384090      74826967    1201454398     435311125  
   591815924    1751666779    1719909452     882367264     528647792    1822492953    1714080583  
   844721902    1241320965    1970586069    1539336778     161152055    1024172870    1683629033  
  1958092348    1816283534     143254760     614784731    1307893631     681191928    1577431909  
   353918363     111507890     708302552     754760606    1103201580    1796874299     376661048  
  1178028547     850845049     811972173    1769844472     455028180     384397977     504728088  
   983675972      59407282      71325024    1828397875    1300728248    2041911093    1220251005  
  1461880303     918600315     756396391    1272489003     587400201     899651151    1887273734  
  1895293833    1580843079    1317221995     101728548    1692350970    2025524547     856489154  
   648068902    1674915198    1233150202    1826097450     378276600    2045122375    1448458274  
   833304780     282036704    1953186362    1816980753     341443986    2024511386    1497894980  
  1642172234    1918938832     570662337     956568889     690055499    1327058728      81574244  
  1277455701      79226232    1968847979    1025265886  

Extra Credit 1

For up to 5 points of extra credit, add code that will count a specific type of number.

Start by displaying a menu of the three types of numbers to the user and getting the user’s selection. The menu should resemble the following:

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count?

The user’s selection MUST be handled as a character value and BOTH upper and lowercase values should be accepted. The value that the user enters should be error checked with a loop (choice is left to you) that will display an error message and allow the user to re-enter their value until a valid value is entered.

Once a valid number type has been made by the user, count that specific type of number in each set and display the count for each set.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit 1 Output (using srand(17);) on Windows PC

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? e


There are 20 numbers in the first set of numbers.

       26602       30017       18297       20363       13015
       28509       15290       29003       24399        3339
       28849       17055       19424        4588       15756
        6098       11834        1351       21383       18431

There are 7 even numbers in the first set of numbers



There are 6 numbers in the second set of numbers.

       14763       14082        4564       25482       30678       20183

There are 4 even numbers in the second set of numbers



There are 16 numbers in the third set of values.

       18376       20694       32234        8292       29828       23406       31490
       25791       15822       24763       23255       15434       32590       15383
         108       24271

There are 11 even numbers in the third set of numbers

Extra Credit 1 Output (using srand(17);) on a Mac

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? O


There are 20 numbers in the first set of numbers.

   507111939  1815247477  1711656657   122498987  1551140683
  1717468248  1161144809  1176904574  1910786348  1115693598
  1782579629   271465306  1262131714  1951735779    10529728
   879479442   281039393  1112538398   292740757   208867622

There are 10 odd numbers in the first set of numbers



There are 7 numbers in the second set of numbers.

    27064583  1755396964   826431462  2056836685  1209899036   250444609
   154595343

There are 4 odd numbers in the second set of numbers



There are 79 numbers in the third set of values.

  1021029944  2055929278   991495116  1732797539  1102501006  1245501526  1621040173
  1844641769  1820283491   452598075   428768851  1510443072   612519917  1733124948
   162813128   506076018  1584392406    85944842  1365948710   899782540    65307606
   260790425    90549448  1446150460   230864474  1783748036   581528932   558682627
   980407305    63551704   814116569  1238860146  1668516157   909588173  1659824265
   853847325  1126262021  1164922289   240501524   544890214  1099555890  1139060795
  1525552207  1148681516    12252882  1923241309  2140309366  1828427112  2030966461
   200740962   156538897   281774304   579285693  1511270400  1632519731  1508044845
  1107708021   712973104  2127692315   227048361  2070846255   445540856  2077173350
  1558327818   105078314   819665564    11538643   656444671  1242090858   132517919
   288122694  2049977720  1895391219    67798135  1316922035  1542176263  1376316598
  1206700749   183926175

There are 38 odd numbers in the third set of numbers

Extra Credit 1 Output (using srand(17);) on CodeChef

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? Z

There are 16 numbers in the first set of numbers.

     3978157     263514239    1969574147    1833982879     488658959  
   231688945    1043863911    1421669753    1942003127    1343955001  
   461983965     602354579     726141576    1746455982    1641023978  
  1153484208  

There are 0 zeroes in the first set of numbers



There are 28 numbers in the second set of numbers.

  1559964282    1484758023     360805106    1295207561     808154853    1578628148  
  1764616483     134664533    1026318808     487190350    2084406199     494281178  
   767599596    1164840817     498259336    1031113836     986931316     184758567  
  1519772795    1218620261    1228622478     793958901    1013139741     425093831  
  1255942866    1615494320    1151235407     854915200  

There are 0 zeroes in the second set of numbers



There are 151 numbers in the third set of values.

   157235967    1800402877     521515284    1641993990      13724335    1816722845     302665195  
  1592352484    1433855680     437329729     471187644    1921046030     374252280     965468822  
   541161979    1539093097    1463728158    1572275815     378540765    1648486725     944564962  
  1597161027     729625555    1738523863     462817120    1154719386     846983081    2078311440  
   158471146    1701898282    1039862443     315707113    1354817511    1561377727    1957701104  
  1368541847    1230616925     112882651     813410683     516988957     550212380    1284598327  
   290551340     924464661     102583501     831713319     316074110    1566311660     256505486  
   694614876    1067314737    1201070448     144292255    1796940293     792110664     607109375  
   804176031    1639093745     537937167     962647177    1193508379    1577799610    1278354291  
   400842243     991693690    1088571747    1769384090      74826967    1201454398     435311125  
   591815924    1751666779    1719909452     882367264     528647792    1822492953    1714080583  
   844721902    1241320965    1970586069    1539336778     161152055    1024172870    1683629033  
  1958092348    1816283534     143254760     614784731    1307893631     681191928    1577431909  
   353918363     111507890     708302552     754760606    1103201580    1796874299     376661048  
  1178028547     850845049     811972173    1769844472     455028180     384397977     504728088  
   983675972      59407282      71325024    1828397875    1300728248    2041911093    1220251005  
  1461880303     918600315     756396391    1272489003     587400201     899651151    1887273734  
  1895293833    1580843079    1317221995     101728548    1692350970    2025524547     856489154  
   648068902    1674915198    1233150202    1826097450     378276600    2045122375    1448458274  
   833304780     282036704    1953186362    1816980753     341443986    2024511386    1497894980  
  1642172234    1918938832     570662337     956568889     690055499    1327058728      81574244  
  1277455701      79226232    1968847979    1025265886  

There are 0 zeroes in the third set of numbers

Extra Credit 2

For up to an additional 5 points of extra credit, add code that will “mark” the specific type of number that the user has selected with a “**” after the number.

Note about extra credit 2: the points will ONLY be awarded if the required portions of the assignment work correctly AND if extra credit 1 works correctly.

Extra Credit 2 Output (using srand(17);) on Windows PC

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? e


There are 20 numbers in the first set of numbers.

       26602**       30017         18297         20363         13015
       28509         15290**       29003         24399          3339
       28849         17055         19424**        4588**       15756**
        6098**       11834**        1351         21383         18431

There are 7 even numbers in the first set of numbers



There are 6 numbers in the second set of numbers.

       14763         14082**        4564**       25482**       30678**       20183

There are 4 even numbers in the second set of numbers



There are 16 numbers in the third set of values.

       18376**       20694**       32234**        8292**       29828**       23406**       31490**
       25791         15822**       24763         23255         15434**       32590**       15383
         108**       24271

There are 11 even numbers in the third set of numbers

Extra Credit 2 Output (using srand(17);) on a Mac

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? O


There are 20 numbers in the first set of numbers.

   507111939**  1815247477**  1711656657**   122498987**  1551140683**
  1717468248    1161144809**  1176904574    1910786348    1115693598
  1782579629**   271465306    1262131714    1951735779**    10529728
   879479442     281039393**  1112538398     292740757**   208867622

There are 10 odd numbers in the first set of numbers



There are 7 numbers in the second set of numbers.

    27064583**  1755396964     826431462    2056836685**  1209899036     250444609**
   154595343**

There are 4 odd numbers in the second set of numbers



There are 79 numbers in the third set of values.

  1021029944    2055929278     991495116    1732797539**  1102501006    1245501526    1621040173**
  1844641769**  1820283491**   452598075**   428768851**  1510443072     612519917**  1733124948
   162813128     506076018    1584392406      85944842    1365948710     899782540      65307606
   260790425**    90549448    1446150460     230864474    1783748036     581528932     558682627**
   980407305**    63551704     814116569**  1238860146    1668516157**   909588173**  1659824265**
   853847325**  1126262021**  1164922289**   240501524     544890214    1099555890    1139060795**
  1525552207**  1148681516      12252882    1923241309**  2140309366    1828427112    2030966461**
   200740962     156538897**   281774304     579285693**  1511270400    1632519731**  1508044845**
  1107708021**   712973104    2127692315**   227048361**  2070846255**   445540856    2077173350
  1558327818     105078314     819665564      11538643**   656444671**  1242090858     132517919**
   288122694    2049977720    1895391219**    67798135**  1316922035**  1542176263**  1376316598
  1206700749**   183926175**

There are 38 odd numbers in the third set of numbers

Extra Credit 2 Output (using srand(17);) on CodeChef

Please notice that this output is dealing with EVEN numbers, which is different from the Extra Credit 1 Output.

*****  Number Types  *****
E)ven
O)dd
Z)ero

What type of number would you like to count? E

There are 16 numbers in the first set of numbers.

     3978157     263514239    1969574147    1833982879     488658959  
   231688945    1043863911    1421669753    1942003127    1343955001  
   461983965     602354579     726141576**  1746455982**  1641023978**
  1153484208**

There are 4 even numbers in the first set of numbers



There are 28 numbers in the second set of numbers.

  1559964282**  1484758023     360805106**  1295207561     808154853    1578628148**
  1764616483     134664533    1026318808**   487190350**  2084406199     494281178**
   767599596**  1164840817     498259336**  1031113836**   986931316**   184758567  
  1519772795    1218620261    1228622478**   793958901    1013139741     425093831  
  1255942866**  1615494320**  1151235407     854915200**

There are 14 even numbers in the second set of numbers



There are 151 numbers in the third set of values.

   157235967    1800402877     521515284**  1641993990**    13724335    1816722845     302665195  
  1592352484**  1433855680**   437329729     471187644**  1921046030**   374252280**   965468822**
   541161979    1539093097    1463728158**  1572275815     378540765    1648486725     944564962**
  1597161027     729625555    1738523863     462817120**  1154719386**   846983081    2078311440**
   158471146**  1701898282**  1039862443     315707113    1354817511    1561377727    1957701104**
  1368541847    1230616925     112882651     813410683     516988957     550212380**  1284598327  
   290551340**   924464661     102583501     831713319     316074110**  1566311660**   256505486**
   694614876**  1067314737    1201070448**   144292255    1796940293     792110664**   607109375  
   804176031    1639093745     537937167     962647177    1193508379    1577799610**  1278354291  
   400842243     991693690**  1088571747    1769384090**    74826967    1201454398**   435311125  
   591815924**  1751666779    1719909452**   882367264**   528647792**  1822492953    1714080583  
   844721902**  1241320965    1970586069    1539336778**   161152055    1024172870**  1683629033  
  1958092348**  1816283534**   143254760**   614784731    1307893631     681191928**  1577431909  
   353918363     111507890**   708302552**   754760606**  1103201580**  1796874299     376661048**
  1178028547     850845049     811972173    1769844472**   455028180**   384397977     504728088**
   983675972**    59407282**    71325024**  1828397875    1300728248**  2041911093    1220251005  
  1461880303     918600315     756396391    1272489003     587400201     899651151    1887273734**
  1895293833    1580843079    1317221995     101728548**  1692350970**  2025524547     856489154**
   648068902**  1674915198**  1233150202**  1826097450**   378276600**  2045122375    1448458274**
   833304780**   282036704**  1953186362**  1816980753     341443986**  2024511386**  1497894980**
  1642172234**  1918938832**   570662337     956568889     690055499    1327058728**    81574244**
  1277455701      79226232**  1968847979    1025265886**

There are 73 even numbers in the third set of numbers