(
Latest Revision:
Monday March 12, 2012
)
THIRD CS 1500 SOLO PROGRAM
Payout Term of an Annuity
Figuring Out How Long It Will Take For An Annuity To Run Out
NOTE:
No use of global variables is allowed in this program. In other words, all
variables must be declared inside main or another function. (See pages 219-220
of Savitch.)
OBJECTIVE:
Practice top down design, the use of the data type double in
mathematical functions, and appropriate formatting of the output of
double values.
THE ASSIGNMENT:
Write a program that computes the payout term of an annuity.
The idea is that you start with a sum of money S in a special account that is
managed by a big company. Every month the company credits a month's worth of
interest to the account and then sends you a check for a monthly payment P
that you use for your living expenses. The annual interest rate R and the
monthly payment P are fixed constants. Unless P is so small that it is no
more than the monthly interest, the money will eventually run out and there
will be no more payments for you to collect.
Naturally, you want to know how long it will take for the money to run out.
For example, if you start out with S = $257,823 in the account, your monthly
payment is $1554.55, and the interest rate is 4.125 percent per annum, how
many monthly payments can you expect to receive? This program will answer such
questions.
PROGRAM INPUT AND OUTPUT:
The program must begin by writing information to explain the purpose of the
program, and a general description of how to use the program.
The program must then prompt for and input the following three items (in the
following order): the amout S of the initial principal, the amount P of the
monthly payment, and the annual interest rate R. It must print individual
prompts for each of the three quantities.
The principal S and payment P amounts must be entered as fixed-point decimals,
denoting dollars and cents, without a dollar sign or commas, using the decimal
point in the standard manner. For example: the user enters 1554.55 to mean
1554 dollars and 55 cents. The prompts you create for S and P must make these
rules clear to the user of the program.
The interest rate must be entered as a fixed-point decimal number of
percentage points. For example: the user enters 4.125 to mean an annual
interest rate of 4.125%. The prompt you create for the interest rate must
make this rule clear to the user of the program.
Make sure to write the program so that it inputs only
- the amount of the initial principal,
- the amount of the payment, and
- the interest rate.
in that order. If I decide I want to test a program I will want to use
redirection from prepared files of input. That won't work out unless you and
I have this precise agreement on the form (and order) of the input.
After the user enters S, P, and R, the program must check to see whether the
money will run out. If so, the program should compute how many monthly
payments there will be.
Before the program stops, it should inform the user of the results. It should
echo the inputs of the user and then give the user the results of the
calculations it has made.
See
the sample script
to get an idea of how an interaction with the program should appear when the
money does and does not run out.
The number of months (the term of the payout) can be calculated with a formula
comparable to this one:
log(P) - log(P-rS)
Months = ------------------------
log(1+r)
where r = R/1200, the decimal equivalent of the monthly interest rate,
and where it is assumed that rS, which is the amount of the first interest
payment, is less than P. If P <= rS then the money will never run out. (Keep
in mind that log(x) is undefined for x <= 0.)
Of course, the formula above for Months is in a format 'to be understood by
humans,' and does not have C++ syntax. Part of your job as programmer is to
create appropriate C++ expressions to perform the calculations.
This should be pretty easy, because the cmath library includes the log
function (the natural logarithm), and the only other problems are to represent
the multiplication and the division with the appropriate C++ syntax.
After computing the number of months, the program must write a message to the
screen like this:
Here is your answer:
$257823.21
will run out after approximately
245
months, assuming that an annual interest of
4.125%
is earned, compounded monthly, and a monthly payment of
$1555.54
is withdrawn immediately after each interest payment.
Your program is required to format
the numbers for output
as shown above.
The
money
must always appear in
fixed-point
notation, preceded by a
dollar sign,
and with
exactly two digits
displayed after the decimal point. The
interest rate
must be displayed in
fixed-point
notation,
as a percentage,
with
exactly three digits
displayed after the decimal point. The "magic formula" on page 56 of Savitch
describes the formatting commands you will need. Keep in mind that it is the
parameter to cout.precision() that controls the number of digits
after the decimal point. Your program will need statements that change the
precision from 2 to 3 and back to 2 again, in order to format all the numbers
correctly.
DESIGN:
I suggest you design the program according to
this structure chart.
You can write the program by starting with a copy of
this program shell.
To finish the program using my design suggestion, fill in the definitions of
all the functions in the program shell (including "main"). Pay close
attention to the header comments for each function. They explain what each
function is supposed to do.
You do not have to use my design suggestion or program shell. However, I
expect you to employ good principles of top-down design. I will withhold
substantial credit if you do not create a program that uses functions
appropriately. As my design suggestion illustrates, there are plenty of jobs
for individual functions in this program Your program must have at least four
functions, including the main function.
DETAILS:
Put the following lines at the top of your program::
#include <cmath>
#include <iostream>
using namespace std ;
WHAT TO TURN IN:
You will be sending me two e-mail messages. Please follow these
rules:
- Always send me e-mail as plain text in the main message body.
Never send me attachments.
- Always use the exact subject line I specify for each
message. (I often get hundreds of e-mail messages in a week. The
subject line allows me to find and sort messages.) You will lose a
significant number of points on the assignment if you use the wrong
subject line.
- Be very careful when you send the e-mail. You may use the
instructions in your
Hello World! lab exercise
for guidance. Of course, you will need to make the obvious changes to
those directions -- you have to use the correct subject line and
filename.
- Always send yourself a copy of each e-mail message you send to me,
check immediately to see if you receive the message intact, and
check within a few minutes to see if you have received e-mail notifying
you about an undeliverable message. You are
responsible for sending e-mail correctly.
Here is the list of things you have to turn in:
- At the start of class on the
due date
place the following item on the "counter" in front of me:
- a hardcopy (printed listing) of your program (the C++ source code).
Make sure all the code is properly formatted and that it all shows
on the paper.
- Before midnight on the the due
date send me the following by e-mail:
- A copy of the source code (C++ code) with subject line:
CS1500Solo3Source, and
- a (filtered) script showing the results of the program for the
following three sets of inputs
- S == 257823.21, P == 1554.55, R == 4.125
- S == 257823.21, P == 1554.55, R == 8.5
- S == 1000000, P == 5365, R == 5
For the script use the subject line: CS1500Solo3Script .
Note that there are no spaces in the subject lines given. It is important
that you do not insert any spaces. My e-mail address is:
john@ishi.csustan.edu
WHEN IS THIS ASSIGNMENT DUE?
Look for the due date in
the class schedule.
(It's at the top level of the class directory.)