The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+12 votes
1.9k views

Consider the program below:

#include <stdio.h>
int fun(int n, int *f_p) {
    int t, f;
    if (n <= 1) {
        *f_p = 1;
        return 1;
    }
    t = fun(n-1, f_p);
    f = t + *f_p;
    *f_p = t;
    return f;
}

int main() {
    int x = 15;
    printf("%d/n", fun(5, &x));
    return 0;
}

The value printed is:

  1. $6$
  2. $8$
  3. $14$
  4. $15$
asked in Algorithms by Veteran (59.6k points)
edited by | 1.9k views

3 Answers

+12 votes
Best answer
The answer is $B$.

Let the address of $x$ be $1000.$

$1$.$f(5,1000)$ $=$ $8$

$2$.$f(4,1000)$ $=$ $5$

$3$.$f(3,1000)$ $=$ $3$

$4$.$f(2,1000)$ $=$ $2$

$5$.$f(1,1000)$ $=$ $1$.

The evaluation is done from $5$ to $1$. Since recursion is used.
answered by Boss (19.7k points)
edited by
0
i didnt understand somebody elaborate?
0
@Apsi_R_Osa see my answer . It may help
+10 votes

Answer 8 . Option B

answered by Boss (22.4k points)
+9 votes

Note:- Before calling the function the rest of the code are first push into stack then we are going to execute function definition.

Plz follow the below diagram carefully.

Hence 8 is the ans.
answered by Boss (23k points)
0
This is helpful. Thank-you
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,688 questions
48,654 answers
156,499 comments
63,963 users