Results 1 to 19 of 19
- 04-18-2009, 04:19 PM #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:
ThanksJava 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; } }
- 04-18-2009, 05:20 PM #2
You will probably find the method Math.min(), the less-than operator (<) and a for loop helpful here.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 05:41 PM #3
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
PS: Click rep if this helped you and dont forget to mark as solvedJava 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 smallestLast edited by AngrYkIdzrUlE; 04-18-2009 at 06:13 PM. Reason: small modification
- 04-18-2009, 06:01 PM #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:
1st loop, minimum = 7Java Code:array1[0] = 7; array1[1] = 5; array1[2] = 6;
2nd loop, minimum = 5
3rd loop, minimum = 6
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-18-2009, 06:06 PM #5
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, 06:26 PM #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:
The above is an implementation of the code you suggested. It returns 6.0 as result.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 } }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-18-2009, 06:30 PM #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
- 04-18-2009, 06:36 PM #8
huh?
I'm tired of trying to explain to you that your suggested code has a flaw/error in it.
The above code is wrong.Java Code:if (array1[i] < array1[0])
The above is the correction.Java Code:if (array1[i] < minimum)
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-18-2009, 09:02 PM #9
- 04-18-2009, 09:14 PM #10
OK, no problem... probably a miscommunication... I also hope th OP has found this thread helpful...
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-19-2009, 06:04 AM #11
- 04-19-2009, 07:00 AM #12
- 04-19-2009, 07:27 AM #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
orJava Code:for each(elementType elementName in collection) { //do this to each elementType }
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).Java Code:for(int number : intArray) { //do this to each word }
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:
This code would print out the lowest int in array1.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); }Last edited by AustinDoggie; 04-19-2009 at 08:22 AM.
- 04-19-2009, 08:22 AM #14
- 04-19-2009, 08:25 AM #15
edit: nevermind.
Last edited by AustinDoggie; 04-19-2009 at 06:58 PM.
- 04-19-2009, 05:42 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
- 04-19-2009, 06:06 PM #17
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
P.S. The for/in loop does work with arrays (primitive or otherwise)!
Neil Coffey
Javamex - Java tutorials and performance info
- 04-19-2009, 06:48 PM #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
- 04-21-2009, 09:14 AM #19
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Finding the Mode in An Array
By carlodelmundo in forum New To JavaReplies: 23Last Post: 10-31-2010, 12:44 PM -
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02-07-2009, 07:48 AM -
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07-31-2008, 01:51 PM -
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02-16-2008, 09:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks