Question

In: Computer Science

In part 2 you will be creating multiple functions to calculate the present value. You may...

In part 2 you will be creating multiple functions to calculate the present value.

You may be asking what a "present value" is. Suppose you want to deposit a certain amount of money into a savings account and then leave it alone to draw interest for some amount of time, say 12 years. At the end of the 12 years you want to have $15,000 in the account. The present value is the amount of money you would have to deposit today to have $15,000 in 12 years.

The formula used needs the future value (F) and annual interest rate (r) and the number of years (n) the money will sit in the account, 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 to the nth power. Assume that ^ is the power function and x^2 is x to the 2nd power (x squared)

You are not allowed to use any global variables. Use of global variables will result in a grade of zero for part 2.

Three read functions

You must have functions to read in the future value, the annual interest rate, and the number of years. That would be three different functions. Give these functions meaningful names. Note that the order of the values will be future value, annual interest rate, and number of years.

In all cases you need to return any valid value back to the calling function.

For all three functions you will write out to cout as prompt for an input value. You will read in that value from cin. If the value is invalid (zero or negative) you need to display an error message and reread the value (with another prompt). You need to do this in a 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 of type double.

Here are the prompts for the three values you need to read in:

Enter future value
Enter annual interest rate
Enter 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 will need to divide these values by 100 to convert them into the values needed in the function (.1 and .125 for the above values). This conversion needs to be done before you call the presentValue function (see below). If you do the conversion in the presentValue function your program will fail the unit tests, so do the conversion before you call the calculate function.

Here are the error messages you need to display if the values are negative:

The future value must be greater than zero
The annual interest rate must be greater than zero
The number of years must be greater than zero

You will also need a function called presentValue with the following signature:

double presentValue(double futureValue, double interestRate, int numberYears)

The presentValue needs to calculate the present value and return that 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, future value, interest rate, and number of years. The function needs to display the interest rate as %, so .05 would display as 5.000%. Give your display function a meaningful name. You will be passing a number of values to this function.

Here is the sample output for a present value of $69,046.56, a future value of $100,000, an interest rate of 2.5% and a number of years of 15,

Present value: $69046.56
Future value: $100000.00
Annual interest rate: 2.500%
Years: 15

Note that the present value and future value have three digits of precision to the right of the decimal point but the interest rate 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 for future value, annual interest rate and number of years.

Get the values for these by calling the read functions you created above.

Once you have the values you need to call your presentValue. Using the result from the presentValue and the input values you read in with your read functions you need to call your display function (written above) to display the values.

The main function

The main function will be the driver for your program. All of the non-main functions are called from main.

For the following sample run assume the input is as follows:

1000000.0
5.0
25

Your program should output the following:

Enter future value
Enter annual interest rate
Enter number of years
Present value: $295302.77
Future value: $1000000.00
Annual interest rate: 5.000%
Years: 25

Here is an example with some invalid data

Input values:

-100
0
1000000.0
5.0
25

Output:

Enter future value
The future value must be greater than zero
Enter future value
The future value must be greater than zero
Enter future value
Enter annual interest rate
Enter number of years
Present value: $295302.77
Future value: $1000000.00
Annual interest rate: 5.000%
Years: 25

Failure to follow the requirements for lab lessons can result in deductions to your points, even if you pass the validation tests. Logic errors, where you are not actually implementing the correct behavior, can result in reductions even if the test cases happen to return valid answers. This will be true for this and all future lab lessons.

Expected output

There are eight tests. The first test will run your program with input and check your output to make sure it matches what is expected. The next three tests are unit tests. The unit tests will directly call the presentValue function. The compilation of the unit test could fail if your presentValue function does not have the required signature. The final four tests will run your program with various input values and make sure you are calculating the correct answers.

You will get yellow highlighted text when you run the tests if your output is not what is expected. This can be because you are not getting the correct result. It could also be because your formatting does not match what is required. The checking that zyBooks does is very exacting and you must match it exactly. More information about what the yellow highlighting means can be found in course "How to use zyBooks" - especially section "1.4 zyLab basics".

Finally, do not include a system("pause"); statement in your program. This will cause your verification steps to fail.

Note: that the system("pause"); command runs the pause command on the computer where the program is running. The pause command is a Windows command. Your program will be run on a server in the cloud. The cloud server may be running a different operating system (such as Linux).

Error message "Could not find main function"

Now that we are using functions some of the tests are unit tests. In the unit tests the zyBooks environment will call one or more 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 function
int main()

Solutions

Expert Solution

Thanks for the question, Here is the complete code in C++.

I want to clear one thing here, the question says to have present value and future value to 3 precision and rate in 1 decimal precision but if you need the sample outputs in the question pv and fv are in 2 decimals and rate is showing with 3 decimals. Please get this clarified with your professor.

thank You. Let me know for any changes, no need to post another question.

==============================================================================

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;



double
getFutureValue ()
{
  

double fv = 0;
  
do
{
  
cout << "Enter future value: ";
  
cin >> fv;
  
if (fv <= 0)
   {
  
cout << "The future value must be greater than zero\n";
  
}
  

}
while (fv <= 0);
  
return fv;

}



