This assignment is a modification of the program that you wrote for Lab 4. In that assignment, you used a traditional sequential file processing model in which you performed input, process, and output in a looping structure; one set of data at a time. Now that you have learned how to use arrays, you are to revert back to the basic input, process, output model. That is, you will input all of the data before you process any of it. Then, you will process all of the data before you generate any output. Finally, you will generate all of the output.
Your new program should make use of three functions. One function will read all of the input data into parallel arrays (one array for the present values, one for the nominal rates, one for the compounding frequencies, and one for the numbers of years). The second function will calculate the future values, the total interests, and the APYs and store the results in parallel arrays. The third function will write the table described in the output specifications below to the output file.
In Lab07.cpp, I've written enough code to get you started. You'll need to
Generate a text file named "Investments.txt" containing a table that gives the results of several lump-sum investments earning compound interest. Each line of the table represents one investment and contains the present value (dollars), the nominal annual rate (as a percent), the compounding frequency (monthly, quarterly, semiannually, or annually), the term in years, the future value (dollars), the total interest earned during the term of the investment (dollars), and the annual percentage yield (as a percent). The table columns are separated by two blank spaces and the table format is illustrated in the example below.
Present Nominal Compounding Future Total Value Rate Frequency Years Value Interest APY ---------- ------- ------------ ----- ---------- ---------- ------- 1000.00 5.25% Annually 10 1668.10 668.10 5.250% 1000.00 5.25% Semiannually 10 1679.05 679.05 5.319% 1000.00 5.25% Quarterly 10 1684.70 684.70 5.354% 1000.00 5.25% Monthly 10 1688.52 688.52 5.378% 1500.00 4.00% Quarterly 5 1830.29 330.29 4.060% 500.00 6.90% Monthly 3 614.63 114.63 7.122% 2500.00 3.93% Monthly 8 3421.84 921.84 4.002%
The investment parameters will be read from a text file named "Investments.dat". Each line of the file contains four numbers: the present value (in dollars), the nominal annual rate (as a percent but without the percent symbol), the compounding frequency (an integer), and the term in years (an integer). The number of lines in the file is unknown ahead of time. The results shown earlier were generated using this data file:
1000 5.25 1 10 1000 5.25 2 10 1000 5.25 4 10 1000 5.25 12 10 1500 4.0 4 5 500 6.9 12 3 2500 3.93 12 8
Calculate the future value, the total interest earned, and the annual percentage yield for each investment:
Variable | Represents | Formula |
FV | Future Value | FV = PV*(1+i)n |
PV | Present Value | Given |
r | Nominal Annual Rate | Given |
ppy | Periods per Year | Given |
i | Periodic Rate | i=r/ppy |
t | Time in Years | Given |
n | Number of Periods | n=t*ppy |
I | Total Interest | I = FV - PV |
APY | Annual Percentage Yield | APY = (1+i)ppy - 1 |
There is no exponentiation operator in C++. However, C++ provides a function that performs this useful operation:
pow(b, n) returns the value of bn as a double
To use this function, you must include the cmath header file.