Thread: Recursive method help?

1. Member Join Date
Nov 2012
Posts
4
Rep Power
0 Recursive method help?

Hi all,

I have an assignment for a class to read integers (until 0 is entered) from standard input into an array (including 0), then compute the minimum, sum of positive numbers, sum of numbers at even indexes and count the number of odd numbers using recursion. I've written my code but all of my answers result in 0. Can anybody help? (Sorry if my format is a little off, this is my first post!)

Java Code:
import java.io.*;

public class Assignment9 {
public static void main (String args[]) throws IOException{
int i = 0;
int[] numbers;
numbers = new int;

//Read the input in and store it in an array
String line = buff.readLine();
try{
while(line.equals("0") == false && i<100){
i++;
numbers[i] = Integer.parseInt(line);
}
}
catch(IOException exception){
System.out.println("exception");
}

//declare variables to call recursive methods
int min = findMin(numbers, 0, i);
int pSum = computePositiveSum(numbers, 0, i);
int eSum = computeSumAtEven(numbers, 0, i);
int odd = countOdd(numbers, 0, i);

//Print the results
System.out.print("The minimum number is " +min +"\n");
System.out.print("The sum of positive numbers is " +pSum +"\n");
System.out.print("The sum of the numbers at even indexes is " +eSum +"\n");
System.out.print("The total count of odd numbers is " +odd);
}

//Recursive method for findMin
public static int findMin(int[] numbers, int startIndex, int endIndex){
if(startIndex == 0){
if(endIndex > numbers[startIndex]){
return numbers[startIndex];
}else{
if(endIndex > numbers[startIndex]){
endIndex = numbers[startIndex];
startIndex --;
}
}
}
return findMin(numbers, startIndex, endIndex);
}
//Recursive method for computePositiveSum
public static int computePositiveSum(int[] numbers, int startIndex, int endIndex){
if(startIndex == endIndex){
if(numbers[startIndex] < 0){
return 0;
}else{
return numbers[startIndex];
}
}else if(numbers[endIndex] > 0){
return computePositiveSum(numbers, startIndex, endIndex - 1) +numbers[endIndex];
}else{
return computePositiveSum(numbers, startIndex, endIndex - 1);
}
}
//Recursive method for computeSumAtEven
public static int computeSumAtEven(int[] numbers, int startIndex, int endIndex){
if(startIndex == endIndex){
if(startIndex %2 == 0){
return numbers[startIndex];
}else{
return 0;
}
}else{
if(endIndex %2 == 0){
return computeSumAtEven(numbers, startIndex, endIndex - 1) + numbers[endIndex];
}else{
return computeSumAtEven(numbers, startIndex, endIndex - 1);
}
}
}
//Recursive method for countOdd
public static int countOdd(int[] numbers, int startIndex, int endIndex){
if(startIndex == endIndex){
if(numbers[startIndex] %2 == 1){
return 1;
}else{
return 0;
}
}else{
if(numbers[startIndex] %2 == 1){
return 1 + countOdd(numbers, startIndex + 1, endIndex);
}else{
return countOdd(numbers, startIndex + 1, endIndex);
}
}
}
}  Reply With Quote

2. Re: Recursive method help?

So, you haven't tried using a debugger yet. Either try that out and step through the execution line by line, or add a bunch of System.out.println's at various points and see where you are going wrong. You are probably overwriting a value with an assignment or breaking the input or something.  Reply With Quote

3. Member Join Date
Nov 2012
Posts
4
Rep Power
0 Re: Recursive method help?

Thanks for the quick reply, and I apologize for my delayed response! I just ran a debugger and found some of my several problems, so thanks for that advice! I don't know why I didn't think of that myself! Again, I'm sorry for my much delayed response.  Reply With Quote

Tags for this Thread

recursion  Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•