Thread: Recursion Function explain...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Recursion Function explain...

    First please view below codes...

    Code:
    #include <stdio.h>
    
        int strange (int n);    
    
        main(void)
        {    
            int n = 4, result;
    
            result = strange (n);
    
            printf("%d", result);
    
        }
    
        int strange (int n)
            {    
            
                int ans;
                if (n ==1)
                    ans = 0;
                else
                    ans = 1 + strange (n/2);
                return (ans);
            }
    I was so confuses with recursion function...I mean how could the output will be 2? I hope that someone could explain how recursion works in details to me please? Very thanks you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you tried tracing the code yet? Note down the value of n and ans for the current call of strange.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    If you follow your code properly it will be 2. Starting off with n = 4, since n != 1 you send it back through and divide it by 2. So now n = 2, but since its != 1 you send it back through again and divide it by 2. Now n = 1 so you return 0. As you follow your code back up you return 1 + 0 = 1. Then going up another step you return 1 + 1 which = 2 thus your final answer is 2. What did you expect it to return?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Ya, of course I try to trace this program before but I still can't obtain the correct output...Now I understand some but why should I return
    1 + 0 = 1 then going up to 1 + 1 again ?

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Ok...now I fully understand how this program works....but I face another more difficult recursion

    Code:
    #include <stdio.h>
    
        int FUNCTION (int n);    
    
        main(void)
        {    
            int n = 6, valid;
    
            valid = FUNCTION (n);
    
            printf("%d", valid);
    
        }
    
        int FUNCTION (int n)
            {    
                if ( n == 1)
                    return 1;
                else if (n<= 0)
                    return 0;
                else
                    return ( FUNCTION ( n - 2) + FUNCTION (n - 1) ) ;
            }
    I try to understand this program clearly and trace out...but I still doesn't get why the output is 8 but not 9?

  6. #6
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Brush up again ur concepts....u are mugging up and not getting the concepts...if u FULLY UNDERSTAND THE PREVIOUS ONE...then this is also of the same type
    Code:
    n=6
    FUNCTION ( n - 2) + FUNCTION (n - 1)
    =FUNCTION ( 4) + FUNCTION (5)
    =2FUNCTION (4) + FUNCTION (3)
    =2{FUNCTION (2)+FUNCTION (3)} + FUNCTION (3)
    =2FUNCTION (0)+2FUNCTION (1)+FUNCTION (2)} + FUNCTION (2) + FUNCTION (1)
    =2FUNCTION (0)+2FUNCTION (1)+FUNCTION (2)} + FUNCTION (0) + FUNCTION (1) + FUNCTION (1)
    =2(0+2+1)+0+1+1
    =6+1+1
    =8

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Ok...I try to understand all the solution you post to me but I seems can't understand start from line 4. I mean where the 2 come from? And why the 5 will become 3...? Sorry if it sounds like I didn't try my best to understand it but I really does try to understand it...

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Maybe try an easier example:
    Code:
    int SOME_FUNC(int n)
    {
        if (n <= 1)
            return 1;
        else
            return n * SOME_FUNC(n-1);
    }
    
    int main(void)
    {
        int result = SOME_FUNC(5);
        printf("%d", result);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Siaw Ys View Post
    Ok...I try to understand all the solution you post to me but I seems can't understand start from line 4. I mean where the 2 come from? And why the 5 will become 3...? Sorry if it sounds like I didn't try my best to understand it but I really does try to understand it...
    u have to break each n every
    FUNCTION(n)=FUNCTION(n-1)+FUNCTION(n-2)

    so
    FUNCTION(5)=FUNCTION(4)+FUNCTION(3)

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    And for reference, here's a non-recursive version of SOME_FUNC:
    Code:
    int SOME_FUNC(int n)
    {
        int output = 1;
        while (n > 1)
        {
            output *= n;
            --n;
        }
        return output;
    }
    If you can understand this one, then you'll understand what the other one is doing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain the following function
    By mahor1989 in forum C++ Programming
    Replies: 3
    Last Post: 07-24-2011, 11:18 PM
  2. Explain: A template function is not a function
    By AnishaKaul in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2010, 10:58 AM
  3. Can someone explain to me what this function is doing?
    By mr_coffee in forum C Programming
    Replies: 7
    Last Post: 09-05-2008, 12:18 PM
  4. can anyone explain the gets() function?
    By Ryan_P in forum C++ Programming
    Replies: 5
    Last Post: 05-18-2002, 01:17 PM