Thread: [SOLVED] Help Finding the Minimum value of an array

1. Member
Join Date
Mar 2009
Posts
6
Rep Power
0

[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:
Java 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

2. You will probably find the method Math.min(), the less-than operator (<) and a for loop helpful here.

3. Originally Posted by MSteinman
I was wondering how to find the minimum value in the array. Here is my code as follows:
Java 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

Java 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
Last edited by AngrYkIdzrUlE; 04-18-2009 at 06:13 PM. Reason: small modification

4. 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:
Java Code:
```array1[0] = 7;
array1[1] = 5;
array1[2] = 6;```
1st loop, minimum = 7
2nd loop, minimum = 5
3rd loop, minimum = 6

Luck,
CJSL

5. 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:
Java 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

6. 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:
Java 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

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

8. huh?

I'm tired of trying to explain to you that your suggested code has a flaw/error in it.
Java Code:
`if (array1[i] < array1[0])`
The above code is wrong.
Java Code:
`if (array1[i] < minimum)`
The above is the correction.

CJSL

9. Originally Posted by CJSLMAN
I'm tired of trying to explain to you that your suggested code has a flaw/error in it.
Java Code:
`if (array1[i] < array1[0])`
The above code is wrong.
Java 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. ;)

10. OK, no problem... probably a miscommunication... I also hope th OP has found this thread helpful...

Luck,
CJSL

11. Originally Posted by MSteinman
I was wondering how to find the minimum value in the array. Here is my code as follows:
Java 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?

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

12. Originally Posted by AustinDoggie
Would this work?

Java 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
Java Code:
`for(double number : array1)`
what does the loop do??

13. 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
Java Code:
```for each(elementType elementName in collection) {
//do this to each elementType
}```
or
Java 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:

Java 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.
Last edited by AustinDoggie; 04-19-2009 at 08:22 AM.

14. 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
Java Code:
```for each(elementType elementName in collection) {
//do this to each elementType
}```
or
Java 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:

Java 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...
Java Code:
```for (initialisation; checks for consistency; update)
{
//code goes here
}```
check it on the above scripts posted... good luck

15. edit: nevermind.
Last edited by AustinDoggie; 04-19-2009 at 06:58 PM.

16. Member
Join Date
Apr 2009
Posts
49
Rep Power
0
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

17. Senior Member
Join Date
Nov 2008
Posts
286
Rep Power
9
P.S. The for/in loop does work with arrays (primitive or otherwise)!

18. Member
Join Date
Mar 2009
Posts
6
Rep Power
0

solved

Hey guys.
Thanks for all the replys. I did get the result I desired. Now I understand more clearly. Thanks

19. Member
Join Date
Feb 2009
Location
South Africa
Posts
18
Rep Power
0
Arrays.sort(array1);
System.out.println("Minimum value was:["+array1[0]+"]");

Posting Permissions

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