int
getNumberOfYears ()
{
  
int years = 0;
  
do
{
  
cout << "Enter number of years: ";
  
cin >> years;
  
if (years <= 0)
   {
  
cout << "The number of years must be greater than zero\n";
  
}
  

}
while (years <= 0);
  
return years;

}



double
getInterestRate ()
{
  

double rate = 0;
  
do
{
  
cout << "Enter annual interest rate: ";
  
cin >> rate;
  
if (rate <= 0)
   {
  
cout << "The annual interest rate must be greater than zero\n";
  
}
  

}
while (rate <= 0);
  
return rate;

}






double
presentValue (double futureValue, double interestRate, int numberYears)
{
  
interestRate = interestRate / 100;
  
double pv = futureValue / (pow (1 + interestRate, numberYears));

}

double

display (double futureValue, double interestRate, int numberYears,
   double presentValue)
{
  
cout << fixed << setprecision (3) << showpoint;
  
//cout<<endl;
cout << "Present value: $" << presentValue << endl;
  
cout << "Future value: $" << futureValue << endl;
  
cout << fixed << setprecision (1) << showpoint;
  
cout << "Annual interest rate: " << interestRate << "%" << endl;
  
cout << "Years: " << numberYears << endl;

}


int

main ()
{
  

double fv, rate, pv;
  
int years;
  

fv = getFutureValue ();
  
rate = getInterestRate ();
  
years = getNumberOfYears ();
  

pv = presentValue (fv, rate, years);
  
display (fv, rate, years, pv);


}

thanks !


Related Solutions

9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
Present value For the case shown in the following​ table, calculate the present value of the...
Present value For the case shown in the following​ table, calculate the present value of the cash​ flow, discounting at the rate given,and assuming that the cash flow is received at the end of the period noted. ​(Click on the icon here in order to copy the contents of the data table below into a​ spreadsheet.) Single cash flow- $148,000 discount rate- 13% end of period (years) - 7
How do you calculate Profit Present Value in excel?
How do you calculate Profit Present Value in excel?
In this paper you will be working with a dollar value of $1,000,000 to calculate present...
In this paper you will be working with a dollar value of $1,000,000 to calculate present value, present value of an annuity, future value and future value of an annuity. In each of the four parts of the assignment, you will provide a scenario in which you personally or a business would need to calculate these values. Personal examples are planning for retirement, planning for education or winning the lottery. Business examples are buying a new delivery truck, bond issues,...
Part A Calculate the pH in 1.00MCH3CO2H. Part B Calculate the concentrations of all species present...
Part A Calculate the pH in 1.00MCH3CO2H. Part B Calculate the concentrations of all species present (H3O+, CH3CO2?, CH3CO2H, and OH?) in 1.00M CH3CO2H.
When you calculate the present value of an asset, for example a bond, you are calculating...
When you calculate the present value of an asset, for example a bond, you are calculating _____. maximum price you would pay for the asset minimumm price you would pay for the asset future value of the asset insurance premiums
Net present value Using a cost of capital of 11​%, calculate the net present value for...
Net present value Using a cost of capital of 11​%, calculate the net present value for the project shown in the following table and indicate whether it is​ acceptable, Initial investment ​(CF 0CF0​) negative 1 comma 143 comma 000−1,143,000 Year ​(t​) Cash inflows ​(CF Subscript tCFt​) 1 $81,000 2 $138,000 3 $193,000 4 $258,000 5 $311,000 6 $377,000 7 $270,000 8 $98,000 9 $45,000 10 $29,000 The net present value​ (NPV) of the project is _____​$ ​(Round to the nearest​...
For each example, calculate the present value, or net present value, of the future amount(s) to...
For each example, calculate the present value, or net present value, of the future amount(s) to support your answer and show your work using either factors (pp. 219 & 221 in the text), an Excel spreadsheet with the Excel PV or NPV functions or the equations, such as PV = FV / (1+Interest Rate)Time. Suppose you have a project where you will invest $20,000 today and receive one payment $26,200 exactly 5 years in the future. If your opportunity cost...
(For this part, you MUST present sufficient solution steps, and MUST apply specific Excel functions =NPV(…),...
(For this part, you MUST present sufficient solution steps, and MUST apply specific Excel functions =NPV(…), =IRR(…), =AVERAGE(…), =YIELD(…) whenever applicable). Please show excel formulas. Given the following information for Bajor Co.: Debt: Bajor’s long-term debt capital consists of bonds with 6.250 percent coupon rate (semiannual coupon payments), 9 years time to maturity, and current price of 106.61 percent of its par value (i.e., price = 106.61 relative to full amount redemption par of 100). Preferred stock: Bajor has not...
(For this part, you MUST present sufficient solution steps, and MUST apply specific Excel functions =PV(…),...
(For this part, you MUST present sufficient solution steps, and MUST apply specific Excel functions =PV(…), =FV(…), =PMT(…), =NPER(…), =RATE(…), =PRICE(…) or =YIELD(…) whenever applicable. Please show me the EXCEL functions that was used to help me better understand was equals what. Using Excel finance formulas You apply for a 20-year, fixed-rate (APR 6.48%) monthly-payment-required mortgage loan for a house selling for $150,000 today. Your bank requires 22% initial down payment of house value (to be paid in cash immediately),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT