Question: Would you kindly provide the answer to this question based on what is asked for . I find the answer in chegg but it had
Would you kindly provide the answer to this question based on what is asked for . I find the answer in chegg but it had different variable names and not using while statement.
Topics
Parameter Passing
Pass By Value/Copy
Pass By Reference/Alias
Description
Write a program that will prompt the user to input two decimal numbers and will display their sum, difference, product and quotient. (The sum is the result of addition; the difference is the result of subtraction; the product is the result of multiplication; and the quotient is the result of division.) The program will do this repeatedly until user enters x when prompted for the first number. Then the program will end. See the test run in the section Testing
Requirements
Do this assignment using the main method and a method compute
Instructions
For each of the methods (main, compute), provide functional description, parameter description, and return value description just above the method code.
Implementation
The program will provide two methods: main and compute.
Main Method
The method main will do the following repeatedly.
It will prompt the user to enter the first number.
It will prompt the user to enter the second number.
It will call the method compute.
It will display the original numbers and their sum, difference, product, and quotient.
It will do it repeatedly till the user enters X when asked to enter the first number
Compute Method
The method compute will have six parameters namely num1, num2, sum, diff, prod, quotient. The first two parameters num1 and num2 will be passed by value (copy) method and the remaining four will be passed by reference (alias) method. The header for this method with an empty body is shown below:
void compute (double num1, double num2,
double & sum, double & diff, double & prod, double & quotient)
{
}
Testing
For doing this assignment, perform the test run below using the values shown.
Test Run
(The user input is shown in bold).
Enter First Number
2.4
Enter Second Number
1.2
First Number: 2.4
Second Number: 1.2
Sum: 3.6
Difference: 1.2
Product: 2.88
Quotient: 2.0
Enter First Number
4.5
Enter Second Number
1.5
First Number: 4.5
Second Number: 1.5
Sum: 6.0
Difference: 3.0
Product: 6.75
Quotient: 3.0
Enter First Number
X
Sample Code
//method prototype
void compute (double num1, double num2,
double & sum, double & diff, double & prod, double & quotient);
//method compute definition
void compute (double num1, double num2,
double & sum, double & diff, double & prod, double & quotient)
{
sum=num1 + num2;
diff=num1 - num2;
prod=num1 * num2;
quotient=num1 / num2;
}
//method main definition
int main ( )
{
double n1, n2, s, d, p, q;
//input n1 from user (Initialization of the loop)
cout << "Enter the first number" << endl;
cin << n1;
while (cin != NULL)
{
//input n2 from user
//call compute
compute (n1, n2, s, d, p, q);
//display sum
cout << "Sum: " << s << endl;
//display difference
cout << "Difference: " << d << endl;
//display product
//display quotient
//input n1 from user (update of the loop)
cout << "Enter the first number" << endl;
cin << n1;
}
//return from the method main
return 0;
}
Discussion
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
