In: Computer Science
Code: please use in visual studio 2019 c++ (not java)
In total you will write 6 functions:
Functions :
A ) addInts, returns int; input is two ints; Add the values of the two parameters and return the sum
B) subInts , returns int; input is two ints; Subtract the values of the two parameters and return the difference
C) multInts, returns int; input is two ints; multiple the values of the two parameters and return the product
D) divInts, returns int; input is two ints; Divide the values of the two parameters and return the quotient
E) modInts, returns int; input is two ints; compute the residual of the values of the two parameters and return the residual
F) A function called computeOperation ( ) that , returns an int; input is three parameters: one 'control' int, and two ints;
if the control int ( the first parameter) is :
Control
Control Parameter | compute return value : |
1 | Return sum of 2nd and 3td parameters |
2 | Return diff of 2nd and 3td parameters |
3 | Return product of 2nd and 3td parameters |
4 | Return quotient of 2nd and 3td parameters |
5 | Return modulus of 2nd and 3td parameters |
expect to see both Function Declarations and of course the Function Definitions for all 6 functions
expect to see some obvious test code or data sets.
expect to see a switch statement not a series of if statements if it is appropriate.
To Be Clear:
in the main function there should be a homework header and then a bunch of 'call's to the
computeOperation ( ) function.
And
computeOperation should then have code to invoke one of the 5 low level compute functions. The value that is returned should be returned by computeOperator.
/*
The C++ program that contains 6 functions called as addInts,subInts
, multInts, modInts,computeOperation ( ).
The program test some sample values of x and y variables. Then call
the function, computeOperation with operation code value in a range
of 1 to 6, x and y values as 3 arguments to the function. Then,
print the results of return values of the function on the console
output.
*/
//arithmatic.cpp
//include header files
#include<iostream>
using namespace std;
//Function prototypes
int addInts(int , int);
int subInts(int , int);
int multInts(int , int);
int divInts(int , int);
int modInts(int , int);
int computeOperation (int , int ,int);
//start of main function
int main()
{
//Set sample values for x and y values
int x=10;
int y=5;
//Set result to 0
int result=0;
//Set operationcode to 1 for addition
int operationcode=1;
result=computeOperation(operationcode,x,y);
cout<<"computeOperation("<<operationcode<<","
<<x<<","<<y<<") = "
<<result<<endl;
//Set operationcode to 1 for subtracation
operationcode=2;
result=computeOperation(operationcode,x,y);
cout<<"computeOperation("<<operationcode<<","
<<x<<","<<y<<") = "
<<result<<endl;
//Set operationcode to 1 for multiplication
operationcode=3;
result=computeOperation(operationcode,x,y);
cout<<"computeOperation("<<operationcode<<","
<<x<<","<<y<<") = "
<<result<<endl;
//Set operationcode to 1 for division
operationcode=4;
result=computeOperation(operationcode,x,y);
cout<<"computeOperation("<<operationcode<<","
<<x<<","<<y<<") = "
<<result<<endl;
//Set operationcode to 1 for modulus
operationcode=5;
result=computeOperation(operationcode,x,y);
cout<<"computeOperation("<<operationcode<<","
<<x<<","<<y<<") = "
<<result<<endl;
system("pause");
return 0;
}
/*Function, addInts takes two arguments of integer
type and returns the sum of the values as return value*/
int addInts(int x , int y)
{
return x+y;
}
/*Function, subInts takes two arguments of integer
type and returns the difference of the values as return
value*/
int subInts(int x , int y)
{
return x-y;
}
/*Function, multInts takes two arguments of integer
type and returns the product of the values as return value*/
int multInts(int x , int y)
{
return x*y;
}
/*Function, divInts takes two arguments of integer
type and returns the quotient of the values as return value*/
int divInts(int x , int y)
{
return x/y;
}
/*Function, modInts takes two arguments of integer
type and returns the remainder of the values as return
value*/
int modInts(int x , int y)
{
return x%y;
}
/*
The function , computeOperation takes three
input arguments of integer type opcode, x and y variables.
Then using switch case, select the appropriate
function call . Then store the return value in result.
Returns the result value.
*/
int computeOperation (int opcode,int x , int y)
{
int result=0;
switch(opcode)
{
case 1:
result= addInts(x,y);
break;
case 2:
result= subInts(x,y);
break;
case 3:
result= multInts(x,y);
break;
case 4:
result= divInts(x,y);
break;
case 5:
result= modInts(x,y);
break;
}
return result;
}
Sample Output: