Weird Recursive Function :: C++

This is a discussion on Weird Recursive Function :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I am having trouble getting a recursive function to work right. Here is the criteria: ----- Variables: X, Y ...

  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,136
    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-17-2006, 12:35 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09: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

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