header

Lab 9

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

How, you might ask, does this help? Consider what happens when the original list is sorted first by years and then by present value:

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

Notice that in the final list, investments with the same present value are now sorted by the number of years.

In order to meet the sorting specifications given earlier, you must first sort the list of records by compounding frequency then sort the list by years and then, finally, sort the list by present value. Consequently, you will need to implement three sorting functions that are identical except that they sort on different fields; one sorts by compounding frequency, one by years, and one by present value.

Output Specifications

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%

Input Specifications

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

Process Specifications

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

Miscellaneous

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.