In part 2 you will be creating multiple functions to calculatethe present value.
You may be asking what a \"present value\" is. Suppose you want todeposit a certain amount of money into a savings account and thenleave it alone to draw interest for some amount of time, say 12years. At the end of the 12 years you want to have $15,000 in theaccount. The present value is the amount of money you would have todeposit today to have $15,000 in 12 years.
The formula used needs the future value (F) and annual interestrate (r) and the number of years (n) the money will sit in theaccount, unchanged. You will be calculating the present value(P).
P = F / ( (1 + r) ^ n)
In the above expression the value (1 + r) needs to be raised tothe nth power. Assume that ^ is the power function and x^2 is x tothe 2nd power (x squared)
You are not allowed to use any global variables. Use ofglobal variables will result in a grade of zero for part2.
Three read functions
You must have functions to read in the future value, the annualinterest rate, and the number of years. That would be threedifferent functions. Give these functions meaningful names. Notethat the order of the values will be future value, annual interestrate, and number of years.
In all cases you need to return any valid value back to thecalling function.
For all three functions you will write out to cout as prompt foran input value. You will read in that value from cin. If the valueis invalid (zero or negative) you need to display an error messageand reread the value (with another prompt). You need to do this ina loop and continue looping until a valid value has been entered.Only the number of years can be an int value. The rest should be oftype double.
Here are the prompts for the three values you need to readin:
Enter future valueEnter annual interest rateEnter number of years
Note that the interest rate will be a number such as 10 or 12.5.These are to be read in as percentages (10% and 12.5%). You willneed to divide these values by 100 to convert them into the valuesneeded in the function (.1 and .125 for the above values). Thisconversion needs to be done before you call the presentValuefunction (see below). If you do the conversion in the presentValuefunction your program will fail the unit tests, so do theconversion before you call the calculate function.
Here are the error messages you need to display if the valuesare negative:
The future value must be greater than zeroThe annual interest rate must be greater than zeroThe number of years must be greater than zero
You will also need a function called presentValue with thefollowing signature:
double presentValue(double futureValue, double interestRate, int numberYears)
The presentValue needs to calculate the present value and returnthat back to the calling function. The formula for this is above.Note that the annual interest would be .08 for 8%.
The display function
You also need a function that displays the present value, futurevalue, interest rate, and number of years. The function needs todisplay the interest rate as %, so .05 would display as 5.000%.Give your display function a meaningful name. You will be passing anumber of values to this function.
Here is the sample output for a present value of $69,046.56, afuture value of $100,000, an interest rate of 2.5% and a number ofyears of 15,
Present value: $69046.56Future value: $100000.00Annual interest rate: 2.500%Years: 15
Note that the present value and future value have three digitsof precision to the right of the decimal point but the interestrate only has one digit to the right of the decimal point.
The main function will be the driver for your program.
Your program will only be processing one set of valid values forfuture value, annual interest rate and number of years.
Get the values for these by calling the read functions youcreated above.
Once you have the values you need to call your presentValue.Using the result from the presentValue and the input values youread in with your read functions you need to call your displayfunction (written above) to display the values.
The main function
The main function will be the driver for your program. All ofthe non-main functions are called from main.
For the following sample run assume the input is as follows:
1000000.05.025
Your program should output the following:
Enter future valueEnter annual interest rateEnter number of yearsPresent value: $295302.77Future value: $1000000.00Annual interest rate: 5.000%Years: 25
Here is an example with some invalid data
Input values:
-10001000000.05.025
Output:
Enter future valueThe future value must be greater than zeroEnter future valueThe future value must be greater than zeroEnter future valueEnter annual interest rateEnter number of yearsPresent value: $295302.77Future value: $1000000.00Annual interest rate: 5.000%Years: 25
Failure to follow the requirements for lab lessons canresult in deductions to your points, even if you pass thevalidation tests. Logic errors, where you are not actuallyimplementing the correct behavior, can result in reductions even ifthe test cases happen to return valid answers. This will be truefor this and all future lab lessons.
Expected output
There are eight tests. The first test will run your program withinput and check your output to make sure it matches what isexpected. The next three tests are unit tests. The unit tests willdirectly call the presentValue function. The compilation of theunit test could fail if your presentValue function does not havethe required signature. The final four tests will run your programwith various input values and make sure you are calculating thecorrect answers.
You will get yellow highlighted text when you run the tests ifyour output is not what is expected. This can be because you arenot getting the correct result. It could also be because yourformatting does not match what is required. The checking thatzyBooks does is very exacting and you must match it exactly. Moreinformation about what the yellow highlighting means can be foundin course \"How to use zyBooks\" - especially section \"1.4 zyLabbasics\".
Finally, do not include a system(\"pause\");statement in your program. This will cause your verification stepsto fail.
Note: that the system(\"pause\"); command runs the pausecommand on the computer where the program is running. The pausecommand is a Windows command. Your program will berun on a server in the cloud. The cloud server may be running adifferent operating system (such as Linux).
Error message \"Could not find main function\"
Now that we are using functions some of the tests are unittests. In the unit tests the zyBooks environment will call one ormore of your functions directly.
To do this it has to find your main function.
Right now zyBooks has a problem with this when your int main()statement has a comment on it.
For example:
If your main looks as follows:
int main() // main function
You will get an error message:
Could not find main function
You need to change your code to:
// main functionint main()