01: #include <iostream>
02: #include <iomanip>
03: #include <cmath>
04: using namespace std;
05:
06: const int BALANCES_ROWS = 11;
07: const int BALANCES_COLS = 6;
08:
09: const double RATE_MIN = 5;
10: const double RATE_MAX = 10;
11: const double RATE_INCR =
12: (RATE_MAX - RATE_MIN) / (BALANCES_ROWS - 1);
13: const int YEAR_MIN = 5;
14: const int YEAR_MAX = 30;
15: const int YEAR_INCR =
16: (YEAR_MAX - YEAR_MIN) / (BALANCES_COLS - 1);
17:
18: /**
19: Prints a table of account balances.
20: @param the table to print
21: @param table_rows the number of rows in the table.
22: */
23: void print_table(const double table[][BALANCES_COLS],
24: int table_rows)
25: { const int WIDTH = 10;
26: cout << setiosflags(ios::fixed) << setprecision(2);
27: for (int i = 0; i < table_rows; i++)
28: { for (int j = 0; j < BALANCES_COLS; j++)
29: cout << setw(WIDTH) << table[i][j];
30: cout << "\n";
31: }
32: }
33: /**
34: Computes the value of an investment with compound interest
35: @param initial_balance the initial value of the investment
36: @param p the interest rate per period in percent
37: @param n the number of periods the investment is held
38: @return the balance after n periods
39: */
40: double future_value(double initial_balance, double p, int n)
41: { double b = initial_balance * pow(1 + p / 100, n);
42: return b;
43: }
44:
45: int main()
46: { double balances[BALANCES_ROWS][BALANCES_COLS];
47: for (int i = 0; i < BALANCES_ROWS; i++)
48: for (int j = 0; j < BALANCES_COLS; j++)
49: balances[i][j] = future_value(10000,
50: RATE_MIN + i * RATE_INCR,
51: YEAR_MIN + j * YEAR_INCR);
52:
53: print_table(balances, BALANCES_ROWS);
54:
55: return 0;
56: }