Thread: Help about recursion

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at my example, note:

    1) You call the recursive function ONE time, only. After that ONE call in main(), the recursive function calls itself the correct number of times, because you set up the base case. That tells it when it should end.

    Remove your for loop in main() which calls the recursive function.

    2) The printing is done INSIDE the recursive function, not in main().

    3) You need an if statement in the recursive function, to give it the logic when it should return. You don't have that if statement in yours yet.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Figen View Post
    you are not giving anything here...there is no interaction with user.
    That's not how functions work. You give them something (or some things), and they give you something back. For instance, if I do
    Code:
    log(10)
    I give the function the value 10, as input, and it gives me back the result, 2.302585 or so. So when you call recursion(6), you are giving the function 6, and you had better be expecting the number 728 back from it.

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Figen View Post
    Code:
    main()
    
    {
    
      int i,k=0;
    
      printf("%d,",k);
      for(i=0;i<13;i++){
      recursion(k);
      printf("%d,",k);
      }
    
    }
    
    recursion(k)
    
    {
    
    
      k=3k+2;
    
      return recursion(k);
    
    }
    How about that now?
    Not good at all. I'm not getting the impression that you have actually tried any of the code you have posted, but instead just threw it up there willy-nilly hoping somebody would take pity and write out the solution for you. First off, your examples wont even compile, which tells me you basically put in zero effort. Second, you clearly didn't follow Adak's or my examples, both of which gave you examples of a recursive function. At the very least, I would have expected you to have a base case in there, since it's been mentioned so much and is in both examples, with a comment to draw your attention to it.

    We all really want to help you, which is why we spend our spare time on this board. Most of us, however, have better stuff to do than write programs for someone else (okay, many of us do that for a living, but it's for a living). We need to see some serious effort from you if you want serious help from us. Read an article on recursion. Try out the examples Adak and I provided, then try tweaking them and see what happens to the values you get. Try modifying them to fit your need for your program.

    If you still have trouble after that, come back with a serious effort, and some specific questions, and we will help you.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Figen View Post
    Code:
    main()
    
    {
    
      int i,k=0;
    
      printf("%d,",k);
      for(i=0;i<13;i++){
      recursion(k);
      printf("%d,",k);
      }
    
    }
    
    recursion(k)
    
    {
    
    
      k=3k+2;
    
      return recursion(k);
    
    }
    How about that now?
    The first time you enter recursion, it does an orphaned math function then calls itself... and then calls itself again... and again.... there is nothing there to stop it from calling itself forever. (Which is why I avoid recursive algorythms like they have a disease... it's too easy to get a runaway.)

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    I found this thread relevant to your question about recursion...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    I found this thread relevant to your question about recursion...
    I was going to do this, but once I got started, I didn't know how to stop. I had to fork() so I could reply.


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

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh you jokers, Brewbuck and Quzah!

    Thanks for the laughs.

  8. #23
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Thumbs up

    Quote Originally Posted by brewbuck View Post
    I found this thread relevant to your question about recursion...

    Quote Originally Posted by quzah View Post
    I was going to do this, but once I got started, I didn't know how to stop. I had to fork() so I could reply.

    Quzah.


    LMAO!

  9. #24
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    A bit like if you go to Google and do a search for Recursion.
    I think you can put a signature here.

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. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM