Results 1 to 19 of 19
  1. #1
    MSteinman is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Cool [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. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by MSteinman View Post
    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 07:13 PM. Reason: small modification

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    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. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    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. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    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. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

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

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by MSteinman View Post
    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. #12
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by AustinDoggie View Post
    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. #13
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    4
    Rep Power
    0

    Default

    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 09:22 AM.

  14. #14
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by AustinDoggie View Post
    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. #15
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    4
    Rep Power
    0

    Default

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

  16. #16
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    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. #17
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    P.S. The for/in loop does work with arrays (primitive or otherwise)!

  18. #18
    MSteinman is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default solved

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

  19. #19
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    Arrays.sort(array1);
    System.out.println("Minimum value was:["+array1[0]+"]");

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  2. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 01:44 PM
  3. Replies: 5
    Last Post: 02-07-2009, 08:48 AM
  4. To find the Maximum and Minimum in an Array of Strings
    By luscious in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 07-31-2008, 02:51 PM
  5. Replies: 1
    Last Post: 02-16-2008, 10:10 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
  •