*** USING C++ *** TOPICS Parameter Passing Pass By Value/Copy Pass By Reference/
ID: 3838446 • Letter: #
Question
*** USING C++ ***
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, difference that of subtraction, product that of multiplication and quotient that of division.) The program will do this repeatedly.
C++ users
*/The program will end if the user enters x when prompted for the first number.
Instructions
For each of the methods (main, compute), provide functional description, parameter description, and return value description just above the method code.
Testing
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
Press Cancel Button (Java users)
x (C++ users)
IMPLEMENTAION
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 method compute.
· It will display the original numbers and their sum, difference, product, and quotient.
Compute Method
The method compute will have six parameters namely num1, num2, sum, diff, prod, quotient. The method will use the first two parameters to receive two numbers and will use the last four for returning sum, difference, product, and quotient of the two numbers. The method will use Pass By Value (Pass By Copy) method for first two parameters and Pass by Reference (Pass By Alias) method for the last four parameters. 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)
{
}
Explanation / Answer
Code:
// Program to demonstrate pass by value and pass by reference techniques
#include <iostream>
using namespace std;
// function prototype/declaration
void compute(double, double, double*, double*, double*, double*);
int main()
{
// variable declaration
double num1, num2;
double sum, diff, prod, quotient;
// initializing the variables sum, diff, prod and quotient to 0
sum = diff = prod = quotient = 0;
// getting input for num1 and num2 from the user
cout << "Enter first number ";
cin >> num1;
cout << "Enter second number ";
cin >> num2;
// calling function compute to calculate sum, diff, prod and quotient using num1 and num2 values
compute(num1, num2, &sum, &diff, &prod, "ient);
//Since sum, diff, prod and quotient are passed by reference
//These variables even though manipulated inside the function their values are reflected outside
//Thats why we are able to access the sum, diff, product and quotient of num1 and num2
cout << endl;
cout << "Sum of the numbers "<< num1 <<" and " << num2 << " is: "<<sum << endl;
cout << "Difference of the numbers "<< num1 << " and " << num2 << " is: "<<diff << endl;
cout << "Product of the numbers "<< num1 <<" and " << num2 << " is: "<<prod << endl;
cout << "Quotient of the numbers "<<num1 <<" and " << num2 << " is: "<<quotient << endl;
return 0;
}
// function to calculate sum, diff, prod, quotient for given numbers
void compute(double num1, double num2, double* sum, double* diff, double* prod, double* quotient)
{
// function does not need to return these values because the variables are manipulated using pointers
*sum = num1 + num2;
*diff = num1 - num2;
*prod = num1 * num2;
*quotient = num1 / num2;
}
Execution and output:
Enter first number 4.5
Enter second number 1.2
Sum of the numbers 4.5 and 1.2 is: 5.7
Difference of the numbers 4.5 and 1.2 is: 3.3
Product of the numbers 4.5 and 1.2 is: 5.4
Quotient of the numbers 4.5 and 1.2 is: 3.75
Enter first number 2.4
Enter second number 1.2
Sum of the numbers 2.4 and 1.2 is: 3.6
Difference of the numbers 2.4 and 1.2 is: 1.2
Product of the numbers 2.4 and 1.2 is: 2.88
Quotient of the numbers 2.4 and 1.2 is: 2
--------------------------------------------------------
For your understanding, there are two techniques basically in programming languages to pass values to functions.
Pass by value:
In this the variable values are copied to function and accessed inside. Any changes for the variables inside function will not be reflected outside.
Pass by reference:
In this instead of variable values the address of variable is passed to the function which means we will be directly accessing the variables. Thats why any changes of the variable inside function will be reflected outside.
In the above case num1 and num2 variables have used "pass by value" technique whereas sum, diff, product and quotient variables have used "pass by reference" technique.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.