Results 1 to 6 of 6
- 02-13-2011, 07:13 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Help with a largest sub-vector 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); } } }
- 02-13-2011, 07:20 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,404
- Blog Entries
- 7
- Rep Power
- 17
- 02-13-2011, 07:35 PM #3
Member
- 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(??)
- 02-13-2011, 08:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,404
- Blog Entries
- 7
- Rep Power
- 17
- 02-13-2011, 08:33 PM #5
Member
- 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; 02-13-2011 at 08:40 PM.
- 02-14-2011, 07:06 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,404
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Vector Problem
By osmaavenro in forum New To JavaReplies: 14Last Post: 10-01-2010, 11:03 AM -
Problem with Vector
By banhbaochay in forum New To JavaReplies: 9Last Post: 04-01-2010, 05:01 PM -
Vector problem
By Ace_Of_John in forum New To JavaReplies: 1Last Post: 01-27-2008, 08:53 PM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM -
vector problem
By mambo_jumbo in forum New To JavaReplies: 1Last Post: 11-17-2007, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks