Thread: Weird Recursive Function :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Weird Recursive Function :: C++

    Hi.

    I am having trouble getting a recursive function to work right.

    Here is the criteria:

    -----
    Variables: X, Y

    if X == 0
    return 2X

    if X>= 1 && Y == 0
    return 0;

    if X >= 1 && Y == 1
    return 2;

    if X >= 1 && Y >= 2
    return return calculateAck(X, calculateAck(X, Y - 1)
    -----

    Here is the code:
    -----

    int CAckermann::calculateAck(int randNumX, int randNumY){ if (randNumX == 0) return (2 * randNumY); else if (randNumX >= 1 && randNumY == 0) return 0; else if (randNumX >= 1 && randNumY == 1) return 2; else if (randNumX >= 1 && randNumY >= 2) return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));}

    -----

    The code above works except when it executes the last else-if statement.

    Am I doing something wrong? Do I need to deincrement one of the variables before passing it into a function?

    Thanks,
    Kuphryn

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Why do you decrement x when you pass it to the recursive function. It isn't in your pseudo code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Recursive Ackermann's function - need help
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2002, 04:42 AM