CSCI 240 Spring 2024

Program 6
Functions
(100 points)


Due: Friday, March 8 on Blackboard by 11:59PM

Overview

For this assignment, modify program 5 so that it uses functions and does error checking with the data that is used in the calculations.

int main()

int main() should be modified so that it calls the functions that are listed under "The Functions" heading below. int main() will no longer have 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, replace the calculation in main() with a calling statement for the corresponding function that performs the calculation. Make sure that the value returned from the function is saved in the same variable that held the result of the calculation in Program 5.

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 as the replacement for the cout/cin combination that were used in Program 5.

The divisor that is used in the division operation cannot be zero. Use a calling statement for the getNonZeroValue() function as the replacement for the cout/cin combination that was used in Program 5.

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 as the replacement for the cout/cin combinations from Program 5.

The end of this assignment write up has a section titled "How to Complete this Assignment" that details a step by step way to successfully complete this assignment.

The Functions

Write and use the following 11 functions in the program.

int addition( int value1, int value2 )

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.

int subtraction( int value1, int value2 )

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.

int multiplication( int value1, int value2 )

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.

int quotient( int value1, int value2 )

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.

int remainder( int value1, int value2 )

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.

int power( int base, int power )

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.

int factorial( int value )

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.

int getValue( string prompt )

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.

int getNonNegativeValue( string prompt )

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.

int getNonZeroValue( string prompt )

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.

char menu()

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.

Program Requirements

  1. 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.

  2. 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.

  3. The getNonZeroValue function should be used to get the divisor that is used in division.

  4. 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).

  5. 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.

  6. Each function must have a documentation box explaining:

    /***************************************************************
    Function: addition
    
    Use: This function calculates the sum of two integer values
    
    Arguments: value1 - an integer that is used in the addition
               value2 - the second integer that is used in the addition
    
    Returns: the sum of the two integer values
    
    Note: None
    ***************************************************************/
    

    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.

  7. Hand in a copy of the source code (CPP file) using Blackboard.

Output

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: +

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

How to Complete this Assignment

A suggestion on how to successfully complete this assignment: write the functions one at a time and test them one at a time.

Start with the addition() function that is described above.

Compile and Run the program.

Select the '+' option from the menu and make sure that the program successfully adds two numbers together and displays the result. If the sum that is displayed is incorrect, check the calling statement. Make sure that the value that is returned from the function is being saved in the same variable that held the result in program 5 (this is the same variable that is being displayed in the cout statement that follows the addition). Make sure that the two variables that were used in the calculation from program 5 are the two variables that are passed to addition() function (ie. they're the two variables between the () on the calling statement). Check the function and make sure that the sum is being returned and not just saved in a variable.

Once the addition function work, add the subtraction() function next. Make sure to follow the same steps that are listed above (prototype before main, header and function after main, comment out calculation from program 5, and add calling statement in main). Compile and Run the program. Select the '-' option and make sure that the program handles subtraction correctly.

Keep adding in the various functions that handle the math one at a time until all of them have been written and used in main().

Once all the math works correctly, remove the statements that were commented out.

It's now time to start adding in the get functions that are listed above. Start with the getValue function because it is the easiest of the three get functions.

Compile and Run the program.

Select the '+' option from the menu and make sure that the program successfully asks the user for the first number to use in the addition. Make sure that the program still successfully adds two numbers together and displays the result. If the sum that is displayed is incorrect, check the calling statement for the getValue() function. Make sure that the value that is returned from the function is being saved in the same variable that was used on the cin instruction that was commented out. Check the function and make sure that the value entered by the user is being returned and not just saved in a variable.

Once the getValue() function successfully gets the first number used in the addition, replace the cout/cin combination that gets the second number used in the addition by following the 3rd and 4th steps from above (comment out the cout/cin and call the getValue() function). Compile and Run the program again. Make sure that the addition produces the correct output.

Once both cout/cin combinations have been replaced for the addition, replace the cout/cin combinations for subtraction and, eventually, multiplication. Do the same thing for the dividend that is used in the division and the base that is used when raising a number to a power. Just make sure to test the different math operations as the cout/cin combinations are being replaced.

Once getValue() has been used in all the required places, remove the statements that were commented out.

Move on to the getNonNegativeValue() function next.

Compile and Run the program.

Select the '^' option from the menu and make sure that the program asks the user for the power to use when raising a number to a power.

Try an invalid number (ie. something that is negative. The program should display an error message and prompt for a new value. Try another invalid number (again, something that is negative). The error message and prompt for a new value should happen again. Try a valid number (such as 4). The program should successfully raise a number to a power and display the result.

If the result that is displayed is incorrect, check the calling statement for the getNonNegativeValue() function. Make sure that the value that is returned from the function is being saved in the same variable that was used on the cin instruction that was commented out. Check the function and make sure that the value entered by the user is being returned and not just saved in a variable.

Once the getNonNegativeValue() function successfully gets the power, replace the cout/cin combination that gets the number used in the factorial calculation by following the 3rd and 4th steps from above (comment out the cout/cin and call the getNonNegativeValue() function). Compile and Run the program again. Make sure that the factorial produces the correct output.

Once getNonNegativeValue() has been used in all the required places, remove the statements that were commented out.

Move on to the getNonZeroValue() function next.

Compile and Run the program.

Select the '/' option from the menu and make sure that the program asks the user for the dividend.

Try entering 0. The program should display an error message and prompt for a new value. Try entering 0 again. The error message and prompt for a new value should happen again. Try a valid number (such as 4). The program should successfully calculate a factorial and display the result.

If the results that are displayed are incorrect, check the calling statement for the getNonZeroValue() function. Make sure that the value that is returned from the function is being saved in the same variable that was used on the cin instruction that was commented out. Check the function and make sure that the value entered by the user is being returned and not just saved in a variable.

Remove the statements that were commented out.

Finally, move on to the menu() function.

Compile and Run the program.

Make an invalid selection from the menu (such as '$'). The program should display an error message and prompt for a new choice. Try another invalid choice (such as 'T'). The error message and prompt for a new choice should happen again. Try a valid choice (such as '+'. The program should go through the steps for an addition operation.

Select the 'q' option from the menu and make sure that the program quits.

Once the menu() function works, replace the cout/cin statement at the end of the loop that redisplays the menu and gets the next choice by following the 3rd and 4th steps from above (comment out the cout/cin and call the menu() function). Compile and Run the program again. Try a couple different operations to make sure the program still handles the calculations correctly.

Finally, enter a value of 'Q' to quit the program. This is just to make sure that both 'q' and 'Q' work as options to end the program.