Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Arrays[Java] (http://www.go4expert.com/forums/arraysjava-t10097/)

cyrow 17Apr2008 18:26

Arrays[Java]
 
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!


All times are GMT +5.5. The time now is 09:51.