Results 1 to 14 of 14
 09092010, 06:54 AM #1Member
 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;
Java Code:array[0] = 5 array[1] = 1 array[2] = 9 array[3] = 6
 09092010, 07:06 AM #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!
 09092010, 07:30 AM #3Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 9
Arrays.sort() and take the first and last.
 09092010, 01:35 PM #4Member
 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.length1; index++) { if (array[i] > somevar) { somevar = array[i]; System.out.println("This is somevar high = " +somevar); } }
 09092010, 01:50 PM #5Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 9
You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.
 09092010, 01:57 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,879
 Blog Entries
 7
 Rep Power
 21
 09092010, 03:00 PM #7This returns an error
 09092010, 07:49 PM #8Member
 Join Date
 Aug 2010
 Posts
 28
 Rep Power
 0
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 = i1; if (array[i] > array[itest]) { largest = array[i]; } }
Cheers

 09092010, 08:00 PM #10
I have to agree with Fubarable here. That's not true...
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}
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 = i1; 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);
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!
 09092010, 10:42 PM #11Member
 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.
 09092010, 11:09 PM #12
The code line you posted does not compile.
What error message do you get when you try to compile it?
 09092010, 11:14 PM #13Member
 Join Date
 Aug 2010
 Posts
 28
 Rep Power
 0
Unresolved compilation problems:
Type mismatch: cannot convert from double to int
 09092010, 11:16 PM #14Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 9
Similar Threads

Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 07:49 AM 
Print the second Largest in an array
By singularity in forum New To JavaReplies: 23Last Post: 05052010, 11:33 AM 
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02072009, 08:48 AM 
second largest array
By bishnu in forum New To JavaReplies: 2Last Post: 01032009, 11:01 AM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM
Bookmarks