Results 1 to 15 of 15
- 05-01-2011, 06:33 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
- 05-01-2011, 06:47 PM #2
Add them all to an array, then loop through the array. Store the currently highest number (start with 0.0 or -1.0) in a variable, and when one of the numbers is higher than that, replace it. Something like this...
Let's say this is your file:
0.1
0.3
0.2
0.2
0.4
0.2
0.45
0.3
So you loop through each one:
double highest = -1.0;
is 0.1 higher than -1.0? yes, so highest = 0.1
is 0.3 higher than 0.1? yes, so highest = 0.3
is 0.2 higher than 0.3? no, so highest = 0.3
is 0.2 higher than 0.3? no, so highest = 0.3
is 0.4 higher than 0.3? yes, so highest = 0.4
is 0.2 higher than 0.4? no, so highest = 0.4
is 0.45 higher than 0.4? yes, so highest = 0.45
is 0.3 higher than 0.45? no, so highest = 0.45
Of course, that's just a general idea, and that logic would be put into a loop through the array instead of line-by-line. But it should give you a good idea of what to do.
- 05-01-2011, 07:10 PM #3
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
The above method is correct from sorting algorithm point of view (of which there are many).
However, if its not necessary for you to implement your own algorithm (bubble/inline sort), you can use the standard Collections.sort() method.
Since your file contains doubles, Double objects already have a natural ordering so there is no need to write your own compareTo method.
What you need to do is:
1. Load all the numbers from the file and add them to an(of type double).Java Code:ArrayList<Double> yourArrayList;
2. Run Collections.sort(yourArrayList);
3. Thats it! Your arrayList is now sorted and the last number is the largest one.
- 05-01-2011, 07:41 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 05-01-2011, 07:58 PM #5
- 05-01-2011, 08:17 PM #6
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
- 05-01-2011, 09:11 PM #7
- 05-02-2011, 07:48 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
- 05-02-2011, 08:02 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Most people won't give you the exact code, I will give you some pseudo code though
Java Code:declare array list loop read line parse line //split if necessary store in array list end loop declare max value loop if condition max = i end if end loop return max
- 05-02-2011, 08:17 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
- 05-02-2011, 08:51 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The array list is to store all the information in memory, can be done like this as well
Java Code:loop read line split line loop if condition reassign max end if end loop end loop return max
- 05-02-2011, 10:34 PM #12
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
well... for the question
I declare two variables like result and compare value.
also i used one loop and in the loop two values are compared
if the compare is higher than the result then store result as compare value.
i will show you just little bit more specific code but not exact one...
value /* string value from the file or buffer */
result /* convert value to double */
value /* string value from the file or buffer */
compare /* convert value to double and it is same way as result one */
loop /* like for loop until there is no number anymore in the file or loop 98 times since your file has 100 numbers and got two numbers already. */
if(result < compare)
{
result = compare;
}
value /* string value from the file or buffer */
compare /* convert value to double and it is same way as result one */
}
finally show the result one.
- 05-03-2011, 03:43 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
oh ok ...
I had found this code something in google:
Here's the code :
As you can see the code requires the user to type in the number ..Java Code:{ Scanner input=new Scanner(System.in); System.out.println("Enter positive real numbers: "); Integer array[]=new Integer[10]; for(int i=0;i<array.length;i++){ array[i]=input.nextInt(); } System.out.println("Sorted Array in reverse order:"); Arrays.sort(array,Collections.reverseOrder()); for(int i=0;i<array.length;i++){ System.out.println(array[i]); } }
How can i convert :
As this code, the number(round) would be printed out into the file. How can this code to be modify so that it will be similar to the above ?Java Code:for (int j = 0; j < bitSet2.length() - 1 || j < result1.length() - 1; j++) { if (bitSet2.get(j)) { writer.append("1"); writer.append(","); writer.flush(); } else { writer.append("0"); writer.append(","); writer.flush(); } } nlast = (nlast / (ntarget + nresult - nlast)); float round = Round(nlast, 3); writer.append(round); writer.append("\n"); }
- 05-03-2011, 06:23 AM #14
Excuse me for sounding old fashioned, but why don't you write your own code based on the concepts and pseudocode provided? That way, you won't get in trouble for plagiarism, you will have actually learned the material, and you will feel better about having accomplished something.
- 05-03-2011, 11:09 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
See I agree, writing your own code would be far better here. I think the idea of cycling through the array with a conditional element (is it bigger than current biggest) would be the best idea for larger arrays. On small scale things the difference in required processes won't do much, but larger scale will take longer if you sort it first and then go for the last number.
Also, sorting required the Java sort method, using the conditional you write the whole thing yourself so you can feel smart ;)
~Karenthian
Similar Threads
-
Find the second largest number in the array
By radhi16 in forum New To JavaReplies: 5Last Post: 01-13-2011, 05:08 PM -
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Help with Binary recursive method to find the largest integer
By flyingcurry in forum New To JavaReplies: 12Last Post: 10-31-2010, 06:14 PM -
Find max number in 2D array?
By spatel14 in forum New To JavaReplies: 3Last Post: 06-30-2010, 04:27 PM -
Find a number from a string
By florentp in forum New To JavaReplies: 2Last Post: 03-20-2009, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks