Thread: recursion

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

    recursion

    Hi all i have problems to understand recursion .Here are two programms.Could someone tell me how they work?

    1.
    Code:
    #include <stdio.h>
    
    void up_and_down(int);
    
    main( )
    {
    up_and_down(1);
    }
    
    void up_and_down(n);
    int n;
    {
    printf ("Στάδιο %d\n", n);
    if (n < 4)
    up_and_down(n+1);
    printf ("Στάδιο %d\n", n);
    }
    2.
    Code:
    #include <stdio.h>
    
    int summing(int);
    
    main()
    {
    int k=0;
    
    k = k + summing(1);
    printf("%d\n", k);
    }
    
    int summing(n)
    int n;
    {
    int j=0;
    
    if (n++ < 4)
    j = summing(n)+ n;
    return j;
    }

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'll just explain recursion.
    Code:
    void blah(void)
    {
        int n = 2+2;
        // THIS is recursion:
        blah();
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    recursion

    1.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void up_and_down(int);
    
    main( )
    {
    
    up_and_down(1);
    system("PAUSE");
    return(EXIT_SUCCESS);
    
    }
    
    void up_and_down(int n)
    {
    
    printf ("step %d\n", n);
    
    if (n < 4) {
       up_and_down(n+1);
    }   
    
    printf ("step %d\n", n);
    
    }
    Output is:

    step 1
    step 2
    step 3
    step 4
    step 4
    step 3
    step 2
    step 1

    At first call of function
    up_and_down(1) will print "step 1" and will call up_and_down(2) then print again "step 1"
    up_and_down(2) prints out "step 2" and calls up_and_down(3)
    .....
    step 1 [first printf up_and_down(1)]
    -step 2[first printf up_and_down(2)]
    -----step 3[first printf up_and_down(3)]
    ---------step 4[first printf up_and_down(4)]
    ---------step 4[second printf up_and_down(4)]
    -----step 3[second printf up_and_down(3)]
    -step 2[second printf up_and_down(2)]
    step 1[second printf up_and_down(1)]

    In recursion a function creates a loop by calling itself within.
    Condition if(n<4) makes the function to stop at the moment when n=4 [recursion stops]

    Runs first printf then it is call up_and_down(n+1) wich will run again first printf, after it finishes, second printf will run from last call [n, n-1, ...1]
    Last edited by cflorinel; 01-27-2005 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No one is going to tell you how a program works (especially if there are errors in the code for said program). Do a board search for posts by HelpMePlease and you'll find all about this...

    The idea behind recursion is that if you have an algorithm that requires an operation to be done multiple times, it may be appropriate for a function to call itself. Once more, do a board search, and you'll find many examples of how recursion is used.

    edit: cflorinel - please refer to the announcement at the top of the forum, and learn how to use code tags.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can you not know how that works? It tells you how it works when it runs.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Quote Originally Posted by Kleid-0
    I'll just explain recursion.
    Code:
    void blah(void)
    {
        int n = 2+2;
        // THIS is recursion:
        blah();
    }
    It is quite important for recursion to have an ending condition.
    Above code will loop infinetly.
    Second problem with this modification
    instead of k = k + summing(1);
    put k = k + summing(0);
    or k = 1 + summing(1);
    Will recursively compute 1+2+....+n = (n(n+1))/2

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cflorinel
    It is quite important for recursion to have an ending condition.
    Yes.

    Quote Originally Posted by cflorinel
    Above code will loop infinetly.
    No. theoretically, yes. In reality, no. You'll run out of stack space and the program will crash.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    for: quzah and sean_mackrory.
    Errors within the script were corrected for the first problem in my post.
    I can see you are for quite some time in this forum and you can't even modify 2 lines to make it work. I suppose aulmedic555 is a begginer and he is looking for help, not for critics about code.
    PS: I modified my code with CODE tag - I'm not familar with vBulletin.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I suppose aulmedic555 is a begginer and he is looking for help, not for critics about code.
    We have indeed been on the forum for a long time. That's why we recognize the signs of someone looking for help when they haven't put in the effort themselves.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just shut the I am sillyI am sillyI am sillyI am silly up. See what happens is people grab some code off of Google or some place, and without even trying to figure out what it does, they stick it up here, have people tell them what it does, so they can turn it in for homework or something. Then some idiot like you comes along and does all the work for them.

    It's OBVIOUS they didn't write the code. It's also obvious that they didn't even try to figure it out. Hell, all they had to do is compile it and run it, and it would have TOLD THEM what it did.

    It's also obvious that YOU didn't read the forum announcements before posting. If you had, you wouldn't have needed to edit your post, because you would have been shown how to use code tags. So don't come in here and try to start talking big, or you'll get a beating.

    For the record, most people who just show up and post some random code and say "How does this work?" are NOT looking for help. They're looking for someone to do everything for them, just like you did.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You guys we've probably had this arguement before like 5000e^71283 times! Let's just let the river flow instead of building dams that keep breaking.

  12. #12
    .
    Join Date
    Nov 2003
    Posts
    307
    I think it's frustration - about 80% of the stuff on this forum is from under-motivated types looking for shortcuts.

    People who don't read forum guidelines seem to also fall into this category more often than not.

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Let them have free time while we figure out the problems. It's a win-win situation! We get stronger in programming, and they have fun doing something else, hehehe.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity. To try and stem the flow, so that those few who actually seek knowledge may make it to the shores of it safely, and not drown in the never ending sea of said ignorance. So that they might find a foothold or purchase to pull themselves with great effort to some beacon of hope, some refuge, and not be dragged down into the swirling abyss of stupidity, clawed at by the masses who would take them down with them. So I valiantly set the right from the wrong, a beacon of light in the dark chasm of inanity...

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity. To try and stem the flow, so that those few who actually seek knowledge may make it to the shores of it safely, and not drown in the never ending sea of said ignorance. So that they might find a foothold or purchase to pull themselves with great effort to some beacon of hope, some refuge, and not be dragged down into the swirling abyss of stupidity, clawed at by the masses who would take them down with them. So I valiantly set the right from the wrong, a beacon of light in the dark chasm of inanity...
    Excuse my ignorance but did you just come up with that or is that a quote I don't recognize?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM