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

1. Member
Join Date
May 2013
Posts
17
Rep Power
0

## 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. ## Re: how to find the smallest value from a non-empty unsorted array of integers?

Array indices start at 0, your for loop ends at index 8, which does not exist.

3. Member
Join Date
May 2013
Posts
17
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,491
Rep Power
9

## 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

5. Member
Join Date
May 2013
Posts
17
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,491
Rep Power
9

## 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

7. Member
Join Date
Apr 2013
Posts
22
Rep Power
0

## 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. ## 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

9. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,491
Rep Power
9

## 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

10. Member
Join Date
May 2013
Posts
17
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,491
Rep Power
9

## 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

#### Posting Permissions

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