(
Latest Revision:
Saturday March 21, 2015
)
FOURTH CS 1500 SOLO PROGRAM
Loan Payment Calculator
Figuring Out How Much The Monthly
Payment on a Loan Should Be
NOTE:
No global variables are allowed in this program.
In other words, all variables must be declared inside main
or another function.
(See pages 221-224 of the ninth edition 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 amount of the monthly payment on a loan.
The idea is that someone borrows L dollars at an annual interest rate of R, and
opts to pay off the loan in M monthly payments. The program's job is to figure
out how much the borrower has to pay each month in order to pay off the loan in
exactly M months.
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 amount of the loan, the annual interest rate,
and the number of monthly payments. It must print individual prompts for each
of the three quantities.
The amount of the loan must be entered as a fixed-point decimal, denoting
dollars and cents, without a dollar sign or commas, using the decimal
point in the standard manner when the amount is not a whole number of
dollars. For example: the user enters 1543.89 to mean 1543 dollars and 89
cents, or 4800 to mean 4800 dollars. The prompt you create for the
loan amount must make these rules clear to the user of the program.
The interest rate must be entered as a fixed-point 'decimal equivalent' to
a percentage. For example: the user enters 0.08125 to mean an
annual interest rate of 8.125%. The prompt you create for the interest
must make this rule clear to the user of the program.
The number of monthly payments must be entered as a positive integer.
For example: the user enters 60 to mean sixty monthly payments.
The prompt you create for the number of monthly payments 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 loan,
- the interest rate, and
- the number of payments,
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.
You may assume that all numbers entered by the user will be positive. If you wish, you can write the program to check for bad (non-positive) input. If there's bad input, the program should just write an error message and stop.
After the user enters the input (loan amount L, interest rate R, and
number of monthly payments M), the program must compute the amount of
the monthly payment by using these (pseudo-code) formulae:
NthPower = (1+(R/12))N
ratio = NthPower / (NthPower - 1)
P = (R*L/12)*ratio
Please notice that the formulae above are pseudo-code.
You will need to make some changes in order to use them in a C++ program.
If you have done your reading and studying correctly, you should know what to do.
If not, ask me.
Note that your program can use the pow function to calculate the
power: (1+(R/12))N
For example 32 is pow( 3, 2 ), which equals 9,
and 25 is pow( 2, 5 ), which equals 32.
The output of the program must tell
- what the input was,
- what the calculated amount of the payment is, and also
- what the borrower will pay in interest and principal, as well as the grand total.
Ideas regarding 3 above:
- The principal is just one of the inputs of the user.
- Once the payment is known, the total amount paid by the borrower
is easily calculated as P*M, which is the amount of the payment
times the number of payments.
- The interest is also easily calculated, because it is just the
difference between the total amount paid and the amount of the loan, P*M-L.
For example, if the user enters 256000 for the amount of
the loan, 0.038 as the interest rate, and 240 as the number of months, then
your program should output this:
Here is your answer:
For a loan of $256000.00, at interest rate 3.800%,
paid out over 240 months,
the amount of the payment must be: $1524.46
The amount of interest paid is $109871.32.
The principal paid is $256000.00.
The total paid is $365871.32.
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 program will have to output 100*R, not R.)
The "magic" formatting directives
in the box at the bottom of page 56 of the ninth edition of Savitch
are the 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.
You can examine a sample script of a program run
here.
Use it as a guide for writing the prompts
and other output statements of your program.
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 skeleton (shell).
To write the program using my design suggestion, fill in the definitions of
all the functions in the program skeleton (including "main").
Pay close attention to the header comments for each function.
They explain what each
function is supposed to do. If you want to rely on my design suggestion, then you have to conform to the directions in the header comments. You have to write the code for the functions in the skeleton exactly as the comments describe. The functions must do exactly what the comments say they do, no more and no less.
You do not have to use my design suggestion or program skeleton. 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 ;
TESTING:
For the test script you will duplicate the tests on the sample runs.
You can check results on other sets of inputs by using an online payment calculator,
such as
this one.
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. (Each line of code must be less than 79
characters in length.)
- 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:
CS1500Solo4Source, and
- a (filtered) script showing the results of the program for the
three sets of inputs tested in the
sample script.
For the script use the subject line: CS1500Solo4Script .
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.)