Page 1 of 1

Reputation:

# Find Smallest number in a random array

Posted 18 December 2010 - 12:49 PM

Im trying to find the smallest sumber in an array of size 10, that fills 5 spots in the array with random numbers. Im not sure the best way to go about entering the code for the findSmallest method below, any ideas?

```// The "AssignmentTen" class.
import java.awt.*;
import hsa.Console;

public class AssignmentTen
{
static Console c;           // The output console

public static void main (String [] args)
{
c = new Console ();

int size = 10;
int numPositions = 5;
int value;

c.println("The random numbers in the list are:");

randomFill (size, numPositions);

} // main method

public static int findSmallest(int list[])
{

}

public static int [] randomFill (int size, int numPositions)
{

int upper = 21;
int lower = 10;
int random;

int [] list = new int [10];

for (int i = 0 ; i < numPositions ; i++)
{
double rawRandom = Math.random ();
random = (int) ((rawRandom * (upper - lower)) + lower);
list [i] = random;

c.println (list [i]);

}

return list;
}
} // AssignmentTen class

```

Is This A Good Question/Topic? 0

## Replies To: Find Smallest number in a random array

### #2 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: Find Smallest number in a random array

Posted 18 December 2010 - 01:10 PM

What you can do is:
1)create a variable that is going to hold the smallest integer in your array
2)make the above variable point to first element in the array
3)loop from 2nd to last element in the array
4)inside the loop make an if statement that is going to check whther the current element in the array is less then the variable from point 1
5)if its not ignore it
6) if it is smaller then make variiable from point 1 hold this smallest number so far
7) at the end print the variable

### #3 Scottm

Reputation: 42
• Posts: 238
• Joined: 11-February 10

## Re: Find Smallest number in a random array

Posted 18 December 2010 - 01:28 PM

darek9576, on 18 December 2010 - 12:10 PM, said:

What you can do is:
1)create a variable that is going to hold the smallest integer in your array
2)make the above variable point to first element in the array
3)loop from 2nd to last element in the array
4)inside the loop make an if statement that is going to check whther the current element in the array is less then the variable from point 1
5)if its not ignore it
6) if it is smaller then make variiable from point 1 hold this smallest number so far
7) at the end print the variable

Here's is what the code should look like::

```int lowestNumber = Array[0];

for(int i = 1; i < Array.length-1; i++){

if(Array[i] < lowestNumber){
lowestNumber = Array[i];
}
}
System.out.println(lowestNumber);
```

Now, fix that to make it work for your program

### #4 m-e-g-a-z

• Winning

Reputation: 497
• Posts: 1,457
• Joined: 19-October 09

## Re: Find Smallest number in a random array

Posted 18 December 2010 - 01:55 PM

I have a snippet on this - Min,max,average and sum of array.

Reputation:

## Re: Find Smallest number in a random array

Posted 22 December 2010 - 01:56 PM

just 1 small problem with the function im having now, im not sure how to enter the []list array into the findSmallest() section of the code . how would i enter it so that it will run properly?

```// The "AssignmentTen" class.
import java.awt.*;
import hsa.Console;

public class AssignmentTen
{
static Console c;           // The output console

public static void main (String [] args)
{
c = new Console ();

int size = 10;
int numPositions = 5;
int value;

c.println("The random numbers in the list are:");

randomFill (size, numPositions);

c.println("The smallest number in the list is:");

//Problem  here D:
findSmallest();

} // main method

public static int findSmallest(int list[])
{
int lowestNumber = list[0];

for(int i = 1; i < 5; i++)
{

if(list[i] < lowestNumber)
{

lowestNumber = list[i];
}

}
return (lowestNumber);

}

public static int [] randomFill (int size, int numPositions)
{

int upper = 21;
int lower = 10;
int random;

int [] list = new int [10];

for (int i = 0 ; i < numPositions ; i++)
{
double rawRandom = Math.random ();
random = (int) ((rawRandom * (upper - lower)) + lower);
list [i] = random;

c.println (list [i]);

}

return list;
}
} // AssignmentTen class

```

### #6 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: Find Smallest number in a random array

Posted 22 December 2010 - 02:14 PM

Hello there,

Wouldn't you just create the array that contains the numbers to check, then pass that to the method findSmallest()? Something like this maybe:

```int[] list = {1,2,3,4}; //create the array to check

int smallestNumber = findSmallest(list); //method returns the number, so store it in an int variable

```

This post has been edited by [email protected]: 22 December 2010 - 02:51 PM