# [SOLVED] Help Finding the Minimum value of an array

• 04-18-2009, 05:19 PM
MSteinman
[SOLVED] Help Finding the Minimum value of an array
I was wondering how to find the minimum value in the array. Here is my code as follows:
Code:

```public class MDS05_02 {           public static void main(String[] args){         double[] array1 = new double[8] ;         array1[0] = 1;         array1[1] = 2;         array1[2] = 4;         array1[3] = 5;         array1[4] = 10;         array1[5] = 100;         array1[6] = 2;         array1[7] = -22;         }        }```
Thanks
• 04-18-2009, 06:20 PM
OrangeDog
You will probably find the method Math.min(), the less-than operator (<) and a for loop helpful here.
• 04-18-2009, 06:41 PM
AngrYkIdzrUlE
Quote:

Originally Posted by MSteinman
I was wondering how to find the minimum value in the array. Here is my code as follows:
Code:

```public class MDS05_02 {           public static void main(String[] args){         double[] array1 = new double[8] ;         array1[0] = 1;         array1[1] = 2;         array1[2] = 4;         array1[3] = 5;         array1[4] = 10;         array1[5] = 100;         array1[6] = 2;         array1[7] = -22;         }        }```
Thanks

First you let your first value in the array "be" the minimum value/ smallest then you go through the array and compare with the value then if there is a smaller one, return that as minimum.

e.g

Code:

```double minimum = array1[0]; //sets the first to be the smallest for (int i = 0; i < array1.length; i++) //goes through your array {     if (array1[i] < array1[0]) //checks and replaces if necessary     {         minimum = array[i];          } } System.out.println( minimum ); //returns the value of the smallest```
PS: Click rep if this helped you and dont forget to mark as solved
• 04-18-2009, 07:01 PM
CJSLMAN
huh... not so sure...
hey angrykid... you sure that works? You're only checking against one element (array[0]). Shouldn't the comparartion be against minimum variable? See what happens in the following case:
Code:

```array1[0] = 7; array1[1] = 5; array1[2] = 6;```
1st loop, minimum = 7
2nd loop, minimum = 5
3rd loop, minimum = 6

Luck,
CJSL
• 04-18-2009, 07:06 PM
AngrYkIdzrUlE
Quote:

Originally Posted by CJSLMAN
hey angrykid... you sure that works? You're only checking against one element (array[0]). Shouldn't the comparartion be against minimum variable? See what happens in the following case:
Code:

```array1[0] = 7; array1[1] = 5; array1[2] = 6;```
1st loop, minimum = 7
2nd loop, minimum = 5
3rd loop, minimum = 6

Luck,
CJSL

The logic is assume the first is the smallest/minimum then going through the loop to compare, if there is a smaller number then it replaces the first value if not the first value IS the minimum.

PS: the 3rd loop is incorrect, the value should remain 5, please post your code if there is a problem i might point it out.

Regards
• 04-18-2009, 07:26 PM
CJSLMAN
Angry... you have not run your own program and haven't carefully read my post. Please, run the following and see what the result is:
Code:

```public class MinimumArray { public static void main(String args[]) { double[] array1 = new double[3] ; array1[0] = 7; array1[1] = 5; array1[2] = 6; double minimum = array1[0]; //sets the first to be the smallest for (int i = 0; i < array1.length; i++) //goes through your array {     if (array1[i] < array1[0]) //checks and replaces if necessary     {         minimum = array1[i];          } } System.out.println( minimum ); //returns the value of the smallest } }```
The above is an implementation of the code you suggested. It returns 6.0 as result.

CJSL
• 04-18-2009, 07:30 PM
AngrYkIdzrUlE
i think that's what you wanted it to return since 6.0 is the smallest value in your array...or maybe i dont understand your question, please make it clear
• 04-18-2009, 07:36 PM
CJSLMAN
huh?
I'm tired of trying to explain to you that your suggested code has a flaw/error in it.
Code:

`if (array1[i] < array1[0])`
The above code is wrong.
Code:

`if (array1[i] < minimum)`
The above is the correction.

CJSL
• 04-18-2009, 10:02 PM
AngrYkIdzrUlE
Quote:

Originally Posted by CJSLMAN
I'm tired of trying to explain to you that your suggested code has a flaw/error in it.
Code:

`if (array1[i] < array1[0])`
The above code is wrong.
Code:

`if (array1[i] < minimum)`
The above is the correction.

CJSL

Whoa!!!...no need to get "tired" Mr. Chris :) ....thanks for pointing that out. I hope the person who initially asked for help got something out of this.
Thanks again, Chris. ;)
• 04-18-2009, 10:14 PM
CJSLMAN
OK, no problem... probably a miscommunication... I also hope th OP has found this thread helpful...

