CMPT 130 Lab 4 - Functions

 

In this lab you are going to use functions to calculate and print compound interest on a loan. The lab is broken down into four parts.

 

Labs are assessed so make sure that the TA has seen, and marked, your finished work before you leave the lab.

 

Introduction

You are going to write a program that calculates the compound interest (where interest is calculated on the principal plus previous interest on the loan) on a loan. Your program is going to consist of a main function and two other functions, one to calculate the compound interest amount and one to print the results of your calculations.

 

Sample Output

Enter the amount of the loan: 12000

Enter the interset rate as a value between 0 and 1: .1

Enter the term of the loan in years: 3

 

Loan Principal: 12000

Loan Rate: 0.1

Loan Term: 3

Compound Interest: 3972

User input is shown in green.

 

Part 1 – main Function

As usual your program should include the iostream library and use the standard namespace (using namespace std;). Under these lines, write a main function that prompts the user to enter input for the loan amount (known as the principal), the interest rate, and the term in years. The first two values should be floats, and the third an integer. These values should be assigned to variable with appropriate names (I'd suggest loan, rate and term). Due to time constraints we are not going to perform any input checking. This is something you can do later if you want some practice.

 

Refer to the sample output for what to print to prompt the user to enter input.

 

I would suggest testing your main function by adding a line of output that prints back your input values before moving on to part 2.

 

Part 2 – Compound Interest Function Stub

At this point you are going to write what is referred to as a stub function. This is a function that has the correct function header (return type and parameter list) and that returns a value of the correct type but that doesn't actually do anything – that is, it doesn't calculate whatever value it is meant to. This isn't strictly necessary for the program we are writing but it is a useful technique that allows you to use your stub in the rest of a program (albeit not returning the correct value) and then come back and complete it later. Which is exactly what you will do in parts 3 and 4 of the lab.

 

Function Prototype

First write a function prototype for the compound interest function between your include statements and the main function. The compound interest function needs to return an integer, and needs parameters for the loan amount, the interest rate and the term. It should look like this:

 

int compoundInterest(float loan, float rate, int term);

 

Function Definition

Now write the function definition below the main function. This will consist of the function header (given above) without the semi-colon and the body of the function in {}s. for now, as we are only writing a stub, the body should just consist of a return statement that returns the value zero (return 0;).

 

Part 3 – Display Loan Details Function

Now you've written your stub function you can write the second function. This function should also have a prototype above the main function and a definition below the main function. This function is responsible for printing the output, and for calling the compound interest function to calculate the interest. Like the compound interest function, it has parameters for the loan amount, the interest rate and the loan term. Note that you can give these parameters the same names as your other function as they have different scope. Unlike the compound interest function the display loan details function is not going to return anything, so should have the void return type.

 

The function should just consist of a series of cout statements that display the output shown in the sample at the start of the lab. One of these cout statements will include a function call to your compound interest function. When you call your compound interest function its arguments should be the parameters of your display loan function, like this:

 

cout << compoundInterest(loan, rate, term);

 

Once you've defined your display loan details function, call it in your main function (below the statements that get input) and give it your input variables as arguments. You can now test your entire program which should be complete except that the interest value will always be zero, since you haven't completed your compound interest function yet.

 

Part 4 – Compound Interest Function Stub

The only task left to do is to write the body of your compound interest function. There are a couple of ways you can do this, either of which are fine. The first is to write a loop that does this (written in pseudocode):

 

total = loan (i.e. the principal)

for each year

total = total + total*rate

repeat

return total - loan

 

The quicker alternative is to just use a formula. This involves the pow function so you will need to include the cmath library.

 

interest = principal * (1 + rate)term - principal

 

Assessment

1 mark for completion of at least the first three parts of the lab. If you don't have time to implement part 4 please do so on your own time.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)