Part II
Write a program that produces a table showing how different interest rates affect the number of
years that will pass before the money runs out. Your program should read in the initial savings
(i.e. x) and the annual spending (i.e. y). If the values entered are unreasonable (either value
is zero or negative, or y is greater than x) or invalid (i.e. total garbage) your program should
output an error message and have the user try again, and repeat this until reasonable values are
obtained. The table should cover interest rates from 2% to 10% (in steps of 0.5%). Each line
should show the interest rate, the number of years until the money runs out, and the amount
remaining in the account when the money runs out. If the money will never run out, the normal
line should be replaced with a message to this effect and any further table lines should be
skipped (because they would, if output, all say the same thing). Do not waste time counting
columns and making your table appear exactly like that in the sample run below. You should,
ECOR 1606 Page 3 of 7 December 2003
ECOR 1606 Page 2 of 7 December 2003
Question #1: (25 marks)
Imagine that somebody is about to retire. They have x dollars in savings, and plan to spend
y dollars every year. At the start of every year (January 1st) they will withdraw y dollars
from their savings account. At the end of every year (December 31st) their bank will pay interest
(at z percent) on the money remaining in the account. The following example should make
things clear.
Suppose x = $100,000, y = $20,000, and z = 5%
Year 1: $20,000 withdrawn, leaving $80,000
interest = 0.05 * $80,000 = $4,000
money at end of year = $84,000
Year 2: $20,000 withdrawn, leaving $64,000
interest = 0.05 * $64,000 = $3,200
money at end of year = $67,200
and so on.
In general, the money will eventually run out. Come some January 1st, the man will find that he
does not have enough money left to get him through the year. Although there may still be some
money left in the account, that amount is less than y. It is possible, however, that the money
will never run out. This will happen if the interest earned every year equals or exceeds the
amount of money spent.
Part I
Write a function that, given x, y, and z, returns the number of full years that will pass
before the money runs out. If the money will never run out, your function should return -1. If
the money will run out, your function should also return the amount of money that will be left
in the saving account when this happens.
You may assume that the values supplied to your function will be reasonable (i.e. that x, y,
and z will all be greater than zero). Do not worry about the fact that monetary amounts may
involve fractional cents.
however, fully demonstrate your knowledge of outputting formatting. Note that dollar amounts
should be output with two digits after the decimal point.
Enter initial savings and annual spending: 500000 -6
*** Unreasonable or invalid values rejected. ***
Enter initial savings and annual spending: total junk
*** Unreasonable or invalid values rejected. ***
Enter the initial savings and annual spending: 500000 30000
Interest Rate Years Money Left
-----------------------------------
2.0 19 29484.49
2.5 21 3905.25
3.0 22 14465.19
3.5 24 3168.54
4.0 26 3708.46
4.5 28 22159.00
5.0 32 10557.61
5.5 37 28405.31
6.0 49 8674.88
6.5 The money will never run out.