Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview For this assignment, write a program that will perform various arithmetic operations. The program should be able to: add two integer values subtract two

Overview

For this assignment, write a program that will perform various arithmetic operations.

The program should be able to:

add two integer values

subtract two integer values

multiply two integer values

divide two integer values

raise an integer value to an integer power

calculate the factorial of an integer

Rather than starting from scratch for this assignment, a driver program is available and will need to be modified to complete the assignment. The driver program can be found below:

/*************************************************************** CSCI 240 Program 6 Spring 2018 Programmer: Section: Date Due: Purpose: This program performs simple arithmetic calculations and displays the results. ***************************************************************/ #include  #include  using namespace std; int main() { char operation; int num1, num2, result, remain; //Display the menu to the user and get their first choice cout << "What operation would you like to perform:" << endl << " + addition" << endl << " - subtraction" << endl << " * multiplication" << endl << " / division" << endl << " ^ number to power" << endl << " ! factorial" << endl << " q quit" << endl << endl << "Operation? "; cin >> operation; //While the user does not want to quit while( operation != 'q' and operation != 'Q' ) { //Addition operation if( operation == '+' ) { //Get two numbers from the user cout << endl << "What is the first number to add? "; cin >> num1; cout << endl << "What is the second number to add? "; cin >> num2; //Add the numbers together result = num1 + num2; //Display the result of the addition cout << endl << num1 << " + " << num2 << " = " << result; } //Subtraction operation else if( operation == '-' ) { //Get two numbers from the user cout << endl << "What is the first number to subtract? "; cin >> num1; cout << endl << "What is the second number to subtract? "; cin >> num2; //Subtract the second number from the first number result = num1 - num2; //Display the result of the subtraction cout << endl << num1 << " - " << num2 << " = " << result; } //Multiplication operation else if( operation == '*' ) { //Get two numbers from the user cout << endl << "What is the first number to multiply? "; cin >> num1; cout << endl << "What is the second number to multiply? "; cin >> num2; //Multiply the numbers together result = num1 * num2; //Display the result of the multiplication cout << endl << num1 << " * " << num2 << " = " << result; } //Division operation else if( operation == '/' ) { //Get two numbers from the user cout << endl << "What is the dividend? "; cin >> num1; cout << endl << "What is the divisor? "; cin >> num2; //Divide the first number by the second number, calculating both the quotient //and the remainder result = num1 / num2; remain = num1 % num2; //Display both results of the division cout << endl << num1 << " / " << num2 << " = " << result << endl << num1 << " % " << num2 << " = " << remain; } //Exponentiation operation (number raised to a power) else if( operation == '^' ) { //Get two numbers from the user. The first number is the base value. The second //number is the power. cout << endl << "What is the base number? "; cin >> num1; cout << endl << "What is the power? "; cin >> num2; //Calculate the result of raising the first number (num1) to a power (the //second number) result = 1; for( int cnt = 1; cnt <= num2; cnt++ ) { result *= num1; } //Display the result cout << endl << num1 << "^" << num2 << " = " << result; } //Factorial operation else if( operation == '!' ) { //Get the number to use in the calculation from the user cout << endl << "What is the number? "; cin >> num1; //Calculate the result of multiplying 1 times each value through the number //entered by the user. result = 1; for( int cnt = 2; cnt <= num1; cnt++ ) { result *= cnt; } //Display the result cout << endl << num1 << "! = " << result; } //Invalid operation else { //Display an error message cout << endl << "That is an invalid operation!"; } cout << endl; //Display the menu to the user and get their next operation choice cout << endl << endl << "What operation would you like to perform:" << endl << " + addition" << endl << " - subtraction" << endl << " * multiplication" << endl << " / division" << endl << " ^ number to power" << endl << " ! factorial" << endl << " q quit" << endl << endl << "Next Operation? "; cin >> operation; } return 0; } 

The program is menu-driven, allowing the user to select an option and then based on the option, asking for more information. 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.

For all of the arithmetic operations, with the exception of factorial, the user will be prompted to enter two integer values to use in the calculation. The factorial option will only prompt the user to enter one integer value to use in the calculation.

To receive full credit for the assignment, the following 3 parts must be completed.

Part 1:

For part 1 of the assignment, transform the cascading decision statement that is inside of the while loop into a switch statement. After making the change, the program should run in exactly the same manner as the original driver program.

Part 1 Output

What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Operation? + What is the first number to add? 12 What is the second number to add? -8 12 + -8 = 4 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? - What is the first number to subtract? 45 What is the second number to subtract? 62 45 - 62 = -17 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? * What is the first number to multiply? 7 What is the second number to multiply? 4 7 * 4 = 28 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? / What is the dividend? 8 What is the divisor? 12 8 / 12 = 0 8 % 12 = 8 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? ^ What is the base number? 4 What is the power? 6 4^6 = 4096 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? ! What is the number? 6 6! = 720 What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? & That is an invalid operation! What operation would you like to perform: + addition - subtraction * multiplication / division ^ number to power ! factorial q quit Next Operation? q 

Part 2:

DO NOT move on to this part of the assignment until Part 1 runs correctly.

For part 2 of the assignment, code functions that will perform the various arithmetic operations and then call the functions in main(). The calling statements that are placed in main() should replace the calculations that are currently in the switch statement.

The Functions

Write and use the following 7 functions in the program.

int addition( int value1, int value2 )

This function will calculate and return the sum of two numbers. 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 values and return the result.

int subtraction( int value1, int value2 )

This function will calculate and return the difference of two numbers. 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 value from the first value and return the result.

int multiplication( int value1, int value2 )

This function will calculate and return the product of two numbers. 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 values and return the result.

