Thread: Question about Recursive Functions

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    Question about Recursive Functions

    Show the exact screen-output of each of the following programs:

    c)

    Code:
    #include <stdio.h>
    int f (int a)
    {
    if (a== 1)
    return 1;
    else if (a==2)
    return 3;
    else 
    return (f(a-1) + f(a-2));
    } //end of function
    void main ()
    {
    int n;
    printf ("Enter n:\n");
    scanf ("%d", &n);
    printf("%d", f(n));
    }
    Hint: Assume entered value of n = 6


    answer:18
    ===========================================


    Code:
    #include<stdio.h>
    
     
    
    int myfunction(int p, int q)
    
    {
    
    if (p == q)
    
    return p;
    
    else if (p < q)
    
    return myfunction(q - p, p);
    
    else
    
    return myfunction(p - q, q);
    
    }
    
     
    
    void main()
    
    {
    
    int result;
    
    result=myfunction(20,4);
    
    printf("%d",result);
    
    }
    answer:4




    my question is how to get the answer????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main ()
    main returns an int - see the FAQ

    Also learn how to indent code with some consistency. All this "to the left" indentation is much too difficult to follow.

    > my question is how to get the answer?
    You mean other than running it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jack999
    Show the exact screen-output of each of the following programs:

    c)

    Code:
    #include <stdio.h>
    int f (int a)
    {
    if (a== 1)
    return 1;
    else if (a==2)
    return 3;
    else 
    return (f(a-1) + f(a-2));
    } //end of function
    void main ()
    {
    int n;
    printf ("Enter n:\n");
    scanf ("%d", &n);
    printf("%d", f(n));
    }
    Hint: Assume entered value of n = 6


    answer:18
    Code:
    f(6) = 
    f(5)                             + f(4) = 
    f(4)               + f(3)        + f(3)        + f(2) =
    f(3)        + f(2) + f(2) + f(1) + f(2) + f(1) +   3 =
    f(2) + f(1) +   3  +   3  +   1  +   3  +   1  +   3 = 
      3  +   1  +   3  +   3  +   1  +   3  +   1  +   3 =
    
    
    
    18
    See if you can answer the other one now.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    Code:
    f(6) = 
    f(5)                             + f(4) = 
    f(4)               + f(3)        + f(3)        + f(2) =
    f(3)        + f(2) + f(2) + f(1) + f(2) + f(1) +   3 =
    f(2) + f(1) +   3  +   3  +   1  +   3  +   1  +   3 = 
      3  +   1  +   3  +   3  +   1  +   3  +   1  +   3 =
    i didnot get it

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sigh... after I put all that effort into aligning my post.

    Do you at least understand the following?

    1. f(6) = f(5) + f(4)
    2. f(5) = f(4) + f(3)
    3. f(4) = f(3) + f(2)
    4. f(3) = f(2) + f(1)
    5. f(2) = 3
    6. f(1) = 1

    If you can understand all of those, then it is simply a matter of substitution. If you can't...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    Code:
    1. f(6) = f(5) + f(4)
    2. f(5) = f(4) + f(3)
    3. f(4) = f(3) + f(2)
    4. f(3) = f(2) + f(1)
    5. f(2) = 3
    6. f(1) = 1
    i got it
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to programming, help with recursive functions.
    By HitchHiker in forum C Programming
    Replies: 6
    Last Post: 09-22-2008, 12:53 PM
  2. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  3. A question about pointers in functions
    By occams razor in forum C Programming
    Replies: 3
    Last Post: 05-15-2007, 12:16 AM
  4. question about c functions
    By LowLife in forum C Programming
    Replies: 1
    Last Post: 11-16-2005, 12:20 PM
  5. .h and functions question
    By Mick D in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2005, 12:53 AM