Results 1 to 3 of 3
  1. #1
    ashplusham is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default 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[101];
    
    
    //Read the input in and store it in an array
    		InputStreamReader input =  new InputStreamReader(System.in);
    		BufferedReader buff = new BufferedReader(input);
    		String line = buff.readLine();
    		try{
    			while(line.equals("0") == false && i<100){
    				i++;
    				line = buff.readLine();
    				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);
    			}
    		}
    	}
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.

  3. #3
    ashplusham is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Recursive method help?

    Hey quad,

    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.

Similar Threads

  1. Recursive Method
    By emschorsch in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 07:01 AM
  2. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  3. recursive method
    By michail in forum New To Java
    Replies: 0
    Last Post: 01-31-2010, 02:50 PM
  4. Writing a recursive method :S
    By Thousand in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 04:13 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 05:45 AM

Tags for this Thread

Posting Permissions

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