# Array[] get smallest/largest value

• 09-09-2010, 06:54 AM
bobocheez
Array[] get smallest/largest value
Hi,
How would you determine the smallest and largest value if you have the following:

These are double numbers and there can be many arrays, as many as the user inputs.

Code:

```Double array[] = new Double[10000]; array[i] = n;```
That would turn out, for example:

Code:

```array[0] = 5 array[1] = 1 array[2] = 9 array[3] = 6```
How can we make the computer find the lowest number, which is 1? What about the highest number, 9?
• 09-09-2010, 07:06 AM
Zack
Make a loop through, from i = 0 to i < length of array, to loop through every item of the array.

Keep a variable, declared at the start of the loop, which sets to the current number if it's higher/lower than its previous value. For example, here is some pseudocode:
Code:

```arr = { 1, 7, 36, 2, 9, 18 }; somevar = -1; for (i in (0, arr.length - 1)) {     if (arr[i] > somevar) {         somevar = arr[i];     } } // Here, somevar is equal to the highest value of the array.```

You can do the same for lowest value, using something like 9999999 instead of -1 as somevar's initial value (and checking < instead of >). Of course, this is just pseudocode, but you should have no problem translating it to Java.

Good luck!
• 09-09-2010, 07:30 AM
Arrays.sort() and take the first and last.
• 09-09-2010, 01:35 PM
bobocheez
I've tried both of those methods, but I don't think I've translated them correctly.

Code:

```array[0] = 5 array[1] = 1 array[2] = 9 array[3] = 6         Double somevar = -1.0;         for (int index = 0;index < array.length-1; index++) {             if (array[i] > somevar) {                 somevar = array[i];                 System.out.println("This is somevar high = " +somevar);             }                     }```
This returns an error at if (array[i] > somevar) {
• 09-09-2010, 01:50 PM
You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.
• 09-09-2010, 01:57 PM
JosAH
Quote:

You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.

The way the OP translated the other tip, I do believe it. ;-)

kind regards,

Jos
• 09-09-2010, 03:00 PM
Norm
Quote:

This returns an error
Please copy and paste the full text of the error here.
• 09-09-2010, 07:49 PM
bobocheez
Quote:

Originally Posted by JosAH
The way the OP translated the other tip, I do believe it. ;-)

kind regards,

Jos

I know PHP which is kind of like a simplified JAVA. But it turns out that Double can't be ordered. It has to be an int.

What I ended up doing is comparing each value that was input to the previous value.

Code:

```                                        if (i > 0) {                                                 int itest = i-1;                                                 if (array[i] > array[itest]) {                                                         largest = array[i];                                                 }                                         }```
Thanks for the support
Cheers
• 09-09-2010, 07:51 PM
Fubarable
Quote:

Originally Posted by bobocheez
I But it turns out that Double can't be ordered. It has to be an int.

Since when has this been true? You may want to reconsider this.
• 09-09-2010, 08:00 PM
Zack
Quote:

Originally Posted by bobocheez
I know PHP which is kind of like a simplified JAVA. But it turns out that Double can't be ordered. It has to be an int.

I have to agree with Fubarable here. That's not true...

Quote:

Originally Posted by bobocheez
What I ended up doing is comparing each value that was input to the previous value.

Code:

```                                        if (i > 0) {                                                 int itest = i-1;                                                 if (array[i] > array[itest]) {                                                         largest = array[i];                                                 }                                         }```
Thanks for the support
Cheers

Not really sure why you did it this way. This only compares it to the preceding value. Let's say you have this array:
Code:

`{12, 15, 3, 5}`
It will say that 5 is the largest in the array, because 5 is larger than 3.

Example:
Code:

```                int[] array = {12, 15, 3, 5};                 int largest = -9999;                 for (int i = 0; i < array.length; i++) {                         if (i > 0) {                                 int itest = i-1;                                 if (array[i] > array[itest]) {                                         largest = array[i];                                 }                         }                 }                 System.out.println("Bobo's method: "+largest);                 largest = -9999;                 for (int i = 0; i < array.length; i++) {                         if (array[i] > largest) {                                 largest = array[i];                         }                 }                 System.out.println("Zack's method: "+largest);```
Output:
Code:

```run: Bobo's method: 5 Zack's method: 15```

Hopefully you see why your method does not work as you described. If not, let me know and I can explain further. Cheers!
• 09-09-2010, 10:42 PM
bobocheez
Yea I know that my code does not accomplish the desired result. I got an A for it though.

What if you have
int[] array = {12.1, 15.9, 3.0, 5.0};
I don't understand how you would compare those values with Double.
• 09-09-2010, 11:09 PM
Norm
The code line you posted does not compile.
What error message do you get when you try to compile it?
• 09-09-2010, 11:14 PM
bobocheez
Unresolved compilation problems:
Type mismatch: cannot convert from double to int
• 09-09-2010, 11:16 PM