What will print when the following code is executed I know w
What will print when the following code is executed? I know what will print but my question is how to get that without throwing it in a compiler, like a step by step guide.
What will be the output when the following code is executed.
#include<iosstream>
using namespace std;
void A( int, int , int);
void B( int, int , int);
void C( int, int , int);
void WhatWillIPrint(int, int, int);
int main()
{
int a=1, b=2, c=3;
WhatWillIPrint(a, b, c);
return 0;
}
void A(int a, int c, int b)
{
cout << a+5 <<\" ** \"<< b+3 <<\" ** \"<< c+7;
}
void B(int b, int c, int a)
{
A(c+1, a+1, b);
}
void C(int a, int b, int c)
{
B(b+1, a, c+1);
}
void WhatWillIPrint(int a, int b, int c)
{
C(a, b, c);
}
Solution
Let us go through the code. In main method we have
WhatWillIPrint(a, b, c); function called
It is now like WhatWillIPrint(1, 2, 3);
Go to function : void WhatWillIPrint(int a, int b, int c)
It has C(a, b, c);
Values from WhatWillIPrint function are passed to this.
It is now like C(1, 2, 3)
Go to function : void C(int a, int b, int c)
It has B(b+1, a, c+1);
Values from C function are passed. Do math. Positions are changed. Be careful while considering the parameters.
It is now like B(2+1, 1, 3+1) -> B(3, 1, 4)
Go to function : void B(int b, int c, int a)
It has A(c+1, a+1, b);
It is now like A(1+1, 4+1, 3) -> A(2, 5, 3)
Go to function : void A(int a, int c, int b)
A(int a, int c, int b)
a = 2
c = 5
b = 3
cout << a+5 <<\" ** \"<< b+3 <<\" ** \"<< c+7;
a+5 = 2+5 = 7
b+3 = 3+3 = 6
c+7 = 5+7 = 12
Substituting these values in cout, output is mentioned below
7 ** 6 ** 12

