| CSCI 240 | Spring 2026 |
For this assignment, write a program that will generate and display simulated investment values benefiting from compound interest using three sets of parameters, each implemented using different looping mechanisms.
The cpp file that is submitted for grading must be named assign4.cpp.
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));
Ask the user to enter the seed value for the random number generator and use it with the srand() instruction. Only ever do this step once.
Generate a random real number between 1000 and 1400, which should be our first two symbolic constants in this program. This will represent the yearly capital contributed.
Generate a random real number between 2 and 4, which should be our next two symbolic constants in this program. This will represent the interest rate of this investment fund.
Generate a random integer number between 15 and 21. This will represent the number of years of reporting on this investment.
Print out some initial reporting details regarding this investment fund, and
then in a for loop, print out the value of the investment
fund as it changes after every year. The amount increases by adding the yearly
capital contributed and the interest to the current running total. Interest is
calculated by multiplying the current running total by interest_rate / 100
before the yearly capital contribution is considered.
When displaying the investment fund value, print it into a space 12 characters wide. Additionally, after every 5 updates, print a blank line. After every 10, print 2 blank lines.
Finally, calculate the amount of interest earned over this period of time by subtracting from the final investment fund value the amount of capital contributed over the course of num_of_years. 100 dash characters are used to create partitions between each investment report, which you are encouraged to print by using a for-loop that prints one dash character, 100 times. A value of which you should define as your 13th symbolic constant.
Repeat this algorithm for investment fund #2, except the minimum/maximum yearly contribution will be 7000/10000 (symbolic constants #5 and #6), the minimum/maximum interest rate will be 3.00 and 4.50 (symbolic constants #7 and #8), and for a range of years between 19 and 25. For an extra challenge (not worth extra credit), define these min/max interest rates as integers, which will require a small modification to the code used in the first part of this assignment.
The investment fund reporting done here will use a while loop this time. Using a different looping mechanism here will result in points lost.
Repeat this algorithm for investment fund #3, except the minimum/maximum yearly contribution will be 18500/22500 (symbolic constants #9 and #10), the minimum/maximum interest rate will be 4.50 and 6.00 (symbolic constants #11 and #12), and for a range of years between 23 and 29.
The investment fund reporting done here will use a do while loop this time. Using a different looping mechanism here will result in points lost.
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.
This program MUST use at least THIRTEEN (13) symbolic constants, as described above.
More symbolic constants may be added to the code if necessary.
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.
For-loop, while-loop, and do-while-loops must be used as described above.
The program MUST use the 13 symbolic constants described above.
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) on the autograder and Blackboard.
Enter a RNG seed value: 55 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1002.69 Interest rate: 2.55% Invested over 15 years. Year 0: $ 1002.69 Year 1: $ 2030.95 Year 2: $ 3085.43 Year 3: $ 4166.80 Year 4: $ 5275.74 Year 5: $ 6412.96 Year 6: $ 7579.18 Year 7: $ 8775.14 Year 8: $ 10001.60 Year 9: $ 11259.33 Year 10: $ 12549.13 Year 11: $ 13871.82 Year 12: $ 15228.24 Year 13: $ 16619.25 Year 14: $ 18045.73 Year 15: $ 19508.59 Interest earned over time: $ 4468.24 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $8519.90 Interest rate: 3.87% Invested over 21 years. Year 0: $ 8519.90 Year 1: $ 17369.86 Year 2: $ 26562.67 Year 3: $ 36111.61 Year 4: $ 46030.47 Year 5: $ 56333.59 Year 6: $ 67035.86 Year 7: $ 78152.73 Year 8: $ 89700.27 Year 9: $ 101695.16 Year 10: $ 114154.73 Year 11: $ 127096.98 Year 12: $ 140540.62 Year 13: $ 154505.06 Year 14: $ 169010.48 Year 15: $ 184077.84 Year 16: $ 199728.92 Year 17: $ 215986.33 Year 18: $ 232873.55 Year 19: $ 250414.97 Year 20: $ 268635.94 Year 21: $ 287562.78 Interest earned over time: $ 108644.88 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $20463.75 Interest rate: 4.61% Invested over 28 years. Year 0: $ 20463.75 Year 1: $ 41870.27 Year 2: $ 64262.98 Year 3: $ 87687.33 Year 4: $ 112190.84 Year 5: $ 137823.22 Year 6: $ 164636.48 Year 7: $ 192685.03 Year 8: $ 222025.78 Year 9: $ 252718.27 Year 10: $ 284824.75 Year 11: $ 318410.38 Year 12: $ 353543.28 Year 13: $ 390294.78 Year 14: $ 428739.41 Year 15: $ 468955.19 Year 16: $ 511023.69 Year 17: $ 555030.31 Year 18: $ 601064.31 Year 19: $ 649219.12 Year 20: $ 699592.38 Year 21: $ 752286.38 Year 22: $ 807407.94 Year 23: $ 865069.00 Year 24: $ 925386.50 Year 25: $ 988482.81 Year 26: $ 1054486.00 Year 27: $ 1123529.88 Year 28: $ 1195754.62 Interest earned over time: $ 622769.62 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---
Enter a RNG seed value: 107 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1004.79 Interest rate: 2.66% Invested over 17 years. Year 0: $ 1004.79 Year 1: $ 2036.31 Year 2: $ 3095.26 Year 3: $ 4182.39 Year 4: $ 5298.43 Year 5: $ 6444.16 Year 6: $ 7620.36 Year 7: $ 8827.85 Year 8: $ 10067.46 Year 9: $ 11340.05 Year 10: $ 12646.48 Year 11: $ 13987.67 Year 12: $ 15364.53 Year 13: $ 16778.02 Year 14: $ 18229.11 Year 15: $ 19718.79 Year 16: $ 21248.10 Year 17: $ 22818.09 Interest earned over time: $ 5736.66 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $9130.70 Interest rate: 4.43% Invested over 21 years. Year 0: $ 9130.70 Year 1: $ 18665.62 Year 2: $ 28622.64 Year 3: $ 39020.47 Year 4: $ 49878.61 Year 5: $ 61217.43 Year 6: $ 73058.23 Year 7: $ 85423.21 Year 8: $ 98335.59 Year 9: $ 111819.61 Year 10: $ 125900.56 Year 11: $ 140604.88 Year 12: $ 155960.16 Year 13: $ 171995.22 Year 14: $ 188740.14 Year 15: $ 206226.36 Year 16: $ 224486.70 Year 17: $ 243555.42 Year 18: $ 263468.31 Year 19: $ 284262.75 Year 20: $ 305977.75 Year 21: $ 328654.09 Interest earned over time: $ 136909.39 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $20214.62 Interest rate: 5.14% Invested over 27 years. Year 0: $ 20214.62 Year 1: $ 41468.27 Year 2: $ 63814.36 Year 3: $ 87309.04 Year 4: $ 112011.34 Year 5: $ 137983.34 Year 6: $ 165290.31 Year 7: $ 194000.86 Year 8: $ 224187.12 Year 9: $ 255924.97 Year 10: $ 289294.12 Year 11: $ 324378.47 Year 12: $ 361266.12 Year 13: $ 400049.81 Year 14: $ 440827.00 Year 15: $ 483700.12 Year 16: $ 528776.94 Year 17: $ 576170.69 Year 18: $ 626000.50 Year 19: $ 678391.56 Year 20: $ 733475.50 Year 21: $ 791390.75 Year 22: $ 852282.88 Year 23: $ 916304.81 Year 24: $ 983617.50 Year 25: $ 1054390.00 Year 26: $ 1128800.25 Year 27: $ 1207035.25 Interest earned over time: $ 661240.56 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---
Enter a RNG seed value: 99 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1000.30 Interest rate: 2.04% Invested over 20 years. Year 0: $ 1000.30 Year 1: $ 2021.01 Year 2: $ 3062.53 Year 3: $ 4125.31 Year 4: $ 5209.77 Year 5: $ 6316.35 Year 6: $ 7445.50 Year 7: $ 8597.69 Year 8: $ 9773.38 Year 9: $ 10973.06 Year 10: $ 12197.21 Year 11: $ 13446.33 Year 12: $ 14720.94 Year 13: $ 16021.54 Year 14: $ 17348.68 Year 15: $ 18702.89 Year 16: $ 20084.73 Year 17: $ 21494.76 Year 18: $ 22933.56 Year 19: $ 24401.70 Year 20: $ 25899.79 Interest earned over time: $ 5893.79 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $8219.09 Interest rate: 4.11% Invested over 25 years. Year 0: $ 8219.09 Year 1: $ 16776.39 Year 2: $ 25685.83 Year 3: $ 34961.89 Year 4: $ 44619.67 Year 5: $ 54674.86 Year 6: $ 65143.82 Year 7: $ 76043.57 Year 8: $ 87391.85 Year 9: $ 99207.12 Year 10: $ 111508.58 Year 11: $ 124316.24 Year 12: $ 137650.94 Year 13: $ 151534.36 Year 14: $ 165989.09 Year 15: $ 181038.64 Year 16: $ 196707.47 Year 17: $ 213021.08 Year 18: $ 230005.98 Year 19: $ 247689.81 Year 20: $ 266101.34 Year 21: $ 285270.50 Year 22: $ 305228.47 Year 23: $ 326007.72 Year 24: $ 347642.03 Year 25: $ 370166.59 Interest earned over time: $ 164689.34 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $21129.67 Interest rate: 4.81% Invested over 27 years. Year 0: $ 21129.67 Year 1: $ 43275.89 Year 2: $ 66487.56 Year 3: $ 90815.95 Year 4: $ 116314.77 Year 5: $ 143040.34 Year 6: $ 171051.69 Year 7: $ 200410.66 Year 8: $ 231182.08 Year 9: $ 263433.91 Year 10: $ 297237.38 Year 11: $ 332667.12 Year 12: $ 369801.41 Year 13: $ 408722.22 Year 14: $ 449515.50 Year 15: $ 492271.38 Year 16: $ 537084.25 Year 17: $ 584053.06 Year 18: $ 633281.50 Year 19: $ 684878.31 Year 20: $ 738957.50 Year 21: $ 795638.44 Year 22: $ 855046.25 Year 23: $ 917312.19 Year 24: $ 982573.75 Year 25: $ 1050975.00 Year 26: $ 1122667.12 Year 27: $ 1197808.25 Interest earned over time: $ 627307.19 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---
Enter a RNG seed value: 1 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1000.00 Interest rate: 2.26% Invested over 17 years. Year 0: $ 1000.00 Year 1: $ 2022.60 Year 2: $ 3068.31 Year 3: $ 4137.65 Year 4: $ 5231.17 Year 5: $ 6349.39 Year 6: $ 7492.89 Year 7: $ 8662.23 Year 8: $ 9857.99 Year 9: $ 11080.78 Year 10: $ 12331.21 Year 11: $ 13609.89 Year 12: $ 14917.48 Year 13: $ 16254.61 Year 14: $ 17621.97 Year 15: $ 19020.22 Year 16: $ 20450.08 Year 17: $ 21912.25 Interest earned over time: $ 4912.25 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $8375.95 Interest rate: 3.80% Invested over 25 years. Year 0: $ 8375.95 Year 1: $ 17070.10 Year 2: $ 26094.55 Year 3: $ 35461.83 Year 4: $ 45184.97 Year 5: $ 55277.50 Year 6: $ 65753.45 Year 7: $ 76627.37 Year 8: $ 87914.39 Year 9: $ 99630.21 Year 10: $ 111791.11 Year 11: $ 124414.01 Year 12: $ 137516.45 Year 13: $ 151116.66 Year 14: $ 165233.53 Year 15: $ 179886.70 Year 16: $ 195096.55 Year 17: $ 210884.22 Year 18: $ 227271.66 Year 19: $ 244281.66 Year 20: $ 261937.86 Year 21: $ 280264.84 Year 22: $ 299288.06 Year 23: $ 319033.97 Year 24: $ 339530.03 Year 25: $ 360804.72 Interest earned over time: $ 151405.97 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $18688.18 Interest rate: 5.52% Invested over 24 years. Year 0: $ 18688.18 Year 1: $ 38407.57 Year 2: $ 59215.08 Year 3: $ 81170.75 Year 4: $ 104337.93 Year 5: $ 128783.48 Year 6: $ 154577.92 Year 7: $ 181795.72 Year 8: $ 210515.39 Year 9: $ 240819.81 Year 10: $ 272796.44 Year 11: $ 306537.53 Year 12: $ 342140.44 Year 13: $ 379707.94 Year 14: $ 419348.41 Year 15: $ 461176.22 Year 16: $ 505312.09 Year 17: $ 551883.38 Year 18: $ 601024.50 Year 19: $ 652877.19 Year 20: $ 707591.12 Year 21: $ 765324.19 Year 22: $ 826242.94 Year 23: $ 890523.19 Year 24: $ 958350.44 Interest earned over time: $ 509834.12 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---
Enter a RNG seed value: 777 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1176.46 Interest rate: 3.87% Invested over 20 years. Year 0: $ 1176.46 Year 1: $ 2398.45 Year 2: $ 3667.73 Year 3: $ 4986.13 Year 4: $ 6355.55 Year 5: $ 7777.97 Year 6: $ 9255.44 Year 7: $ 10790.09 Year 8: $ 12384.12 Year 9: $ 14039.85 Year 10: $ 15759.65 Year 11: $ 17546.01 Year 12: $ 19401.50 Year 13: $ 21328.80 Year 14: $ 23330.68 Year 15: $ 25410.04 Year 16: $ 27569.87 Year 17: $ 29813.28 Year 18: $ 32143.52 Year 19: $ 34563.93 Year 20: $ 37078.02 Interest earned over time: $ 13548.82 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $9100.70 Interest rate: 3.32% Invested over 23 years. Year 0: $ 9100.70 Year 1: $ 18503.54 Year 2: $ 28218.56 Year 3: $ 38256.12 Year 4: $ 48626.92 Year 5: $ 59342.04 Year 6: $ 70412.89 Year 7: $ 81851.30 Year 8: $ 93669.46 Year 9: $ 105879.98 Year 10: $ 118495.90 Year 11: $ 131530.66 Year 12: $ 144998.17 Year 13: $ 158912.81 Year 14: $ 173289.42 Year 15: $ 188143.33 Year 16: $ 203490.39 Year 17: $ 219346.97 Year 18: $ 235729.98 Year 19: $ 252656.92 Year 20: $ 270145.84 Year 21: $ 288215.38 Year 22: $ 306884.81 Year 23: $ 326174.09 Interest earned over time: $ 116857.98 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $22438.99 Interest rate: 4.80% Invested over 29 years. Year 0: $ 22438.99 Year 1: $ 45955.73 Year 2: $ 70601.97 Year 3: $ 96431.97 Year 4: $ 123502.59 Year 5: $ 151873.41 Year 6: $ 181606.88 Year 7: $ 212768.44 Year 8: $ 245426.70 Year 9: $ 279653.53 Year 10: $ 315524.28 Year 11: $ 353117.91 Year 12: $ 392517.16 Year 13: $ 433808.75 Year 14: $ 477083.56 Year 15: $ 522436.88 Year 16: $ 569968.50 Year 17: $ 619783.06 Year 18: $ 671990.25 Year 19: $ 726704.94 Year 20: $ 784047.56 Year 21: $ 844144.38 Year 22: $ 907127.62 Year 23: $ 973135.94 Year 24: $ 1042314.62 Year 25: $ 1114816.00 Year 26: $ 1190799.62 Year 27: $ 1270432.75 Year 28: $ 1353890.62 Year 29: $ 1441357.00 Interest earned over time: $ 790626.31 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---
Enter a RNG seed value: 10101 ---------------------------------------------------------------------------------------------------- Investment #1 parameters, explored using a for-loop. Yearly investment: $1190.22 Interest rate: 3.91% Invested over 16 years. Year 0: $ 1190.22 Year 1: $ 2426.98 Year 2: $ 3712.09 Year 3: $ 5047.46 Year 4: $ 6435.03 Year 5: $ 7876.86 Year 6: $ 9375.07 Year 7: $ 10931.85 Year 8: $ 12549.51 Year 9: $ 14230.41 Year 10: $ 15977.04 Year 11: $ 17791.96 Year 12: $ 19677.85 Year 13: $ 21637.47 Year 14: $ 23673.72 Year 15: $ 25789.58 Year 16: $ 27988.17 Interest earned over time: $ 8944.65 ---------------------------------------------------------------------------------------------------- Investment #2 parameters, explored using a while-loop. Yearly investment: $9672.76 Interest rate: 3.90% Invested over 25 years. Year 0: $ 9672.76 Year 1: $ 19723.14 Year 2: $ 30165.90 Year 3: $ 41016.33 Year 4: $ 52290.37 Year 5: $ 64004.55 Year 6: $ 76176.05 Year 7: $ 88822.72 Year 8: $ 101963.12 Year 9: $ 115616.52 Year 10: $ 129802.95 Year 11: $ 144543.22 Year 12: $ 159858.95 Year 13: $ 175772.61 Year 14: $ 192307.53 Year 15: $ 209487.98 Year 16: $ 227339.16 Year 17: $ 245887.23 Year 18: $ 265159.44 Year 19: $ 285184.03 Year 20: $ 305990.38 Year 21: $ 327609.00 Year 22: $ 350071.62 Year 23: $ 373411.19 Year 24: $ 397661.91 Year 25: $ 422859.38 Interest earned over time: $ 181040.38 ---------------------------------------------------------------------------------------------------- Investment #3 parameters, explored using a do-while-loop. Yearly investment: $19086.06 Interest rate: 5.27% Invested over 23 years. Year 0: $ 19086.06 Year 1: $ 39178.34 Year 2: $ 60329.88 Year 3: $ 82596.53 Year 4: $ 106037.08 Year 5: $ 130713.41 Year 6: $ 156690.69 Year 7: $ 184037.48 Year 8: $ 212826.00 Year 9: $ 243132.25 Year 10: $ 275036.25 Year 11: $ 308622.22 Year 12: $ 343978.84 Year 13: $ 381199.47 Year 14: $ 420382.38 Year 15: $ 461631.00 Year 16: $ 505054.25 Year 17: $ 550766.75 Year 18: $ 598889.25 Year 19: $ 649548.75 Year 20: $ 702879.00 Year 21: $ 759020.81 Year 22: $ 818122.44 Year 23: $ 880339.94 Interest earned over time: $ 441360.53 ---------------------------------------------------------------------------------------------------- --- END OF REPORT ---