553 views
2 votes
2 votes

Sum of the value printed by Rec(6)?

2 Answers

Best answer
1 votes
1 votes

if a ≤ 0

   return a;

but note that returned value doesn't use anywhere ====> just ignore the values if a ≤ 0

note that 'a' is a auto variable.

 

Rec(6) = print(6) Rec(4) print(6) Rec(3) ----- (1)

Rec(4) = print(4) Rec(2) print(4) Rec(1)

Rec(2) = print(2) Rec(0) print(2) Rec(-1) ===> 2 2

Rec(1) = print(1) Rec(-1) print(1) Rec(-2) ===> 1 1

∴ Rec(4) = 4  2 2 4 1 1

 

Rec(3) = print(3) Rec(1) print(3) Rec(0)

Rec(1) = print(1) Rec(-1) print(1) Rec(-2) ===> 1 1

∴ Rec(3) = 3  1 1 3

 

∴ Rec(6) = 6  4  2 2 4 1 1   6  3  1 1 3

selected by
1 votes
1 votes

 

Another method for doing these type of questions is Tree method,

go from top to down left to right and print element when encounter any pf().

No related questions found