Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSCI 240 Summer 2017 Program 3 Functions (100 points) Overview For this assignment, write a set of functions that will be used by an interactive

CSCI 240

Summer 2017

Program 3 Functions (100 points)

Overview

For this assignment, write a set of functions that will be used by an interactive menu-driven program that will perform various calculations and operations. A menu of calculation types will be shown to a user at the beginning of the program (and again after each calculation). This menu will be implemented as a function (more details to follow). The user will select an option and the program will then prompt for additional information that is specific to the type of operation to be performed, perform the operation, and display the results of that operation. This process will continue until the user decides to quit.

As noted above, you are only required to write functions for this assignment. A driver program that contains the code necessary for main() can be found on Blackboard. Download the driver program and add your functions to the end of it.

The Functions

string Menu()

This function displays a menu to the user. It will then accept the user's choice (as a string) and check it for validity - that is did the user enter a "1", "2", "3", "4", "Q" or "q"? If so, the string should be returned to the calling function (main()). If not, an error message should be displayed and the user should be given another chance to make a choice - this should continue until the user enters a valid value.

The menu should resemble the following:

Special Purpose Calculator

1) Average

2) Factorial

3) M to the Nth Power

4) Range

Q) Quit

Enter your choice:

int getValue( string prompt, int lowerBound, int upperBound )

This function will get an integer value from the user that is within a specified range. It takes three arguments: a string that will be part of the prompt that is displayed to the user, and two integers that represent the lower and upper bound for a specified range. It returns an integer value that is within the specified range. The function should display a prompt to the user and accept a value. If the value is within the range, it should be returned to the calling function. If the value is invalid, display an error message and ask the user to to re-enter a value - this should be repeated until a valid value is entered.

A calling statement like:

value = getValue( "Enter a number", 10, 50 );

should produce the following prompt to the user:

Enter a number between 10 and 50:

void doAverage()

This function will call the getValue() function to prompt the user for a sum between -1000 and 1000, call the getValue() function to prompt the user for a count between 1 and 1000, pass the two values returned by the getValue() calls to the calcAverage() function, and then display the result that is returned by the calcAverage() function. The calculated average should be displayed with 3 digits after the decimal point.

double calcAverage( int sum, int count )

This function will calculate an average. It takes two arguments: an integer sum and an integer count. It returns a double value which is the calculated average.

void doFactorial()

This function will call the getValue() function to prompt the user for a value between 0 and 10, pass the value returned by getValue() to the calcFactorial() function, and then display the result that is returned by the calcFactorial() function.

int calcFactorial( int number )

This function will calculate the factorial of an integer value. It takes one argument: an integer that factorial should be calculated. It returns an integer value which is the calculated factorial. As a reminder, the factorial of a number is the product of the values between 1 and the number, inclusive. For example, 4! is equal to 4 * 3 * 2 * 1 = 24. The exception to the rule is 0! which is equal to 1.

void doMtoNth()

This function will call the getValue() function to prompt the user for an M value between 1 and 10, call the getValue() function to prompt the user for an N value between 1 and 8, pass the two values returned by the getValue() calls to the calcMtoNth() function, and then display the result that is returned by the calcMtoNth() function.

int calcMtoNth( int M, int N )

This function will calculate a number raised to a specified power. It takes two arguments: an integer that is the number to be raised to power (M) and an integer that is the power to raise the number to (N). It returns an integer value which is the result of raising M to the Nth power.

void doRange()

This function will call the getValue() function 4 times in order to prompt the user for values between 0 and 10000, pass the four values returned by the getValue() calls to the calcRange() function, and then display the result that is returned by the calcRange() function.

int calcRange( int number1, int number2, int number3, int number4 )

This function will calculate a range amoung 4 integer values. It takes four arguments: the integers that should be used in the range calculation. It returns an integer value which is the range of the 4 numbers. The range of the numbers is the difference between the largest of the 4 numbers and the smallest of the 4 numbers.

Processing Requirements

At the top of the C++ source code, include a documentation box like normal. Complete program documentation is required for this assignment, which include documentation boxes for *EACH* function that explains:

its name

its use or function: 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, and in particular whether the function modifies each one

the value returned (if any) or none.

notes on any unusual features, assumptions, techniques, etc.

/***************************************************************

Function:

Use:

Arguments:

Returns:

Notes:

***************************************************************/

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

Output

Special Purpose Calculator

1) Average

2) Factorial

3) M to the Nth Power

4) Range

Q) Quit

Enter your choice: 2

Enter a number between 0 and 10: 8

The factorial of 8 is 40320

Special Purpose Calculator

1) Average

2) Factorial

3) M to the Nth Power

4) Range

Q) Quit

Enter your choice: 3

Enter the M value between 1 and 10: -5

Error: -5 is invalid. Try again: 17

Error: 17 is invalid. Try again: 9

Enter the N value between 1 and 8: 3

9 raised to the 3th power is 729

Special Purpose Calculator

1) Average

2) Factorial

3) M to the Nth Power

4) Range

Q) Quit

Enter your choice: 567

Error: 567 is invalid. Try again: 78

Error: 78 is invalid. Try again: 6

Error: 6 is invalid. Try again: -1

Error: -1 is invalid. Try again: 4

Enter a number between 0 and 10000: 100

Enter another number between 0 and 10000: 53

Enter another number between 0 and 10000: 222

Enter another number between 0 and 10000: 718

The range of 100, 53, 222 and 718 is 665

Special Purpose Calculator

1) Average

2) Factorial

3) M to the Nth Power

4) Range

Q) Quit

Enter your choice: Q

------------------------

/******************************************************************************* CSCI 240 Program 3 Driver Summer 2017 Name: Section: Date Due: Purpose: *******************************************************************************/ #include  #include  using namespace std; //Place your symbolic constant definitions after this line //Place your function prototypes after this line int main() { string userChoice; int i=5; //Get the user's menu choice userChoice = Menu(); //While the user does not want to quit while( userChoice != "Q" and userChoice != "q" ) { //If the user selected option 1 from the menu, calculate an average if( userChoice == "1" ) { doAverage(); } //If the user selected option 2 from the menu, calculate the factorial //of a number else if( userChoice == "2" ) { doFactorial(); } //If the user selected option 3 from the menu, calculate the result of //of raising a number to a power else if( userChoice == "3" ) { doMtoNth(); } //If the user selected option 4 from the menu, calculate the range of //four numbers else { doRange(); } //Get the next menu choice from the user userChoice = Menu(); } return 0; } //Code your functions below this line 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

Consider the following situation. a left-tailed test with= 0.01

Answered: 1 week ago