1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Recursive methods for arrays

  1. Apr 7, 2009 #1
    1. The problem statement, all variables and given/known data
    Ok. I am supposed to write a recursive method for the following:
    1) public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)
    - finds the sum at all the odd indexes of the array
    2) public static double computePositiveSum(double[] numbers, int startIndex, int endIndex)
    - finds the sum of all positive numbers of the array
    3) public static int countNegative(double[] numbers, int startIndex, int endIndex)
    - determines the count of negative numbers in the array



    3. The attempt at a solution
    public static double computeSumAtOdd(double [] numbers, int startIndex, int endIndex)
    { double previousSum = 0;
    double result = 0;
    if (startIndex > endIndex)
    return result;
    if(startIndex == endIndex)
    return numbers[startIndex];

    else
    {
    double a = computeSumAtOdd(numbers,startIndex+2, endIndex);

    result = previousSum + a;
    return result;
    }

    }
    public static double computePositiveSum(double [] numbers, int startIndex, int endIndex)
    {
    double positiveSum = 0;
    if (startIndex==endIndex)
    if(numbers[startIndex]>0)
    return numbers[startIndex];
    else
    return 0;
    else
    if(numbers[startIndex]>0)
    positiveSum = numbers[startIndex] + computePositiveSum(numbers, startIndex + 1,endIndex);
    return positiveSum;

    }
    public static int countNegative(double [] numbers,int startIndex, int endIndex)
    {
    int negCount = 0;
    if(startIndex==endIndex)
    if (numbers[startIndex]<0)
    return 1;
    else
    return 0;
    else if(numbers[startIndex]<0)
    negCount = 1 + countNegative(numbers, startIndex+1, endIndex);
    return negCount;

    }

    i thought i had these right, but they are not working. can anyone point me in the right direction with any of them?
     
  2. jcsd
  3. Apr 7, 2009 #2
    You're almost there. A few comments about two things that immediately stood out to me.

    For computePositiveSum, look closely at what happens in the very last if statements. Basically, for computePositiveSum, consider the case that you are not at the end (startIndex != endIndex) and the first number is negative. So basically you get to if(numbers[startIndex]>0) and the test fails and thus you "return positiveSum;" which is, at this point, 0. Basically, you are forgetting to deal with the case of processing the rest of the array when the number is negative.

    A similar problem exists with countNegative.

    Now, computeSumAtOdd needs a little longer explaination. Perhaps a simple example will convey the issue. Consider [ 1 2 3 ]. This will take two calls, the initial call and one recursive call. What your code does is sets previousSum_1 (indicating previousSum in the first call) to zero, and gets all the way to the recursive call. Then, the recursive call will return the value 3. Now, the original call picks up right where it left off with a_1 = 3. This is added to previousSum_1: result = previousSum_1 + a_1 = 0 + 3 = 3. The problem here is that you aren't doing the correct work when you return from your recursive call. When you return, what you need to do is not return just what the recursive call returns but add on the element that the calling iteration was supposed to process. I think this is what you may have been trying with "previousSum", but, if you were, the variable name doesn't really denote that.

    Hope that helps a bit.
     
  4. Apr 7, 2009 #3
    Thank you!! I got them all to work! Your help is very much appreciated
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Recursive methods for arrays
  1. Function and array (Replies: 26)

  2. Recursive function (Replies: 16)

  3. Recursion Tree (Replies: 3)

  4. Recursive calls (Replies: 15)

Loading...