Thread: Re: Recursive function

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Unhappy Re: Recursive function

    Dear All,

    The following segment is a recursive function returns the sume of an integer array given its length.

    int sumArray(int anArray[], int length) {
    if (length == 0)
    retyrb 0;
    return anArray[length] + sumArray(anArray, length);
    }

    Can anyone tell me where is the erros and how to revise it.

    Thanks a lot.
    Wah

  2. #2
    Unregistered
    Guest
    There are a few possible problems:

    1) array[length].... is "length" the number of elements in the array? If so, I think the first call should be array[length-1] since an array with 5 elements has indexes numbered 0 thru 4.

    2) each time you recursively call this, you are passing the same value (length). That means each time, it will add the same element in the array again, and you may get stuck in a loop. You should probably decrement the index before each recursive call.

    Maybe try changing it to

    "return anArray[length-1] + sumArray(anArray, length-2); "

    ?????

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The end-condition of recursion is length==0, but since length doesn't decrease, the end-condition will never be reached.

    Further, the first element of an array has index 0. So if length==0, it will return 0 instead of the value at first position.

    Code:
    int sumArray (int anArray[], int length) 
    { 
        if (length < 0) 
            return 0;
        return anArray [length] + sumArray (anArray, length-1); 
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >return anArray [length] + sumArray (anArray, length-1);

    anArray[length] would be out-of-bounds
    Code:
    int sumArray (int anArray[], int length) 
    { 
        if (length == 0) 
            return 0;
        return anArray [length-1] + sumArray (anArray, length-1); 
    }
    is better

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It won't work, since you'll never reach the first element of the array. But I'd not care about the out-of-bound, since anArray[-1] won't happen. If length==-1, then the function will simply return 0.

    But perhaps, this is better:

    Code:
    int sumArray (int anArray[], int length) 
    { 
        if (length == 0)
            return anArray [length];
        else
            return anArray [length] + sumArray (anArray, length-1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM