edited by
860 views
1 votes
1 votes
main()

{

int i, x=5, y=10;

for(i=1;i<=2;i++)

{

y+=f(x) + g(x);

printf("%d", y);

}
}

f(int x)

{

int y;

y=g(x);

return (x+y);

}

g(int x)

{

static int y=5;

y=y+7;

return (x+y);

}

 

Kindly explain how recursion is working along with the solution.
edited by

3 Answers

Best answer
2 votes
2 votes

In the main variable i ,x,y are declared and x=5 ,y=10

now for i=1,first iteration will be there,


i=1

y+=f(x)+g(x)=>y=y+(f(5)+g(5)) ( for x=5 )

now f(5) will be called ,value 5 is copied to the local variable x inside the function f (x),now x=5 and y is a local variable defined inside the function f so scope of x and y will be limited to function f (x) .

here y=g(x)=>y=g(5) ,now g(5) will be called ,

value 5 is copied to local variable x inside the function g(x) and here y is static local variable so its scope is local but its lifetime is till the program ending.Here y=5 now y=y+7 is executed so value of y becomes 12,then x+y =17 will be returned.

so y=g(5)=17, now x+y from g(x) will be returned means 22 will be returned.

now g(5) will be called ,in 5 is copied to x,means x=5 and y is static variable and its value is 12 now ,after executing y=y+7 value of y becomes 19 ,now x+y will be returned means 24 .

finally for first iteration y=10+22+24=56.


now i will be incremented and becomes i=2 here x=5 and y=56

y+=f(x)+g(x)=>y=y+(f(5)+g(5)) ( for x=5 )

now f(5) will be called ,value 5 is copied to the local variable x inside the function f (x),now x=5 and y is a local variable defined inside the function f so scope of x and y will be limited to function f (x) .

here y=g(x)=>y=g(5) ,now g(5) will be called ,

value 5 is copied to local variable x inside the function g(x) and here y is static local variable so its scope is local but its lifetime is till the program ending.Here y=19 (previous value) now y=y+7 is executed so value of y becomes 26,then x+y =31 will be returned.

so y=g(5)=31, now x+y from g(x) will be returned means 36 will be returned.

now g(5) will be called ,in 5 is copied to x,means x=5 and y is static variable and its value is 26 now ,after executing y=y+7 value of y becomes 33 ,now x+y will be returned means 38

finally for first iteration y=56+36+38=130


now i will be incremented and becomes 3 ,now condition is false so will come out of the loop

finally ,y=56 for i=1 and y=130 for i=2

       .

 

selected by
0 votes
0 votes

If main is like :-

main()

{

int i, x=5, y=10;

for(i=1;i<=2;i++)

{

y+=f(x) + g(x);

printf("%d", y);
}
}

-------------------------------------------------------------------------------

then, ans would be:- 56 and 130 for two iteration.

Related questions

0 votes
0 votes
1 answer
1
Sambhrant Maurya asked Jul 15, 2018
347 views
0 votes
0 votes
1 answer
2
Sambhrant Maurya asked Jul 15, 2018
203 views
0 votes
0 votes
2 answers
3
Sambhrant Maurya asked Jul 15, 2018
335 views
0 votes
0 votes
2 answers
4
Sambhrant Maurya asked Jul 13, 2018
362 views
A(n){if(n<1)return 1;elsereturn A(n-2) + B (n-1);}B(n){if(n<=1)return 1;elsereturn B(n-1) + A(n-2);}What is the output for A(6)?Please show how the recursion is working h...