How to Correct Recursive Methods for Array Manipulations?

  • Thread starter Thread starter jsmith0476
  • Start date Start date
  • Tags Tags
    Arrays
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
jsmith0476
Messages
5
Reaction score
0

Homework Statement


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



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?
 
Physics news on Phys.org
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 explanation. 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.
 
Thank you! I got them all to work! Your help is very much appreciated