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 12 of 12

Thread: MaxSum algorithm with negative values in Array

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MaxSum algorithm with negative values in Array

    How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file?
    The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them. Thanks

     case 1:
     
                  int sum;
              	System.out.print("Enter list of comma-delimeted integers: ");
              	Scanner scan = new Scanner(System.in);
              	 String input2=scan.next();
                  String[] num = input2.split(",");
                  int[] a= new int[num.length];
                  for (int i = 0; i < num.length; i++) {
                          a[i] = Integer.parseInt(num[i]);
                  }
     
      			sum =maxSum(a);
    case 2:
                        BufferedReader reader = null;
                        Scanner sc = null;
                        String line = "";
                        try {
                           reader = new BufferedReader(new FileReader("integers.txt"));
                           sc =new Scanner(new BufferedReader(new FileReader("integers.txt")));
     
                           while (sc.hasNext()) {
                              line = sc.next();
                           }
                           String s[] = line.split(",");
                           a = new int[s.length];
                           for (int i = 0; i < s.length; i++) {
                              a[i] = Integer.parseInt(s[i]);
                           }
                        }
                        finally {
                           if (reader != null) {
                              reader.close();
                           }
     
                        }  
                      System.out.println(line);
                      sum=maxSum(a)

     public static int maxSum(int [] a)
          {
        	  int maxSum = 0;
        		 int this_sum=0;
        		 int i=0;
        		  for(int j=0; j< a.length; j++)
        		  { 
     
        		    		  this_sum += a[j];
        		    	  if(this_sum > maxSum)
        		    	  {  maxSum = this_sum;
     
        		    	  }
        		    	  else if( this_sum<0)
        		    	  {
        		    		  i=j+1;
        		    	  this_sum=0;
        		    	  }
        		      }
     
        		  return maxSum;
           }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    lets talk about your method public static int maxSum(int [] a)
    can you explain what is that doing? a comment maybe.

    As what I understand with your problem, you want to get the sum of your integer array right? I don't get what's that public static int maxSum(int [] a) is all about? there is a conditional statement inside the loop, what is it checking? and why?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MaxSum algorithm with negative values in Array

    The maxSum is given the problem is the implementation of it but what it is is basically an algorithm that prints out the sum of the
    integer array. It works as is but it won't let me input negative integers for the sum.
    Definition: The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    so if I input 1,2,3,-4,5,-6 the result is 1? is it right?

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MaxSum algorithm with negative values in Array

    In theory yes it should but the result of that is actually 7??? I have no clue where the numbers are coming from but
    1,2,3,4,5,6 prints out 21

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    7 in your code? how come that it should be 7? I'm sorry, I'm a little bit confused on your requirements

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MaxSum algorithm with negative values in Array

    No you were correct it should be 1 but its printing out 7, the code just prints out the sum you had the correct idea but when adding negatives it warps the result into a strange result

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    that is because of your conditional statement in public static int maxSum(int [] a) try to remove them and just put a max_sum += the array element. that should do it

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MaxSum algorithm with negative values in Array

    I'm not supposed to change that algorithm, I can only change the case statements that call upon it

  10. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    why you cannot change that? Actually that is the reason why you got a wrong sum. And there is also one nonsense statement i=j+1;, that statement is completely nonsense. you just equated i to j + 1 and then ignored it.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MaxSum algorithm with negative values in Array

    You were right, i inputted the algorithm incorrectly...now the data file in case 2 is still posting incorrectly, heres the updated algorithm
     {
        	  public static int maxSum(int[] a, int n)
        	  int max_sum = 0;
              for (int i = 0; i < n; i++) {
                 for (int j = i; j < n; j++) {
                    int this_sum = 0;
                    for (int k = i; k <= j; k++) {
                       this_sum += a[k];
                    }
                    if (this_sum > max_sum) {
                       max_sum = this_sum;
                    }
                 }
              }
              return max_sum;
          }

    Then I call it
    maxSum(a, a.length)

  12. #12
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: MaxSum algorithm with negative values in Array

    I'm still a little bit confused on what your method maxSum is really doing. can you explain what does that method can do?
    and please explain what each statement doing. you can put a comment. because as what I understand in requirement, you just need to get the sum of the elements in the array so I don't really get the sense of making nested loop and another loop inside that nested loop and making a condition of comparing the two sum (this_sum and max_sum).

Similar Threads

  1. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  2. Java algorithm to print all the combination of the integer array
    By ashish12169 in forum Algorithms & Recursion
    Replies: 11
    Last Post: May 2nd, 2013, 05:55 PM
  3. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  4. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM