# How can I find out the largest number ... ?

• 05-01-2011, 06:33 PM
TheTypicalStudent
How can I find out the largest number ... ?
How can I pick out the largest number in a file ? The numbers in the file are from a range of 0.1 to 0.5 and there's are 100 of differently ranged numbers ...
• 05-01-2011, 06:47 PM
Zack
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
Maximus-EVG
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
Code:

`ArrayList<Double> yourArrayList;`
(of type double).
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
JosAH
Quote:

Originally Posted by Maximus-EVG
2. Run Collections.sort(yourArrayList);
3. Thats it! Your arrayList is now sorted and the last number is the largest one.

Sorting takes O(n*log(n)) steps while finding a maximum value would only take O(n) steps.

kind regards,

Jos
• 05-01-2011, 07:58 PM
Zack
Quote:

Originally Posted by Maximus-EVG
The above method is correct from sorting algorithm point of view (of which there are many).

My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.
• 05-01-2011, 08:17 PM
Maximus-EVG
Quote:

Originally Posted by Zack
My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.

• 05-01-2011, 09:11 PM
Zack
Quote:

Originally Posted by Maximus-EVG

Skimming can be deadly! Make sure you always drink 1%!
• 05-02-2011, 07:48 AM
TheTypicalStudent
Quote:

Originally Posted by Zack
My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.

Can you give mean example like how the code looks like ? Sorry I'm just keep up Java and I had no ideas how can i start :(
• 05-02-2011, 08:02 AM
sunde887
Most people won't give you the exact code, I will give you some pseudo code though
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
TheTypicalStudent
Quote:

Originally Posted by sunde887
Most people won't give you the exact code, I will give you some pseudo code though
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```

The array list is the array for my ranged of number ?
• 05-02-2011, 08:51 AM
sunde887
The array list is to store all the information in memory, can be done like this as well
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
wookyoo
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
TheTypicalStudent
oh ok ...
Here's the code :
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]);         }     }```
As you can see the code requires the user to type in the number ..
How can i convert :
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");                             }```
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 ?
• 05-03-2011, 06:23 AM
Zack
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
Karenthian
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