Overview For this assignment, write a program that will perform various arithmet
ID: 3731589 • Letter: O
Question
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:
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
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:
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:
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:
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:
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:
from the original driver program, will become:
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:
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:
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:
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.
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
Explanation / Answer
Part 1. Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char operation;
int num1, num2, result, remain;
//While the user does not want to quit
operation = '+'; //A minor tweak To get an entry in while loop
while( operation != 'q' and operation != 'Q' )
{
//Addition operation
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;
switch (operation) {
case '+':
{
//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;
cout << endl << endl;
break;
}
//Subtraction operation
case '-':
{
//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;
cout << endl << endl;
break;
}
//Multiplication operation
//else if( operation == '*' )
case '*':
{
//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;
cout << endl << endl;
break;
}
//Division operation
// else if( operation == '/' )
case '/':
{
//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;
cout << endl << endl;
break;
}
//Exponentiation operation (number raised to a power)
case '^':
{
//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;
cout << endl << endl;
break;
}
//Factorial operation
case '!':
{
//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;
cout << endl << endl;
break;
}
case 'q': break;
case 'Q':break;
//Invalid operation
default:
{
//Display an error message
cout << endl << "That is an invalid operation!";
cout<<endl<<endl;
break;
}
}
}
return 0;
}
part 2:
#include <iostream>
#include <iomanip>
using namespace std;
/***************************************************************
Function: Addition
Use: Adds two numbers and returns the value of their addition
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int addition(int num1, int num2){
return num1 + num2;
}
/***************************************************************
Function: Subtraction
Use: Subtracts two numbers and returns the value of their subtraction
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int subtraction(int num1, int num2){
return num1 - num2;
}
/***************************************************************
Function: Multiplication
Use: Multiplys two numbers and returns the value of their multiplication
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int multiplication(int num1, int num2){
return num1 * num2;
}
/***************************************************************
Function: Quotient
Use: Divides two numbers and returns the quotient
Arguments: int num1, int num2
Returns: int
Note: Don't provide divisor as zero. Gives wrong result for floating and double numbers.
***************************************************************/
int quotient (int num1, int num2){
return num1 / num2;
}
/***************************************************************
Function: Remainder
Use: Divides two numbers and returns the remainder
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int remainder (int num1, int num2){
return num1 % num2;
}
/***************************************************************
Function: Power
Use: Returns the result of power operation.
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int power (int num1, int num2){
//Calculate the result of raising the first number (num1) to a power (the
//second number)
int result = 1;
for( int cnt = 1; cnt <= num2; cnt++ )
{
result *= num1;
}
return result;
}
/***************************************************************
Function: Factorial
Use: Returns the result of factorial operation.
Arguments: int num1
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int factorial (int num1){
int result = 1;
for( int cnt = 2; cnt <= num1; cnt++ )
{
result *= cnt;
}
return result;
}
int main()
{
char operation;
int num1, num2, result, remain;
//While the user does not want to quit
operation = '+';
while( operation != 'q' and operation != 'Q' )
{
//Addition operation
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;
switch (operation) {
case '+':
{
//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 = addition(num1, num2);
//Display the result of the addition
cout << endl << num1 << " + " << num2 << " = " << result;
cout << endl << endl;
break;
}
//Subtraction operation
case '-':
{
//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 = subtraction(num1, num2);
//Display the result of the subtraction
cout << endl << num1 << " - " << num2 << " = " << result;
cout << endl << endl;
break;
}
//Multiplication operation
//else if( operation == '*' )
case '*':
{
//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 = multiplication(num1, num2);
//Display the result of the multiplication
cout << endl << num1 << " * " << num2 << " = " << result;
cout << endl << endl;
break;
}
//Division operation
// else if( operation == '/' )
case '/':
{
//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 = quotient(num1, num2);
remain = remainder(num1, num2);
//Display both results of the division
cout << endl << num1 << " / " << num2 << " = " << result
<< endl << num1 << " % " << num2 << " = " << remain;
cout << endl << endl;
break;
}
//Exponentiation operation (number raised to a power)
case '^':
{
//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 = power(num1, num2);
//Display the result
cout << endl << num1 << "^" << num2 << " = " << result;
cout << endl << endl;
break;
}
//Factorial operation
case '!':
{
//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 = factorial(num1);
//Display the result
cout << endl << num1 << "! = " << result;
cout << endl << endl;
break;
}
case 'q': break;
case 'Q':break;
//Invalid operation
default:
{
//Display an error message
cout << endl << "That is an invalid operation!";
cout<<endl<<endl;
break;
}
}
}
return 0;
}
part3:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
/***************************************************************
Function: Addition
Use: Adds two numbers and returns the value of their addition
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int addition(int num1, int num2){
return num1 + num2;
}
/***************************************************************
Function: Subtraction
Use: Subtracts two numbers and returns the value of their subtraction
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int subtraction(int num1, int num2){
return num1 - num2;
}
/***************************************************************
Function: Multiplication
Use: Multiplys two numbers and returns the value of their multiplication
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int multiplication(int num1, int num2){
return num1 * num2;
}
/***************************************************************
Function: Quotient
Use: Divides two numbers and returns the quotient
Arguments: int num1, int num2
Returns: int
Note: Don't provide divisor as zero. Gives wrong result for floating and double numbers.
***************************************************************/
int quotient (int num1, int num2){
return num1 / num2;
}
/***************************************************************
Function: Remainder
Use: Divides two numbers and returns the remainder
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int remainder (int num1, int num2){
return num1 % num2;
}
/***************************************************************
Function: Power
Use: Returns the result of power operation.
Arguments: int num1, int num2
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int power (int num1, int num2){
//Calculate the result of raising the first number (num1) to a power (the
//second number)
int result = 1;
for( int cnt = 1; cnt <= num2; cnt++ )
{
result *= num1;
}
return result;
}
/***************************************************************
Function: Factorial
Use: Returns the result of factorial operation.
Arguments: int num1
Returns: int
Note: Gives wrong result for floating and double numbers.
***************************************************************/
int factorial (int num1){
int result = 1;
for( int cnt = 2; cnt <= num1; cnt++ )
{
result *= cnt;
}
return result;
}
int main()
{
char operation;
int num1, num2, result, remain;
ifstream infile; //input file stream variable
//this will be used instead of cin
infile.open( "math.txt" ); //open the file for reading
//While the user does not want to quit
while( infile )
{
//Addition operation
infile >> operation;
switch (operation) {
case '+':
{
//Get two numbers from the user
infile >> num1;
infile >> num2;
//Add the numbers together
result = addition(num1, num2);
//Display the result of the addition
cout << num1 << " + " << num2 << " = " << result<<endl<<endl;
break;
}
//Subtraction operation
case '-':
{
//Get two numbers from the user
infile >> num1;
infile >> num2;
//Subtract the second number from the first number
result = subtraction(num1, num2);
//Display the result of the subtraction
cout<< num1 << " - " << num2 << " = " << result<<endl<<endl;
break;
}
//Multiplication operation
//else if( operation == '*' )
case '*':
{
infile >> num1;
infile >> num2;
//Multiply the numbers together
result = multiplication(num1, num2);
//Display the result of the multiplication
cout << num1 << " * " << num2 << " = " << result<<endl<<endl;
break;
}
//Division operation
// else if( operation == '/' )
case '/':
{
infile >> num1;
infile >> num2;
result = quotient(num1, num2);
remain = remainder(num1, num2);
//Display both results of the division
cout << endl << num1 << " / " << num2 << " = " << result
<< endl << num1 << " % " << num2 << " = " << remain;
cout << endl<<endl;
break;
}
//Exponentiation operation (number raised to a power)
case '^':
{
infile >> num1;
infile >> num2;
//Calculate the result of raising the first number (num1) to a power (the
//second number)
result = power(num1, num2);
//Display the result
cout << endl << num1 << "^" << num2 << " = " << result<<endl<<endl;
break;
}
//Factorial operation
case '!':
{
infile >> num1;
//Calculate the result of multiplying 1 times each value through the number
//entered by the user.
result = factorial(num1);
//Display the result
cout << endl << num1 << "! = " << result<<endl<<endl;
break;
}
case 'q': break;
case 'Q':break;
//Invalid operation
default:
{
//Display an error message
infile.ignore(100,' ');
cout << endl << "That is an invalid operation!"<<endl<<endl;
break;
}
}
}
infile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.