int quotient( int value1, int value2 )

This function will calculate and return the quotient that results when dividing two integer values. It takes two arguments: the two integer values to be divided. It returns an integer: the quotient. The function should simply divide the first value by the second value to calculate the quotient and return the result.

int remainder( int value1, int value2 )

This function will calculate and return the remainder that results when dividing two integer values. It takes two arguments: the two integer values to be divided. It returns an integer: the quotient. The function should simply divide the first value by the second value to calculate the remainder and return the result.

int power( int base, int powerValue )

This function will calculate the product of raising a number to a power. It takes two arguments: the two integer values to be used in the calculation. 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 will calculate 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 of 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.

Part 2 Requirement

Each function must have a documentation box explaining:

/*************************************************************** Function: Use: Arguments: Returns: Note: ***************************************************************/ 

See the documentation standards on the course webpage for more examples or if further clarification is needed. Your program will not get full credit (even if it works correctly) if these standards are not followed

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 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. Make sure that main() and any function that you write contains line documentation

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

the value returned (if any) or none

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

Part 2 Output

The output for part 2 is exactly the same as part 1.

Part 3:

DO NOT move on to this part of the assignment until Parts 1 and 2 run correctly.

In the previous assignment, the random number generator was used to remove the need for user input in the program. For the final part of this assignment, all of the user input statements will be replaced by statements that read the information from a file.

Input File

The input file for this assignment is named math.txt. It can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/math.txt or Blackboard.

Each line in the file represents a potential arithmetic operation with the first character on the line indicating the type of operation that should be performed.

If the first character on the line is a '+', then an addition operation should be performed with the two integer values that follow the '+'.

A '-' indicates that a subtraction operation should be performed with the two integer values that follow the '-'. The second value should be subtracted from the first value.

A '*' indicates that a multiplication operation should be performed with the two integer values that follow the '*'.

A '/' indicates that a division operation that results in a quotient and quotient should be performed with the two integer values that follow the '/'. The first number is the dividend. The second number is the divisor.

A '^' indicates that a value should be raised to a power. It will be followed by two integer values. The first number is the base. The second number is the power/exponent.

A '!' indicates that a factorial operation should be performed with the single integer value that follows the '!'.

Any other character is invalid and should cause an error message to be displayed and any other information on the line to be ignored.

So, a line in the file that resembles:

+ 23 34 

should display the result of adding 23 and 34 together.

Working with an Input File

Since the input for this program will be coming from a text file rather than standard input (the keyboard), the program will not use cin when it needs to read data. Instead, it will have to read the data from the file. This also means that there will not be any cout statements to prompt the user to enter information.

In order to read from a file, add two #include statements to the program. The new libraries that are needed for this part of the assignment are fstream and cstdlib.

Now, create an input file stream variable and "connect" it with the text file. The "connection" is created by opening the file:

ifstream infile; //input file stream variable //this will be used instead of cin infile.open( "math.txt" ); //open the file for reading 

The open statement will open a file named math.txt and connect it to the variable infile. The file must be located in the same directory as the CPP file. (For Mac users, you will probably have to put in the set of double quotes(""), find where the file has been saved on your computer, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on your computer, between the quotes.)

Once the file has been opened, make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly:

if( infile.fail() ) //if the input file failed to open { cout << "input file did not open" << endl; exit(-1); //stop execution of the program immediately } 

In order to read a character from a file, the input operator can be used in the same manner that it's used with a cin statement. So:

cout << "What operation would you like to perform:" << endl << " + addition" << endl << " - subtraction" << endl << " * multiplication" << endl << " / division" << endl << " ^ number to power" << endl << " ! factorial" << endl << " q quit" << endl << endl << "Operation? "; cin >> operation; 

from the original driver program, will become:

infile >> operation; 

In the driver program, the user had the option to 'q' or 'Q' to indicate they are ready to quit. Since the data is coming from a file and the prompts to the user are being eliminated, there has to be some other way to determine when the program should stop running. The "other way" is to determine when there is no more data in the file. One way to do this is to use the input file stream variable as a boolean variable:

while( infile ) //while there are characters in the file 

As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is only successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern should be followed.

In the case of an invalid operation type, it's possible that there is other information on the line in the file. For example:

a 12 -9 

If the program is written correctly, the 'a' will be read into the operation variable from the earlier example. Before moving on to the next line in the file, the 12 and -9 need to be removed from the file. To do this, use the ignore function:

infile.ignore( 100, ' ' ); 

This will either ignore the next 100 characters in the input file stream or all of the characters in the input file stream until a newline character is reached, which ever occurs first.

Finally, when a file is done being processed, it should be closed.

infile.close(); 

Part 3 Requirements

In the file that is submitted for grading, make sure that the open statement in main() contains only the file name math.txt. Any path that is placed before the file name (usually on a Mac) should be removed.

Hand in a copy of the source code (CPP file) from Part 3 using Blackboard. This file should contain all of the changes that were made to the original driver program through all 3 parts of the assignment.

Part 3 Output

88 + 19 = 107 29 - 28 = 1 1000 + 14 = 1014 6 * 3 = 18 2^5 = 32 45 / 8 = 5 45 % 8 = 5 6! = 720 That is an invalid operation! 0! = 1 8^0 = 1 9 * -2 = -18 -50 - -324 = 274 -4 * -9 = 36 10 / 2 = 5 10 % 2 = 0 5 / 9 = 0 5 % 9 = 5 240 + 322 = 562 5! = 120 That is an invalid operation! That is an invalid operation! 16^4 = 65536 1 / 2 = 0 1 % 2 = 1 

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago