How they calculated the output of the code

This is question of my midterm exam and it says that I should predict the output. Xcode says that it is 9 4 3, but I do not get how they calculated this and could please help me by giving me a step by step instruction. Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void mystery2(int &a, int b, int &c)
{
   a = b + c;
   b = a + c;
   c = a + b;
}
int main() {
   int a, b, c;
   a = 1;
   b = 2;
   c = 3;
   mystery2(b, c, a);
   cout << a << " " << b << " " << c << endl;
}
Who is Xcode?
Your question has nothing common with programming. It is a simple arithmetic. Did you learn arithmetic?!
Last edited on
@vlad


Anyway, what's the output?
please run the code and hopefully you can see what is going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;
// 2 // 3 // 1
 void mystery2(int &a, int b, int &c)
{
   cout << "b is " << b << endl; // 3
   cout << "c is " << c << endl; // 1
   // the value of 4 is retained by a in memory
   a = b + c;
   cout << "a is " << a << endl;

   cout << endl;
   cout << "a is " << a << endl; // now a is 4
   cout << "c is " << c << endl; // c is 1 still
   b = a + c;
   cout << "b is " << b << endl; // b is now 5 in the fucntion.

   cout << endl;
   cout << "a is " << a << endl; // a is still 4
   cout << "b is " << b << endl; // b is still 5
   c = a + b;
   cout << "c is " << c << endl;  // c is now 9
   cout << endl;
}

int main() {
   int a, b, c;
   a = 1;
   b = 2;
   c = 3;

   cout << "b is " << b << ", c is " << c << " , a is " << a << endl;
   cout << endl;
   // i think your confusion is because the variables are switched here.
   // input 2, input 3, input 1
   mystery2(b, c, a);

   // passing by reference means you do not make a copy and you are modifying the value in that memory address
   // &a and &c are passed by reference
   // b is passed by value
   // passing by value means you make a copy inside the function if you do not return the value
   // it will go out of scope and is destroyed this gives the impression the value did not change. but you infact did change it.
   cout << a << " " << b << " " << c << endl;

}
Last edited on
It is very simple. The initial values of variables are a = 1, b = 2, c = 3.
The function is called as

mystery2(b, c, a);

So its parameters a, b, c will have values a = 2 b = 3 c = 1.
Inside the body of the function we have

a = b + c; => a = 3 + 1 = 4
b = a + c; => b = 4 + 1 = 5
c = a + b; => c = 4 + 5 = 9

As the parameters are defined as references so variables a, b, c in main will have this values.

a = 4;
b = 5;
c = 9;
@vlad


Thank you very much! I just discovered that while figuring out the output, the logic I applied was correct(and I know passing by value and by reference). But I had absent mindlessly read the statement mystery2(b, c, a) as mystery2(a, b, c), and that's where my fault was. Anyway, Thanks again.
Thank you so much I did not realize that it would change to a=2 b=3 c=1 !! You have been very helpful!! thank you again!
Oh, I am sorry. I did not see that the second parameter is not defined as reference.:) So in main the initial value of b will not be changed, In this case the result will be

a = 4; (the parameter is reference)
b = 2; ( the parameter is value )
c = 9; ( the parameter is reference)
That to get the correct result it will be useful to rewrite the function call as


mystery2(2, 3, 1) ;

instead of

mystery2(b, c, a);

So now it is clear enough that function parameters have values

a = 2;
b = 3;
c = 1;

Last edited on
THANK YOU!!!!! <3 <3 <3
Topic archived. No new replies allowed.