- #1
jsmith0476
- 6
- 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?