Results 1 to 14 of 14
- 09-09-2010, 06:54 AM #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.
That would turn out, for example:Java Code:Double array[] = new Double[10000]; array[i] = n;
How can we make the computer find the lowest number, which is 1? What about the highest number, 9?Java Code:array[0] = 5 array[1] = 1 array[2] = 9 array[3] = 6
- 09-09-2010, 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!
- 09-09-2010, 07:30 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Arrays.sort() and take the first and last.
- 09-09-2010, 01:35 PM #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.
This returns an error at if (array[i] > somevar) {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); } }
- 09-09-2010, 01:50 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.
- 09-09-2010, 01:57 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-09-2010, 03:00 PM #7
Please copy and paste the full text of the error here.This returns an error
- 09-09-2010, 07:49 PM #8
Member
- 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.
Thanks for the supportJava Code:if (i > 0) { int itest = i-1; if (array[i] > array[itest]) { largest = array[i]; } }
Cheers
-
- 09-09-2010, 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:
It will say that 5 is the largest in the array, because 5 is larger than 3.Java Code:{12, 15, 3, 5}
Example:
Output: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);
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!
- 09-09-2010, 10:42 PM #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.
- 09-09-2010, 11:09 PM #12
The code line you posted does not compile.
What error message do you get when you try to compile it?
- 09-09-2010, 11:14 PM #13
Member
- Join Date
- Aug 2010
- Posts
- 28
- Rep Power
- 0
Unresolved compilation problems:
Type mismatch: cannot convert from double to int
- 09-09-2010, 11:16 PM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Print the second Largest in an array
By singularity in forum New To JavaReplies: 23Last Post: 05-05-2010, 11:33 AM -
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 -
second largest array
By bishnu in forum New To JavaReplies: 2Last Post: 01-03-2009, 10:01 AM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks