Program 3

Home Windows C++ 241 C++ 242 Contact Info Contents

Program 3 - January 24

Up

Writing and Using Functions

For some kinds of programs, it would be handy to have a set of financial functions to do the most common calculations. For this assignment you will write several such functions and a main program that tests them.

Main Program

Your main program should ask the user to solve one of two different kinds of problems. For each kind of problem, the user will be asked for certain information and then you will call the appropriate function and display the result.

  1. How much an investment will be worth in the future. User must give you the amount invested, number of periods and interest rate. You will call the FutureValue function to get an answer.

  2. How much needs to be invested now to get a certain amount in the future. User must give you the amount desired, number of periods and interest rate. You will call the PresentValue function to get an answer.

Financial Functions

The prototypes for your functions are as follows:

double FutureValue(double amount, unsigned num_periods, double rate);
double PresentValue(double amount, unsigned num_periods, double rate);

You should include these prototypes in a separate header (.h) file and include that header in your main program. The header file should include comments that would help another programmer to use your functions. (For example, the is the rate in your function given as a percent or a decimal fraction?)

The definitions of the functions should be included in a source (.cpp) file separate from your main program. You must add this file to your project in Visual Studio using the Project menu (or just try to compile it and say yes when asked if it should be added).

Note: You must do this as described. Learning to organize a program in multiple files is an important part of this assignment.

Perhaps you are a bit unsure of the formulas for calculating these amounts. There are a number of ways to do it, but the following pseudocode will work for this assignment:

FutureValue(amount, num_periods, rate)
    Multiply the amount by (1.0 + rate)  num_periods times

PresentValue(amount, num_periods, rate)
    Divide the amount by (1.0 + rate) num_periods times
        or for better accuracy:
    Multiply 1.0 by (1.0 + rate) num_periods times, then
    Divide amount by the result

Extra Credit

If this is all new to you, the above will be plenty. However, some of you may be bored by this. In that case, add two more functions to your main program:

  1. How much a periodic investment will be worth in the future. User must give you the amount per period, number of periods and interest rate. Write a separate function to calculate the answer.

Hint: You can do this by looping through all the periods adding the periodic investment and calculating the interest each time. The answer is different depending on whether you invest at the start of the period or the end, but you can do it either way for this assignment.
  1. How much do must be invested each perod to get a certain amount in the future. User must give you the desired future amount, number of periods and rate of interest. Write a separate function to calculate the answer.

Hint: Figure out how much one dollar each period be worth at the end by calling the function for item 3. The answer is the ratio by which any amount would grow. Divide that ratio into the amount you want to have to get the amount you need to invest each month.

Up

Home Windows C++ 241 C++ 242 Contact Info Contents

Copyright © 2000  Charlie Poole. All rights reserved.
Revised: July 15, 2002 - cpoole@ctc.edu