Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Divide and conquer Algorithm

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Divide and conquer Algorithm

    I am learning divide and conquer algorithms in school and I am having some trouble figuring it out. I am required to find the sum of an array but divide and conquering an array to base case 4 then finding the sum of those 4 numbers. I think I have the basic idea of divide and conquer, but I cannot get my code below to work.

    I keep getting this error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at getSum.sumArray(getSum.java:17)
    at getSum.sumArray(getSum.java:21)
    at getSum.main(getSum.java:7)

    public class getSum {
    	static int sum = 0;
    	public static void main(String[] args) {
    			int[] numbers = {2,2,2,2,2,2,2,2};
    			int amount = 0;
    	       amount = sumArray(0,numbers.length,numbers);
    	       System.out.print(amount);
    	}
     
    	public static int sumArray(int first, int last, int[] A){
    		int index = last - first;
    		if(index == 1){
    			return sum;
    		}else if(index <= 4 && index > 1){
    			for(int i = first; first < last; i++){
    				sum += A[i];
    			}
    			return sum;
    		}
    		return (sumArray(first, last / 2, A) + sumArray(last / 2, A.length, A));
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Divide and conquer Algorithm

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at getSum.sumArray(getSum.java:17)
    The code at line 17 uses an index that is past the end of the array.
    Remember that array indexes range from 0 to the array length-1

    If you can't see how the variable's value gets its value, add some println calls to print out the values of the variables so you can see what the computer see.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with While loop to continuously divide
    By jahshuwuh in forum Loops & Control Statements
    Replies: 1
    Last Post: November 22nd, 2012, 08:35 PM
  2. While Loop to divide by 2 continiously
    By jahshuwuh in forum Loops & Control Statements
    Replies: 1
    Last Post: November 22nd, 2012, 08:13 PM
  3. How come 1 divide by 2 is less than 0.5 ?!
    By raabhim in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 01:43 PM
  4. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 06:38 PM
  5. How can i divide a string?
    By noFear in forum Java Theory & Questions
    Replies: 9
    Last Post: September 1st, 2010, 09:45 AM

Tags for this Thread