| CSCI 240 | Spring 2026 |
For this assignment, write a program that will use functions to perform the following arithmetic operations:
The program should also use a function to allow a user to select an option from a menu and validaite it. The menu should resemble the following:
What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation?
where a value of '+' indicates that an addition operation should be performed, '-' indicates subtraction, '*' indicates multiplication, '/' indicates division that results in the quotient and remainder, '^' indicates raising a number to a power, '!' indicates that a factorial operation should be performed, 'q' or 'Q' indicates the user wants to quit the program, and any other character is invalid.
Finally, the program should use functions to validate the values that are used in the arithmetic.
The cpp file that is submitted for grading must be named assign6.cpp.
int main() should be written so that it calls the various functions that are listed under "The Functions" heading below. For this assignment, int main() will not contain any cout/cin statements to prompt the user for information, or any calculations. Those will all be replaced by calling statements for functions.
For each calculation, rather than performing the calculation in main(), put a calling statement for the corresponding function that performs the calculation.
The error checking for this assignment is to make sure that invalid values cannot be used in the calculations. The power value that is used when raising a number to a power and the value used to calculate a factorial cannot be negative. Use a calling statement for the getNonNegativeValue() function that is described below.
The divisor that is used in the division operation cannot be zero. Use a calling statement for the getNonZeroValue() function.
For addition, subtraction, multiplication, the dividend used in division, and the base value that is used when raising a number to a power, there is no real error checking to perform because any integer value can be used. Use a calling statement for the getValue() function.
Write and use the following 11 functions in the program.
This function calculates and returns the sum of two integer values. It takes two arguments: the two integer values to add together. It returns an integer: the sum of the two integer values.
The function should simply calculate the sum of the two argument values and return the result.
This function calculates and returns the difference of two integer values. It takes two arguments: the two integer values to be subtracted. It returns an integer: the difference of the two integer values.
The function should simply subtract the second argument value from the first argument value and return the result.
This function calculates and returns the product of two integer values. It takes two arguments: the two integer values to be multiplied together. It returns an integer: the product of the two integer values.
The function should simply calculate the product of the two argument values and return the result.
This function calculates and returns the quotient that results when dividing two integer values. It takes two arguments: the first is an integer value that represents the dividend, the second is an integer value that represents the divisor. It returns an integer: the quotient that results from the division.
The function should simply divide the first argument value by the second argument value to calculate the quotient and return the result.
This function calculates and returns the remainder that results when dividing two integer values. It takes two arguments: the first is an integer value that represents the dividend, the second is an integer value that represents the divisor. It returns an integer: the remainder that results from the division.
The function should simply divide the first argument value by the second argument value to calculate the remainder and return the result.
This function calculates and returns the product of raising a number to a power. It takes two arguments: the first is an integer value that represent the base value, the second is an integer value that represents the power. It returns an integer: the product of raising a number to a power.
The function should use a loop to calculate the product of raising the first value (the base) to a power (the second value) and return the result.
Note: remember that if the power (the second value) is 0, the result is 1.
DO NOT use the pow function that is part of the cmath library. You're writing your own version of that function.
This function calculates and returns the factorial of an integer value. It takes one argument: the integer value that is used in the factorial calculation. It returns an integer: the factorial of the value.
The function should use a loop to calculate the factorial of the integer value and return the result.
The factorial of a value is the product of 1 times all the values through the integer value. So the factorial of 4 is the product of 1 * 2 * 3 * 4. The exception is the factorial of 0 which is equal to 1.
This function gets an integer value from the user. It takes one argument: a string that is used as a prompt to the user to indicate what should be entered. It returns an integer: the integer value entered by the user.
The function should simply display the string argument, get the user's value, and return the user's value.
This function gets an integer value from the user that is not negative. It takes one argument: a string that is used as a prompt to the user to indicate what should be entered. It returns an integer: the non-negative integer value entered by the user.
The function should simply display the string argument and get the user's value. This should be followed by a loop that verifies the user's value. As long as the user enters a negative 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.
This function gets an integer value from the user that is not zero. It takes one argument: a string that is used as a prompt to the user to indicate what should be entered. It returns an integer: the non-zero integer value entered by the user.
The function should simply display the string argument and get the user's value. This should be followed by a loop that verifies the user's value. As long as the user enters a value of zero, 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.
This function displays a menu of options to the user and gets their choice. It takes no arguments. It returns a character: the user's menu choice.
The function should simply display the menu of possible options to the user and get the user's choice. This should be followed by a loop that verifies the user's value. As long as the user enters a value that is not equal to '+' and '-' and '*' and '/' and '^' and '!' and 'q' and 'Q', 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.
Note: the loop condition is a long compound condition. Be careful when coding it to make sure that it's correctly using complete conditions.
The getValue function should be used to get the two values that are added, the two values that are subtracted, the two values that are multiplied, the dividend that is used in division, and the base value that is used when raising a number to a power.
The getNonNegativeValue function should be used to get the power value that is used when raising a number to a power, and the value that is used to in the calculation of a factorial.
The getNonZeroValue function should be used to get the divisor that is used in division.
The menu function should be called two times in this assignment. The first time should be before the loop (the priming read). The second time should be at the end of the loop (the secondary read).
As with the previous assignment and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation (both in int main() and the functions) AND function documentation boxes are needed. In regards to line documentation, there is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describe what the "chunk" of code does.
Each function must have a documentation box explaining:
/*************************************************************** * A brief description of what the function does * * @param arg_name_1 brief description of the first argument * @param arg_name_2 brief description of the second argument * @param arg_name_3 brief description of the third argument * * @return a brief description of the possible return value(s) * * @note an optional note about the function that may be of * interest to someone using it ***************************************************************/
For example, the addition function might start with:
/*************************************************************** * This function calculates the sum of two integer values * * @param value1 an integer that is used in the addition * @param value2 the second integer that is used in the addition * * @return an integer that represents the sum of the two integer values ***************************************************************/
See the documentation standards (LINK) 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.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? $ Error: that is an invalid operation. Try again: # Error: that is an invalid operation. Try again: @ Error: that is an invalid operation. Try again: + What is the first number to add? 4 What is the second number to add? 17 4 + 17 = 21 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? - What is the first number to subtract? -9 What is the second number to subtract? -5 -9 - -5 = -4 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? * What is the first number to multiply? 3 What is the second number to multiply? -8 3 * -8 = -24 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? / What is the dividend? -9 What is the divisor? 0 Error: the value cannot be zero. Try again: 0 Error: the value cannot be zero. Try again: 0 Error: the value cannot be zero. Try again: 5 -9 / 5 = -1 -9 % 5 = -4 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? ^ What is the base number? 4 What is the power? -3 Error: the value cannot be negative. Try again: -7 Error: the value cannot be negative. Try again: -2 Error: the value cannot be negative. Try again: 3 4^3 = 64 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? ! What is the number? -8 Error: the value cannot be negative. Try again: -7 Error: the value cannot be negative. Try again: 4 4! = 24 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? Q