GATE CSE
First time here? Checkout the FAQ!
x
+4 votes
865 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 (64.5k points)  
edited by | 865 views

3 Answers

+6 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 Veteran (19.1k points)  
selected by
i didnt understand somebody elaborate?
@Apsi_R_Osa see my answer . It may help
+4 votes

Answer 8 . Option B

answered by Veteran (21.3k points)  
+2 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 Veteran (18.6k points)  
Answer:

Related questions



Top Users Sep 2017
  1. Habibkhan

    6362 Points

  2. Warrior

    2234 Points

  3. Arjun

    2212 Points

  4. nikunj

    1980 Points

  5. manu00x

    1726 Points

  6. SiddharthMahapatra

    1718 Points

  7. Bikram

    1716 Points

  8. makhdoom ghaya

    1660 Points

  9. A_i_$_h

    1552 Points

  10. rishu_darkshadow

    1512 Points


25,991 questions
33,561 answers
79,413 comments
31,029 users