Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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
Correct Option: B

The program calculates nth Fibonacci Number. The statement t = fun (n-1, fp) gives the (n-1)th Fibonacci number and *fp is used to store the (n-2)th Fibonacci Number. Initial value of *fp (which is 15 in the above program) doesn't matter. Following recursion tree shows all steps from 1 to 10, for exceution of fun(5, &x).



Your comments will be displayed only after manual approval.