Question: Program 6 Functions (100 points) Overview: For this assignment, re-write program 5 so that it uses functions to display the menu, get values from the
Program 6 Functions (100 points)
Overview: For this assignment, re-write program 5 so that it uses functions to display the menu, get values from the user, and perform the various arithmetic calculations.
Limits: One thing that will be different for this version of the assignment is that there are limits on the values that will be used in the various calculations.
When finding the larger of two real numbers, the two numbers should be between -999.9 and 999.9
When determining if one integer number is divisible by another integer number, the dividend should be between 0 and 1000 while the divisor should be between 1 and 50.
When raising a real number (M) to an integer power (N) - M to the Nth power - the M value should be between 1.0 and 100.0 while the N value should be between 1 and 10.
When calculating the factorial of an integer number, the number should be between 0 and 12.
Basic Program Logic: Note: all of the functions that are mentioned in the following logic are described in the "The Functions" section of this assignment write-up.
The program should start by calling the menu() function to display a menu to the user and get their choice of which arithmetic operation to perform.
In a loop that executes as long as the user does not want to quit, use a cascading decision statement to check the value entered by the user and perform the appropriate operation.
If the user chose to find the larger of two floating point numbers, call the getDouble() function two times to get two floating point values to be compared, call the findLarger() function, passing the two floating point values that were returned from getDouble, to compare the values, and display the result in main().
If the user chose to determine if one integer is divisible by another integer, call the getInt() function two times to get the dividend and divisor, call the isDivisible() function, passing the two integers that were returned from getInt, to determine if the dividend is divisible by the divisor, and display the result in main().
If the user chose to calculate the value of raising a floating point value (M value) to an integer power (N value), call the getDouble() function to get the M value, call the getInt() function to get the N value, call the MtoN() function, passing the values returned from getDouble and getInt, to calculate the result of raising the M value to the Nth power, and display the result in main().
If the user chose to calculate a factorial, call the getInt() function to get an integer value, call the calcFactorial() function, passing the value returned from getInt, to calculate the factorial of the integer value, and display the result in main().
The loop should finish by calling the menu() function to display the menu to the user and get their next choice of which arithmetic operation to perform.
The Functions: Write and use the following 7 functions in the program.
char menu(): This function will display the menu of possible arithmatic operations to the user and get their choice. It takes no argument. It returns a character: the user's choice from the menu.
This function should contain a loop to do the error checking of the user's selection from the menu. As long as the user enters an invalid value, an error message should be displayed and the user should be given a chance to re-enter the value. Once the user enters a valid value, it should be returned to the caller.
As a reminder, the valid menu options are: 'L' 'l' 'D' 'd' 'P' 'p' 'F' 'f' 'Q' 'q'
double getDouble( string promptToUser, double lowerBound, double upperBound ): This function will get a double value from the user that is within a specified range of values. It takes three arguments: a string that represents a prompt to the user to let them know what type of information should be entered, a double that represents the lowerbound of the range of possible values, and a double that represents the upperbound of the range of possible values. It returns a double: the value from the user that is within the specified range.
The function should display the string argument and the two double values to the user and then get the user's value. A loop should be used to verify the user's value. As long as the user enters a value that is not within the specified range, an error message should be displayed and the user should be given a chance to re-enter the value. Once the user enters a valid value, it should be returned to the caller.
As an example of how this function might be used. Program 5 might have had something like:
cout << "Enter the M value "; cin >> mVal;
In this program, the above code should be replaced with:
mVal = getDouble( "Enter the M value ", 1.0, 100.0 );
The calling statement of the function should produce a prompt similar to
Enter the M value (1.0...100.0):
and should proceed to get a value from the user that is between 1.0 and 100.0.
int getInt( string promptToUser, int lowerBound, int upperBound ): This function will get an integer value from the user that is within a specified range of values. It takes three arguments: a string that represents a prompt to the user to let them know what type of information should be entered, an integer that represents the lowerbound of the range of possible values, and an integer that represents the upperbound of the range of possible values. It returns an intger: the value from the user that is within the specified range.
The function should display the string argument and the two integer values to the user and then get the user's value. A loop should be used to verify the user's value. As long as the user enters a value that is not within the specified range, an error message should be displayed and the user should be given a chance to re-enter the value. Once the user enters a valid value, it should be returned to the caller.
double findLarger( double value1, double value2 ): This function will find the larger of two double values. It takes two arguments: a double that represents the first value to be compared and a double that represents the second value to be compared. It returns a double: the larger of the two values.
bool isDivisible( int dividend, int divisor ): This function will determine if the dividend is divisible by the divisor. It takes two arguments: an integer that represents the dividend value to and an integer that represents the divisor value. It returns a boolean: true if the dividend is divisible by the divisor or false if the dividend is not divisible by the divisor.
double MtoN( double Mvalue, int Nvalue ): This function will calculate and return the result of raising a number (Mvalue) to a power (Nvalue). It takes two arguments: a double that represents the M-value and an integer that represents the N-value. It returns a double: the result of raising Mvalue to the Nvalue power.
Note: DO NOT use the pow function to calculate the result. Write a loop that will perform the calculation.
int calcFactorial( int number ): This function will calculate and return the factorial of a number. It takes one argument: an integer that represents number to be used to calculate a factorial. It returns an integer: the factorial of the passed in number.
As a reminder, the factorial is the product of all integers from 1 to the integer number. The exception is 0! which is equal to 1.
Program Requirements:
-
As with the previous assignments and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation AND function documentation boxes are needed. Make sure that main() and any function that you write contains line documentation
Each function must have a documentation box detailing:
- its name
- its use or purpose: that is, what does it do? What service does it provide to the code that calls it?
- a list of its arguments briefly describing the meaning and use of each
- the value returned (if any) or none
- notes on any unusual features, assumptions, techniques, etc.
/*************************************************************** Function: double getDouble( string promptToUser, double lowerBound, double upperBound ) Use: This function gets a double value within a specified range Arguments: promptToUser - a string that represents a prompt to the user to let them know what type of information to enter lowerBound - a double that represents the lower bound of the specified range upperBound - a double that represents the upper bound of the specified range Returns: A double that is within the specified range Note: None ***************************************************************/
See the documentation standards on the course webpage for more examples or if further clarification is needed. A program will not get full credit (even if it works correctly) if these standards are not followed
-
Make sure to use meaningful variable names.
-
Be sure to #include
-
Hand in a copy of the source code (CPP file) using Blackboard.
Extra Credit
For up to 5 points of extra credit, add a function that will find the range of four double numbers. The range is the difference between the largest and smallest of the four numbers.
The new function should be named findRange. It takes four arguments: four doubles that represent the double values to be used in calculating the range. It returns a double: the difference between the largest and smallest of the four passed in values.
The menu() function will have to be updated to add the new option. Use a value of 'R' or 'r' as the menu selection for the range option.
main() will also have to be updated to handle the new option. If the range option is selected, the getDouble function should be called four times to get four values between -100.0 and 100.0. The four values should be passed to the new findRange function. The resulting range should be displayed in main().
Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