Luck,
CJSL
• 04-19-2009, 07:04 AM
AustinDoggie
Quote:

Originally Posted by MSteinman
I was wondering how to find the minimum value in the array. Here is my code as follows:
Code:

```public class MDS05_02 {           public static void main(String[] args){         double[] array1 = new double[8] ;         array1[0] = 1;         array1[1] = 2;         array1[2] = 4;         array1[3] = 5;         array1[4] = 10;         array1[5] = 100;         array1[6] = 2;         array1[7] = -22;         }        }```
Thanks

Would this work?

Code:

```public double findSmallest() {     double tempNumber = array1[0];     for(double number : array1) {           if(array1[number] < tempNumber) {               tempNumber = number;           }     }     return tempNumber; }```
That should return the lowest number in that array.
• 04-19-2009, 08:00 AM
AngrYkIdzrUlE
Quote:

Originally Posted by AustinDoggie
Would this work?

Code:

```public double findSmallest() {     double tempNumber = array1[0];     for(double number : array1) {           if(array1[number] < tempNumber) {               tempNumber = number;           }     }     return tempNumber; }```
That should return the lowest number in that array.

i dont quite understand this line
Code:

`for(double number : array1)`
what does the loop do??
• 04-19-2009, 08:27 AM
AustinDoggie
That is the for each loop.

You can read about it if you go to google and type "java sun loops" and it should be the first link (the for-each loop)

In Psudo code it would look like this
Code:

```for each(elementType elementName in collection) {     //do this to each elementType }```
or
Code:

```for(int number : intArray) {     //do this to each word }```
Where it says int, you could put whatever type of elements your collection (array) holds (such as int, String, char, Double etc.) Then, name that (such as number, word, letter, etc.). Then you put ":" and after that you put the name of your colletion (such as array1 or intArray).

Does that clear it up? If not, remember that google search I told you about above.

EDIT: I tried it and I'm having some difficulties using the for each loop for this. I think it is because for each loops don't work for primitive arrays; I forgot about that. If you had an ArrayList it would probably work. But since you have a primitive array, I just suggest you use a while loop:

Example:

Code:

```int index = 0; int lowest = 0; while(index < (array1.length() - 1)) {     if(array1[index] < array1[index + 1]) {           lowest = array1[index];           index++;     } System.out.println(lowest); }```
This code would print out the lowest int in array1.
• 04-19-2009, 09:22 AM
AngrYkIdzrUlE
Quote:

Originally Posted by AustinDoggie
That is the for each loop.

You can read about it if you go to google and type "java sun loops" and it should be the first link (the for-each loop)

In Psudo code it would look like this
Code:

```for each(elementType elementName in collection) {     //do this to each elementType }```
or
Code:

```for(int number : intArray) {     //do this to each word }```
Where it says int, you could put whatever type of elements your collection (array) holds (such as int, String, char, Double etc.) Then, name that (such as number, word, letter, etc.). Then you put ":" and after that you put the name of your colletion (such as array1 or intArray).

Does that clear it up? If not, remember that google search I told you about above.

EDIT: I tried it and I'm having some difficulties using the for each loop for this. I just suggest you use a while loop:

Example:

Code:

```int index = 0; int lowest = 0; while(index < (array1.length() - 1)) {     if(array1[index] < array1[index + 1]) {           lowest = array1[index];           index++;     } }```

Wow, didnt know the for-each loop....try the for loop instead then...
Code:

```for (initialisation; checks for consistency; update) {  //code goes here }```
check it on the above scripts posted... good luck
• 04-19-2009, 09:25 AM
AustinDoggie
edit: nevermind.
• 04-19-2009, 06:42 PM
DavidG24
hey mate,

this may be a more inefficient way of handling the task, but for arrays of small dimensions, why not use

int smallest = sort(yourarry)[0]; //??

Otherwise I would simply use

private int find_smallest(int[] myarray){
int smallest = myarray[0];
for(int i : myarray)
smallest = i < smallest ? i : smallest;

return smallest;
}

Sorry I have put the code in the proper fashion...just joined the site, don't know how to do it!

Hope this helps,

David
• 04-19-2009, 07:06 PM
neilcoffey
P.S. The for/in loop does work with arrays (primitive or otherwise)!
• 04-19-2009, 07:48 PM
MSteinman
solved
Hey guys.
Thanks for all the replys. I did get the result I desired. Now I understand more clearly. Thanks
• 04-21-2009, 10:14 AM
beezerbutt
Arrays.sort(array1);
System.out.println("Minimum value was:["+array1[0]+"]");