1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Arrays[Java]

Discussion in 'Java' started by cyrow, Apr 17, 2008.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.io.*;
    import java.util.*;
    
    class Comp2004{
    	static int[]results;
    	public static void main(String[] args){
    		
     //write a method which returns the largest sum of any consecutive integers in the array
    		
    		int []num = {27,6,-50,21,3,14,16,-8,42,33,-21,9};
    		
    		int []results = new int[100];
    		
    		int val = LargestSum(num,results);
    		
    		System.out.println("The largest value is :"+ val);	
    	}//main
    	public static int LargestSum(int[]num,int[]results){
    		int x = 2;//start for loop to run twice,then 3 times etc.
    		int n = num.length;
    		int sum = 0;
                                    int large = 0;
    		
    	
    		int j = 0;
    		while(x < n){
    			for(int i = 0; i < x; i++){
    				sum = sum + (num[i] + num[i + 1]);
    				
    			}
    			results[j] = sum;
                                                    if(results[j] > results[large]) large = results[j];
    			x++;
                                                    j++;
    		}//end while
    	return large;
    	}//end function
    	}//class
    
    Problem
    I know the largest consecutive sum is 115 by adding up 21, -3, 14, 16, 8, 42 and 33;
    The code is not generating an error but the result is incorrect. I connot figure what is wrong. Help!
     

Share This Page