Write a C++ program which consists of several functions besides the main() function.
1) The main() function, which shall ask for input
from the user (ProcessCommand() does this) to compute the
following: SumProductDifference and Power. There should be a well
designed user interface.
2) A void function called SumProductDifference(int,
int, int&, int&, int&), that computes the sum, product,
and difference of it two input arguments, and passes the sum,
product, and difference by-reference.
3) A value-returning function called Power(int a, int
b) that computes a raised to the b power. Design and implement your
own power function using an iterative control structure, or even
recursion. Do not simply write a wrapper around the C++ function
called pow().
4) There should be a user loop and a menu so that the
user can select either SumProductDifference, Power, or Quit. Then
ProcessCommand() should ask the user for two values to compute
SumProductDifference or Power. ProcessCommand() should then also
output the answer.
#include <iostream>
using namespace std;
void SumProductDifference(int x,int y,int &sum,int
&product,int &diff)
{
sum=x+y;
product=x*y;
diff=x-y;
}
int power(int a,int b)
{
int r=1;
for(int i=1;i<=b;i++)
r=r*a;
return r;
}
void ProcessCommand(int &x,int &y)
{
cout<<"Enter two numbers :";
cin>>x>>y;
}
int main()
{
int x,y,sum,product,diff;
ProcessCommand(x,y);
SumProductDifference(x,y,sum,product,diff);
cout<<"Sum = "<<sum<<"\nProduct=
"<<product<<"\nDifference ="<<diff;
cout<<"\nPower of "<<x<<"to "<<y<<"
is :"<<power(x,y);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.