Recursion Help

This is a discussion on Recursion Help within the C++ Programming forums, part of the General Programming Boards category; I am reading about recursion and I understand that it there is two versions of it: indirect and direct and ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:53 PM
  5. Recursion
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2002, 08:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21