This assignment is a modification of the program that you wrote for Lab 8. The only change is in the output specifications. The output is to be sorted by present value in ascending order. All records having the same present value are to be sorted by the number of years in ascending order. All records having the same present value and the same number of years are to be sorted by compounding frequency in ascending order.
You might think that meeting this specification will be difficult but, in reality, it is not. One of the properties of an insertion sort (but not a selection sort) is that it is stable. That is, following the sorting operation, records having the same key value appear in the same relative order that they did before the sort. This is illustrated in the table below where each record consists of two fields and the records are sorted by present value. Notice that after sorting, the two records having a present value of 500 are in the same relative order that they were before the sorting operation (15 years and 5 years). Similarly, the three records having a present value of 1,000 are also in the same relative order (10 years, 20 years, and 15 years).
Before Sorting | After Sorting by Present Value | ||
Present Value | Years | Present Value | Years |
1,000 | 10 | 500 | 15 |
500 | 15 | 500 | 5 |
1,000 | 20 | 1,000 | 10 |
500 | 5 | 1,000 | 20 |
1,000 | 15 | 1,000 | 15 |
Before Sorting | After Sorting by Years | After Sorting by Present Value | |||
Present Value | Years | Present Value | Years | Present Value | Years |
1,000 | 10 | 500 | 5 | 500 | 5 |
500 | 15 | 1,000 | 10 | 500 | 15 |
1,000 | 20 | 500 | 15 | 1,000 | 10 |
500 | 5 | 1,000 | 15 | 1,000 | 15 |
1,000 | 15 | 1,000 | 20 | 1,000 | 20 |
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. The table is to be sorted primarily by present value. Records with the same present value are to be sorted by years. Records with the same present value and the same number of years are to be sorted by compounding frequency. All sorts are in ascending order.
Present Nominal Compounding Future Total Value Rate Frequency Years Value Interest APY ---------- ------- ------------ ----- ---------- ---------- ------- 500.00 6.90% Monthly 3 614.63 114.63 7.122% 1000.00 5.25% Semiannually 5 1295.78 295.78 5.319% 1000.00 5.25% Annually 10 1668.10 668.10 5.250% 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% 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 4 10 1500 4.0 4 5 1000 5.25 1 10 2500 3.93 12 8 1000 5.25 2 5 500 6.9 12 3 1000 5.25 12 10
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.