# Thread: Array[] get smallest/largest value

1. Member
Join Date
Aug 2010
Posts
28
Rep Power
0

## 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.

Java Code:
```Double array[] = new Double[10000];

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

Java 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?

2. 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:
Java 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!

3. Senior Member
Join Date
Jun 2008
Posts
2,562
Rep Power
8
Arrays.sort() and take the first and last.

4. Member
Join Date
Aug 2010
Posts
28
Rep Power
0
I've tried both of those methods, but I don't think I've translated them correctly.

Java 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) {

5. Senior Member
Join Date
Jun 2008
Posts
2,562
Rep Power
8
You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.

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

7. This returns an error
Please copy and paste the full text of the error here.

8. Member
Join Date
Aug 2010
Posts
28
Rep Power
0
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.

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

9. 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.

10. 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...

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

Java 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:
Java Code:
`{12, 15, 3, 5}`
It will say that 5 is the largest in the array, because 5 is larger than 3.

Example:
Java 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:
Java 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!

11. Member
Join Date
Aug 2010
Posts
28
Rep Power
0
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.

12. The code line you posted does not compile.
What error message do you get when you try to compile it?

13. Member
Join Date
Aug 2010
Posts
28
Rep Power
0
Unresolved compilation problems:
Type mismatch: cannot convert from double to int

14. Senior Member
Join Date
Jun 2008
Posts
2,562
Rep Power
8
Originally Posted by bobocheez
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.
First of all by using a double array rather than an int array. Secondly, even if you did use the int values of those doubles (i.e. drop everything after the decimal point) you could still compare them to doubles.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•