How to Correct Recursive Methods for Array Manipulations?

  • Thread starter Thread starter jsmith0476
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary
SUMMARY

The discussion focuses on correcting recursive methods for array manipulations in Java. The methods in question are computeSumAtOdd, computePositiveSum, and countNegative. Key issues identified include the handling of negative numbers in computePositiveSum and countNegative, and the incorrect accumulation of sums in computeSumAtOdd. The solutions provided emphasize the need to process all elements of the array correctly, ensuring that recursive calls return the appropriate values.

PREREQUISITES
  • Java programming language proficiency
  • Understanding of recursion and recursive methods
  • Familiarity with arrays in Java
  • Basic debugging skills for identifying logical errors in code
NEXT STEPS
  • Review Java recursion techniques and best practices
  • Learn about array manipulation methods in Java
  • Study error handling and debugging strategies in Java
  • Explore performance optimization for recursive algorithms
USEFUL FOR

Java developers, computer science students, and anyone looking to improve their skills in recursive programming and array manipulation.

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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K