Thread: Recursions

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Recursions

    Hello, i took a test and had a problem with a function. I was wondering if someone could possibly explain the correct way of finding the answer. I have a final exam on this tomorrow and am still very confused! thanks for the help guys

    Code:
    for the function of p below, give the output of the call p(5)
    void p( int x)
    {
    if (x<=0) return;
    if (x%2==0) cout <<x<<endl;
    p(x-1);
    if (x%2 !=0) cout x<<endl;
    }
    
    
    for the function p below give the output of the call p(3)
    
    void p (int x)
    {
    cout <<x<<endl;
    if (x> 0) {p(x-1);p(x-1);}
    }
    for the first one i got only 5 but itshould be 4 2 1 3 5 and i have no idea how to figure that out

    the second one should be 321001002100100

    if any of you can explain how this concept works i would greatly appreciate it

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just follow the flow of the program and write down each step.

    I'll start with the first one.

    Call p with x = 5
    If x < 0 return (5 < 0 is false so don't return)
    If x &#37; 2 == 0 then output x (5 % 2 is 1 so don't output 5)
    Call p with new x = current x - 1 (in other words call p with x = 4)
    If x < 0 return (4 < 0 is false so don't return)
    If x % 2 == 0 then output x (4 % 2 is 0 so output 4)
    ... and so on

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    i am starting to understand the first part of it going from 4 3 ect i dont understand how it goes back up? i figured once u got to a lower number like that it cant have an output of 5 at the end. the second one i am not understanding at all.. the first one i am seeing it more clearly

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It goes back up as the call stack unwinds because your function prints old values of x before it calls p again and also before it returns.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i dont understand how it goes back up?
    Post your step by step. What happens after you execute a return?

    >> the second one i am not understanding at all..
    Post your step by step like I did. It will be easier to help you understand where you are confused.

  6. #6
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    i will help a little since i can understand iterative ways better than recursive ones
    #1
    Code:
    void p( int x) {
        while (x>0) {
            if (x&#37;2 == 0) cout << x << endl;
            --x;
            if (x%2 != 0) cout << x << endl;
        }
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But iteration just has one function call, recursion has several.
    The problem is that in recursion, when the function calls itself, it doesn't jump to top. Instead, a new function of the same type is generated you might say, and this new one starts from the top and goes down. When that new function is done, it goes back to the old one again.
    Hope that gives some insights into the whole. Try running a debugger or writing it down on a paper and it might make some more sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what are RECURSIONS....???
    By ajayd in forum C Programming
    Replies: 4
    Last Post: 02-16-2008, 10:42 PM
  2. Recursions killing me
    By salvadoravi in forum C Programming
    Replies: 12
    Last Post: 01-29-2008, 04:48 AM
  3. Recursions
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:12 PM
  4. Replies: 3
    Last Post: 11-17-2001, 09:16 AM
  5. Recursions....please help
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 01:54 PM