Create an example function definition of a function that takes…
Customer Question
Create an example function definition...
Create an example function definition of a function that takes at least two parameters. At least one of the parameters should be passed by value, and at least one should be passed by reference.
Then show a sample call to your function, along with the declaration of any constants/variables used by the function call. You do not need to write an entire program.
All functions you write will be prototyped. So create prototypes for all of your user-defined functions. The prototypes should be placed above main, and the function definitions should be placed below main.
Program #1
Given the following recursive function, answer the two sub-problems below.
float wk7recur (float value, int num)
{
if (num == 0)
return 1;
else
return ( value * wk7recur (value, num - 1) );
}
Sub-Problem 1:
What would the value of answer be for each of the following? Calculate your answers by hand. You will need to report ALL intermediate answers for each level of recursion (NOT just the final result).
a) float answer = wk7recur (2.0, 4);
b) float answer = wk7recur (3.0, 3);
c) float answer = wk7recur (1.0, 5);
Sub-Problem 2:
Figure out what computation is being performed by the wk7recur function.
What is the name of the math computation performed by this function?
Place the answers to sub-problems 1a-c & 2 in the top of program comments for this program (be sure to show intermediate answers for each level of recursion).
After you complete the subproblems, you will write a program that includes both the recursive version of the function wk7recur (from above) and an iterative version of the function (which you will write) called wk7loop.
The wk7loop function should use iteration (looping) instead of recursion to do the same computation as the wk7recur function.
The main function should first explain to the user what the program will do.
Then it should prompt the user for the floating point number, fnum, and the integer inum, and error check them as follows:
fnum may be any value, positive or negative, but a 0 value will indicate the user wants to exit.
inum may be any positive integer (including 0).
If the user does NOT enter 0 for fnum, then call BOTH of the functions, using fnum and inum as parameters.
The results of both function calls should be printed out to 3 decimal places, with descriptive text. Note that the results should be the same!
Sample Output:
Recursive result = 4.840
Iterative result = 4.840
The only way the user may exit the program is to enter 0 for the value of fnum -- do not ask the user whether to run the program again.
The program should LOOP and continue to compute results for new values until the user enters 0 for the value of fnum. When the user enters 0 for fnum, the program should not ask for the value of inum – it should just exit.
Program #2
Write a modular financial calculator program. Make sure to use the following:
• The indentation and spacing practices introduced in the reading
• double for all floating point values
• Constants for all fixed values
• Prototypes for all user-defined functions
The program should first provide a menu for the user to select what s/he wants to do, with Exit as one of the choices.
The user will be able to choose to do loan or savings calculations, using one of the following four formulas:
• Compute a Loan Payment for a given Loan Amount to be borrowed:
MonthlyPayment = (factor * monthlyInterestRate * LoanAmount) / (factor - 1);
• Compute the Loan Amount you could borrow for a given affordable Monthly Payment:
LoanAmount = ( (factor - 1) * monthlyPayment ) / (factor * monthlyInterestRate);
• Compute the Total amount that will be accumulated for a given Monthly Amount saved:
Total = ( (factor - 1) * monthlyAmount ) / monthlyInterestRate;
• Compute Monthly Amount required to accumulate a given Total amount:
monthlyAmount = ( Total * monthlyInterestRate ) / ( factor - 1 );
For all of these choices, the user will enter an annual interest rate (e.g. 7.25 for 7.25%), and a number of years to borrow or save (e.g. 3.5 for 3½ years), and a monetary amount.
Input Error Checking
Error check the following user input values:
• Yearly interest rate must be between 1% and 30%.
• The number of years to borrow or save must be between 1 year and 50 years.
• Monetary amounts must be positive and non-zero, and must be no more than $ 9,999,999.99.
The program will need to convert the annual interest rate to a monthly interest rate (divide by 100 to get a fractional interest rate, and then divide that by 12 for monthly interest) before using it in the formulas.
All of the formulas also require that you first calculate a factor and then use it in your formula. The factor formula is:
factor = exp( numberOfMonths * log(1 + monthlyInterestRate));
Note that exp and log are predefined mathematical functions, located in the C++ library.
For hand calculating results to test your program, the equivalent algebraic equation is:
Factor = (1 + MonthlyInterestRate) TermInMonths
Output
Calculate and display the results, including the information the user input in the following format:
Sample output (for both Loan calculations):
Sample output (for both Savings calculations):
Total Annual Monthly
Amount Number Interest Savings
Saved of Years Rate Required
------------ ---------- -------- ----------
$ 100000.00 7.5 years 7.500% $ 831.13
Note that dollar amounts are calculated to the nearest penny (i.e. rounded to 2 decimal places).
Years should be displayed to one decimal place, and the annual interest rate to 3 decimal places.
After displaying the results, the program should pause and then return to the menu to let the user choose another formula or choose to exit. The only way the user may exit the program is to choose to exit from the menu -- do not ask the user whether to run the program again.
So each time a formula is chosen from the menu, the program will read new inputs, display the results, and loop back to the menu -- until the user chooses to exit.
This program should be modular. You must include the following functions:
• A function to read and validate the interest rate. Pass back both the yearly interest rate and the monthly interest rate, using reference parameters.
• A function to read and validate the number of years to borrow or save. Pass back both the term in years and the term in months, using reference parameters.
• A function to read and validate a dollar amount.
• A function to display the menu and read and validate the user's choice.
• A function to calculate and return the factor, called by a function other than main.
• One or more functions to compute the results.
• Only one or two functions to output the results.
• Each function should perform only one logical task. When breaking down the code into function, look for similarities in the code. Use the functions to minimize the amount of code you have to write.
•
•
• Program and Function Documentation (both programs)
•
• 1) Be sure to include top of program comments as specified in WBW section 1.8.
•
• 2) Make sure you comment any constants/variables whose names are XXXXX XXXXX descriptive.
•
• 3) You must also include a comment above each user-defined function that includes:
•
• What the function does
• Description of the input parameters (if any), used to pass values into the function
• Description of the output parameters (if any), used to pass values back from the function
• Description of what is returned from the function via the return statement (if anything)
Submitted: 12 years ago.Category: ProgrammingShow More
Show Less
Ask Your Own Programming Question