Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: how to find the smallest value from a non-empty unsorted array of integers?

  1. #1
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default how to find the smallest value from a non-empty unsorted array of integers?

    Hi, Everyone. I am a newer in java. Now i have a practice question, i am not sure what the problem about my programming?

    The question is:

    n a file called MinimumElement.java write a public class of the same name containing a method with the signature:
    public int min(int [] a);

    that finds the minimum value of a non-empty unsorted array of integers. Use the fastest algorithm you can devise for doing this.
    As an example of how the code should work, the driver code:

    MinimumElement myMin = new MinimumElement();
    int [] myArray = {5,-11,2,3,14,5,-11,2};
    System.out.println(myMin.min(myArray));

    should print the number, -11.

    My coding is:
    public class MinimumElement {
    int myArray[] = {5,-11,2,3,14,5,-11,2};
    public static void main(String args[]){
    //int min = [0] mrArray;
    MinimumElement myMin = new MinimumElement();
    int min = myMin.myArray[0];
    for(int i =1;i<9;i++)
    {
    if(myMin.myArray[i]<=min)
    {
    min = myMin.myArray[i];
    }
    }
    System.out.println(min);
    }
    }
    but when i run the code, it show me :Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at MinimumElement.main(MinimumElement.java:10)

    Anyone can help me ?

    Thanks very much!!!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Please use [CODE][/CODE] Tags to format your code.
    Array indices start at 0, your for loop ends at index 8, which does not exist.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Thanks very much! Yes, i chang to 8, it works already. But according to the question, what the meaning of 'public int min(int [] a)'? My coding format is correct for this question?

    Thanks very much!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Actually, you might want to change it to myArray.length.

    The answer to your question is that it accepts an array of type int as an argument and returns a type int as the return value.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Thanks for your replaying. Could u help me to modify my code, really need your help.

    Thanks!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Just create a method with the signature "public int min(int[] myArray)." Then paste your min algorithm into the method and return the minimum value when you find it.

    To call the method, create an instance of your class in "main" and use it to call the method. You need to supply the array of integers as an argument.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    there is no need to use all that to get smallest number, a simple code would be:

    Java Code:
    int arr[] = {5,-11,2,3,14,5,-11,2};
    int lowestNum;
    
    for(int i=0; i<arr.length(); i++)
    {
    if(lowestNum<arr[i]) lowedtNum=arr[i];
    }
    System.out.println("The lowest number is:\t"+lowestNum);

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Does that code even compile? Did you try it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Except for the fact that you made a syntax error, your code is essentially the same as the OP's. You omitted the method, the main entry point and the enclosing class. According to the post the practice exercise is supposed to use a method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    Hi,Jim

    I followed your suggestion, but still have problem about the call method part.

    My code is below:
    public class MinimumElement {
    int[] myArray = {5,-11,2,3,14,5,-11,2};
    public static void main(String args[]){

    //min();
    }
    public int min(int[] myArray){
    MinimumElement myMin = new MinimumElement();
    int min = myMin.myArray[0];
    for(int i =1;i<8;i++)
    {
    if(myMin.myArray[i]<=min)
    {
    min = myMin.myArray[i];
    }
    }
    System.out.println(myMin.min(myArray));
    return min;
    }
    }

    How to call the method?

    Thanks very much!

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to find the smallest value from a non-empty unsorted array of integers?

    For future posts please enclose your code within code tags. [CODE][/CODE]. I have done that here for this discussion.


    Java Code:
    public class MinimumElement {
      int[] myArray = {5,-11,2,3,14,5,-11,2};
      public static void main(String args[]){
      //min();
      }
     
       public int min(int[] myArray){
           MinimumElement myMin = new MinimumElement();
           int min = myMin.myArray[0];
           for(int i =1;i<8;i++)
          {
              if(myMin.myArray[i]<=min)
             {
               min = myMin.myArray[i];
             }
         }
         System.out.println(myMin.min(myArray));
         return min;
        }
    }
    You're pretty close but you need to do a couple of things. Move your class instantiation to main. Next, the array parameter in min is local to min so you don't need an instance to access it. Move the print statement to main as well. Change 8 to use the array length field. That way you can pass an array of arbitrary length to the method. Finally, in the main entry point you will need to access myArray with an instance of the class (e.g. myMin.myArray).

    Regards,
    Jim
    hjxlpp likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Find the Biggest Number + Smallest
    By d4v1dz in forum New To Java
    Replies: 19
    Last Post: 10-26-2011, 07:43 AM
  2. find first empty cell?
    By truant420 in forum New To Java
    Replies: 5
    Last Post: 03-15-2011, 02:13 PM
  3. Finding the Smallest Element in an Array
    By Cod in forum New To Java
    Replies: 1
    Last Post: 02-20-2011, 03:05 AM
  4. How to find out if part of an array is empty
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 07:09 PM
  5. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-10-2010, 12:16 AM

Posting Permissions

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