1 votes 1 votes Consider the following C function: int f(int n) { static int r = 0; If (n < = 0) return 1; If (n > 3) { r = n; return f (n – 2) + 2; } return f(n – 1) + r; } What is the value of f(5)? (A) 3 (B) 7 (C) 9 (D) 18 Programming in C programming-in-c functions + – dhairya asked Jul 12, 2016 • edited Jul 12, 2016 by pC dhairya 2.6k views answer comment Share Follow See 1 comment See all 1 1 comment reply asu commented Jul 12, 2016 reply Follow Share ans is 18 0 votes 0 votes Please log in or register to add a comment.
Best answer 2 votes 2 votes Ans D sh!va answered Jul 12, 2016 • selected Jul 12, 2016 by pC sh!va comment Share Follow See 1 comment See all 1 1 comment reply dhairya commented Jul 12, 2016 reply Follow Share When n=0 then we just need to return 1; so why have u added with 5 i.e. 5........?? 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes f(5) [n>3 so execute if block ] | [r=n so r=5] return f(3) + 2 (16+2=18.........returns 18) | return f(2) + r (11+r=11+5=16...returns 16) | return f(1) + r (6+r=6+5=11...returns 11) | return f(0) + r (here f(0) returns 1 and r=5 so total returned =6 ) and go from bottom to up | (here f(0) returns 1) so finally f(5)=18 asu answered Jul 12, 2016 • edited Jul 13, 2016 by asu asu comment Share Follow See all 6 Comments See all 6 6 Comments reply dhairya commented Jul 12, 2016 reply Follow Share In f(0) why have u added ''r'' ......?? If (n < = 0) return 1; here n=0; so it will simply return 1....?? and " return f(n – 1) + r;" will come in else part of " If (n < = 0) return 1; " ..so that wont be executed... I know i am wrong but plzz explain how.. 0 votes 0 votes asu commented Jul 13, 2016 reply Follow Share clear now??? 0 votes 0 votes dhairya commented Jul 13, 2016 reply Follow Share no....:'( return f(0) + r why u did this....?? Why have u added r with f(0)..?? In the question it is written that when { if( n<=0 ) return 1; } And here n=0 ryt.....?? So why have u added " r "....? 0 votes 0 votes asu commented Jul 13, 2016 reply Follow Share for f(1) {n>3 ..false... so else part executed } | f(0)+r(this is the return value of f(1)) now find the value of f(0){ here n=0 and returns 1... 0 votes 0 votes dhairya commented Jul 13, 2016 reply Follow Share .thank u soooooooooooooooooooooooo much @asu...... 0 votes 0 votes asu commented Jul 13, 2016 reply Follow Share u r welcome............ 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes The answer is Option D. Pankaj kumar answered Jul 12, 2016 Pankaj kumar comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes All procedure calls are mapped w.r.t. stack, so f(0) : r := 5 : returns 1 f(1) : r := 5 : Return f(0)+r f(2) : r := 5 : Return f(1)+r f(3) : r := 5 : Return f(2)+r f(5) : r := 5 : Return f(3)+2 Solve from top to bottom and get answer D(18). Michail answered Jul 12, 2016 Michail comment Share Follow See all 0 reply Please log in or register to add a comment.