5 Replies - 3976 Views - Last Post: 22 December 2010 - 02:14 PM Rate Topic: -----

#1 Guest_Brendon*


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  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 Scottm  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Find Smallest number in a random array

Posted 18 December 2010 - 01:28 PM

View Postdarek9576, 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
Was This Post Helpful? 1
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_brendon*


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



Was This Post Helpful? 0

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • 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 CodingSup3rnatur@l-360: 22 December 2010 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1