Ask Question

What is the output of the following program fragment:

void find (int a, int& b, int& c)

{

int temp;

c = a + b;

temp = a;

a = b;

b = 2 * temp;

}

int main ()

{

int x, y, z;

x = 10;

y = 20;

z = 25;

find (x, y, z);

cout<< x <<" "<< y <<" "<< z <

return 0;

}

Output:

+5
Answers (1)
  1. 23 June, 15:12
    0
    Output of the given code is: 10 20 30.

    Explanation:

    Here in function find (int a, int& b, int& c), three parameters are passed, firstis passed by value and rest are passed by reference. When a parameter is passed by value, the any changes made by the other function doesn't reflects in the main function. And when a value is passed by reference, then any changes made by other function will reflects in the main function also.

    Here find is called with (10,20,25). So in the function, c=a+b i e. c=10+20. Here b, c are passed by reference. So any change in b, c will change the value of y, z in main. temp=10, then a=b i. e a=20, and b=2*temp That is b=2*10. Here value of a=20, b=20 and c=30. Since b, c are passed by reference then it will change the value of y, z in main function but a will not change the value of x.

    Hence the value of x=10, y=20 and z = 30 after the function call.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What is the output of the following program fragment: void find (int a, int& b, int& c) { int temp; c = a + b; temp = a; a = b; b = 2 * ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers