We are interested in writing a program that handles the four basic math operatio
ID: 3873932 • Letter: W
Question
We are interested in writing a program that handles the four basic math operations (i.e., addition, subtraction, multiplication, and division) as separate functions. Each function will have two doubles as arguments and return a double. We will assume that the second arugment for the division function cannot be zero. The prototypes for these functions are as follows:
1
To appreciate the effectiveness of using functions, you will be asked to create a function that handles entering a value for a variable and another function that handles printing the value for a single variable. Whenever the program needs to get the input from the user or display the result of a computation, it should call one of these two functions. The input function will take no arguments but return a double. This function should contain a prompt and allow the user to enter a number. The output function will take a double as a parameter but return nothing. This function should display the result of one of the above functions. The prototypes for these functions are as follows:
Your main function should declare the variables and use function calls. No other code is needed in main. Note that with this exercise, the text in the input and output statements cannot be specific. An example of the output is given below:
Explanation / Answer
//Please see the code below do thumps up if you like the solution
//Please see the code below do thumps up if you like the solution
#include <stdio.h>
#include <conio.h>
void myoutput(double x)
{
printf("Result is %.2lf ",x);
}
double myinput()
{
double number=0;
printf("Enter a number: ");
scanf("%lf",&number);
return number;
}
double mysum(double x, double y)
{
return (x+y);
}
double mydiff(double x, double y)
{
return (x-y);
}
double myproduct(double x, double y)
{
return (x*y);
}
double myquotient(double x, double y)
{
return (x/y);
}
int main ()
{
double result=0;
double firstNumber=0;
double secondNumber=0;
firstNumber= myinput();
secondNumber=myinput();
result=mysum(firstNumber,secondNumber);
myoutput(result);
result=mydiff(firstNumber,secondNumber);
myoutput(result);
result=myproduct(firstNumber,secondNumber);
myoutput(result);
result=myquotient(firstNumber,secondNumber);
myoutput(result);
getch();
return 0;
}
OUTPUT:
Enter a number: 7
Enter a number: 4
Result is 11
Result is 3
Result is 28
Result is 1.75
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.