Thread: Recursion Help

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Recursion Help

    I am reading about recursion and I understand that it there is two versions of it: indirect and direct and understand that part however when I try to apply recursion to a problem (involving fractions) I get confused and start getting ........ed off how i cannot wrap my mind around it...

    Any ideas on how to learn this?

  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
    Write your answer without recursion to begin with.

    There really isn't anything mysterious, it is just a function call disguised as a loop.

    Eg, two strlen functions.
    Code:
    int mylen ( const char *s ) {
      int i;
      for ( i = 0 ; *s++ ; i++ );
      return i;
    }
    
    int mylen ( const char *s ) {
      if ( *s == '\0' ) return 0;
      else return 1 + mylen(s+1);
    }
    In the case of strlen, the base case is when the length is zero, and the answer is therefore "obvious".
    Otherwise, the recursive step is to solve part of the problem (string is at least 1 char long), then call recursively to work out the length of the tail of the string.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 08:03 AM
  2. Recursion
    By casper7 in forum C Programming
    Replies: 19
    Last Post: 07-14-2008, 07:51 PM
  3. can someone help me out with recursion
    By JOsephPataki in forum C Programming
    Replies: 10
    Last Post: 05-13-2003, 04:55 PM
  4. Recursion
    By scottmanc in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 03:53 PM
  5. Recursion
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2002, 08:33 PM