How to Correct Recursive Methods for Array Manipulations?

  • Thread starter jsmith0476
  • Start date
  • Tags
    Arrays
In summary, the code in the attempt at a solution does not do the correct work when it returns from the recursive call.
  • #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?
 
Physics news on Phys.org
  • #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 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.
 
  • #3
Thank you! I got them all to work! Your help is very much appreciated
 

Related to How to Correct Recursive Methods for Array Manipulations?

1. What are recursive methods for arrays?

Recursive methods for arrays are functions that call themselves to solve a problem by breaking it down into smaller sub-problems. In the context of arrays, recursive methods can be used to perform operations on each element of the array, such as finding the sum or maximum value.

2. How do recursive methods work with arrays?

Recursive methods work with arrays by breaking down the array into smaller sub-arrays and performing the desired operation on each sub-array. This continues until the base case is reached, which is when the sub-array has only one element. The results are then combined to get the final result.

3. What are the advantages of using recursive methods for arrays?

One advantage of using recursive methods for arrays is that they can simplify complex problems into smaller, more manageable sub-problems. They also tend to be more concise and elegant compared to iterative methods. Additionally, recursive methods can save memory space by not creating new arrays.

4. Can recursive methods be used for any type of array?

Yes, recursive methods can be used for any type of array, including one-dimensional, two-dimensional, and multi-dimensional arrays. The key is to identify the base case and the appropriate operation to be performed on each sub-array.

5. Are there any limitations to using recursive methods for arrays?

One limitation of using recursive methods for arrays is that they can be less efficient compared to iterative methods, especially for large arrays. This is because recursive methods involve function calls and memory allocation, which can take up more time and resources. Additionally, recursive methods can also lead to stack overflow errors if the base case is not properly defined.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
985
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top