Results 1 to 14 of 14
  1. #1
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default 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;
    That would turn out, for example:

    Java Code:
    array[0] = 5
    array[1] = 1
    array[2] = 9
    array[3] = 6
    How can we make the computer find the lowest number, which is 1? What about the highest number, 9?

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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!

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Arrays.sort() and take the first and last.

  4. #4
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    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.length-1; index++) {
    	    if (array[i] > somevar) {
    	        somevar = array[i];
    	        System.out.println("This is somevar high = " +somevar);
    	    }
    	    
    	}
    This returns an error at if (array[i] > somevar) {

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    You've tried Arrays.sort()? And it didn't work? Sorry, but I don't believe that.
    The way the OP translated the other tip, I do believe it. ;-)

    kind regards,

    Jos

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    This returns an error
    Please copy and paste the full text of the error here.

  8. #8
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The way the OP translated the other tip, I do believe it. ;-)

    kind regards,

    Jos
    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 = i-1;
    						if (array[i] > array[itest]) {
    							largest = array[i];
    						}
    					}
    Thanks for the support
    Cheers

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bobocheez View Post
    I But it turns out that Double can't be ordered. It has to be an int.
    Since when has this been true? You may want to reconsider this.

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by bobocheez View Post
    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.
    I have to agree with Fubarable here. That's not true...

    Quote Originally Posted by bobocheez View Post
    What I ended up doing is comparing each value that was input to the previous value.

    Java Code:
    					if (i > 0) {
    						int itest = i-1;
    						if (array[i] > array[itest]) {
    							largest = array[i];
    						}
    					}
    Thanks for the support
    Cheers
    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}
    It will say that 5 is the largest in the array, because 5 is larger than 3.

    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 = 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);
    Output:
    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!

  11. #11
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    The code line you posted does not compile.
    What error message do you get when you try to compile it?

  13. #13
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Unresolved compilation problems:
    Type mismatch: cannot convert from double to int

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by bobocheez View Post
    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.
    First of all by using a double array rather than an int array. Secondly, even if you did use the int values of those doubles (i.e. drop everything after the decimal point) you could still compare them to doubles.

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Print the second Largest in an array
    By singularity in forum New To Java
    Replies: 23
    Last Post: 05-05-2010, 11:33 AM
  3. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  4. second largest array
    By bishnu in forum New To Java
    Replies: 2
    Last Post: 01-03-2009, 10:01 AM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM

Posting Permissions

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