Results 1 to 6 of 6
 02132011, 08:13 PM #1Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
Help with a largest subvector problem
I am a noob so be gentle.
Have to create a program to find the largest sub vector from a text file, the contents of which are below:
6,94,63,41,20,5,61,83,14,50,
36,30,32,51,96,72,99,23,59,23,
I have a program written to do this but it returns 439 instead of 458 as the largest sub vector and I can't figure out why, it also prints out 0 and 100 before the 438 and I don't know why either. Any guidance or help is much appreciated.
Code below:
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class largestSubVector { public static void main(String[] args) throws IOException { int [] data = new int [20]; String line = null; File file = new File("vector20.txt"); BufferedReader bufRdr = new BufferedReader(new FileReader(file)); while((line = bufRdr.readLine()) != null) { StringTokenizer st = new StringTokenizer(line,","); int i = 0; while (st.hasMoreTokens()) { data[i] = Integer.parseInt(st.nextToken()); //System.out.println(data[i]); i++; } int this_sum = 0, max_sum = 0; int n = 20; for (int j = 0; j < n; j++) { this_sum += data[j]; if (this_sum > max_sum) { max_sum = this_sum; } else if (this_sum < 0) { this_sum = 0; } } System.out.println(max_sum); } } }
 02132011, 08:20 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,249
 Blog Entries
 7
 Rep Power
 24
 02132011, 08:35 PM #3Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
Ok I got rid of the 0 and 100 problem just some code inside the while loop that should have been outside. Updated code below:
Java Code:package Assignment1; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class largestSubVector { public static void main(String[] args) throws IOException { int [] data = new int [20]; String line = null; File file = new File("vector20.txt"); BufferedReader bufRdr = new BufferedReader(new FileReader(file)); while((line = bufRdr.readLine()) != null) { StringTokenizer st = new StringTokenizer(line,","); int i = 0; while (st.hasMoreTokens()) { data[i] = Integer.parseInt(st.nextToken()); //System.out.println(data[i]); i++; } } int this_sum = 0, max_sum = 0; int n = 20; for (int j = 0; j < n; j++) { this_sum += data[j]; if (this_sum > max_sum) { max_sum = this_sum; } else if (this_sum < 0) { this_sum = 0; } } System.out.println(max_sum); } }
The problem wasn't well explained to me so I'm not sure if I will be able to explain it well to you. But for the numbers above the largest sub vector is the underlined values:
6,94,63,41,20,5,61,83,14,50,
36,30,32,51,96,72,99,23,59,23,
= 458
So for example in my code the 100 was coming from the 6,94, which is 100 then the 63, 41 end that sub vector. Then the next sub vector is 20,5, = 25 then 61 ends that sub vector.
I believe it's also called the largest subsequence problem(??)
 02132011, 09:00 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,249
 Blog Entries
 7
 Rep Power
 24
 02132011, 09:33 PM #5Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
can you explain to me how you are getting 439?
83 14 50 + 36 + 30 + 32 + 51 + 96 + 72 + 99 + 23 = 458?
edit: Nevermind I see where you are getting it now.
Now that I see where the 439 is coming from I should be able to fix this myself, will post the updated code in case anybody wants to see it. Thanks for your help, you indirectly got me to solve it lol. :)Last edited by caseballs; 02132011 at 09:40 PM.
 02142011, 08:06 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,249
 Blog Entries
 7
 Rep Power
 24
Similar Threads

Vector Problem
By osmaavenro in forum New To JavaReplies: 14Last Post: 10012010, 11:03 AM 
Problem with Vector
By banhbaochay in forum New To JavaReplies: 9Last Post: 04012010, 05:01 PM 
Vector problem
By Ace_Of_John in forum New To JavaReplies: 1Last Post: 01272008, 09:53 PM 
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12122007, 01:47 PM 
vector problem
By mambo_jumbo in forum New To JavaReplies: 1Last Post: 11172007, 11:44 PM
Bookmarks