Please read before submitting:
All work that you submit in this class must be entirely your own. In particular:
The purpose of this assignment is to make sure that you know how to write a program that contains functions that meet specific requirements. WARNING: Your work will receive no points if you solve the problem without implementing the three required C++ functions listed below (two versions of a C++ new_balance function and one version of a C++ years_needed function). Moreover, your functions must work correctly for any valid arguments (not just for the particular numbers that your main program uses).
For the assignment, write a program that an investor can use to compute future bank balances. The program asks the user for two pieces of information: The amount of an initial deposit (entered as a double number in dollars, such as 42.31 for 42 dollars and 31 cents), and an interest rate (entered as a double number, such at 0.05 for 5% interest). The program prints the results of three computations, each of which assumes that interest is paid once per year on the amount in the account at the year's start: (1) The balance in the account after one year of interest; (2) The balance in the account after ten years of compounding interest; (3) The number of years required for the balance to reach at least one million dollars. The account balances should be printed with a dollar sign in front and rounded to two decimal places (such as $42.31). In order to get numbers to print with two decimals, include both <iostream.h> and <iomanip.h> and place these lines in your main function (just after you declare any local variables that the main function uses):
cout.precision(DIGITS); cout.setf(ios::fixed, ios::floatfield);
Your program must include functions with these exact prototypes:
double new_balance(double initial_balance, double interest_rate); // This first version of the new_balance function computes the // final account balance that will be reached by starting with // an initial balance and adding one year's interest at a given // interest rate (such as 0.05 for 5%). The function works // correctly for any non-negative arguments. double new_balance(double initial_balance, double interst_rate, int n); // This second version of the new_balance function computes the // final account balance that will be reached by starting with // an initial balance and adding n years of interest at a given // interest rate (such as 0.05 for 5%). The interest is added // once per year and it is applied to the entire balance. // For example, new_balance(100.00, 0.10, 2) is 121.00 since // the first year received 10.00 interest (10% of 100) and the // second year received 11.00 interest (10% of the 110, which // was the second year's starting balance). The function works // correctly for any non-negative arguments. int years_needed(double initial_balance, double interest_rate, double goal); // The years_needed function computes the number of years needed for // a given starting balance to reach a given goal at a certain rate // of compount interest. The function works correctly for any // positive arguments.
Important Note: Your program must include the three functions show above in this exact format. Dora will actually test your program by throwing away your main program and using her own main program that calls the functions with lots of diferent numbers.
Make sure that your programs follow good documentation standards and have the same information required for the first homework assignment. The specific style guidelines that the graders will use is posted at http://www.cs.colorado.edu/~main/intro/style.html . However, for this assignment, the graders will not yet require NMR or NGE.
Submit your .cxx file as Homework 2 to www.cs.colorado.edu/~main/dora/
In the second version of
the balance after n years should be computed with the following formula:
initial_balance * ((1+interest_rate) raised to the n power)Use the pow(x,b) function from <math.h> to compute the value of a number x raised to the b power.
years_needed function can be implemented by
using logarithms. But if you are unfamiliar with logarithms, another
approach is to repeatedly call
new_balance with higher and
higher years, until you get an answer that is at least equal to the
goal. If you use this second appraoch, then start by calling
new_balance with n=0, then n=1, then n=2, and so on.
When you get a return value that is at least equal to the goal,
then your answer is the current value of n.