4 Replies - 6707 Views - Last Post: 27 January 2011 - 06:46 AM Rate Topic: -----

#1 Guest_Bill*


Reputation:

Find smallest number in java array

Posted 26 January 2011 - 04:39 PM

Having a small, yet annoying problem with this code below :/ . The code is suposed to create an array with random numbers then find the smallest number in the array. only problem is that it wont recognise the (list) in the main method from the random fill method. ( i marked where the error is).


// 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:");
        
        findSmallest(list);    //ERROR HERE.
        
        
        




    } // main method
    


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

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


        int [] list = new int [size];

        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;
    }
    
    
  public static int findSmallest(int list[])
  {
  
        int lowestNumber = list[0];  

    

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

   

         if(list[i] < lowestNumber)
         {  

         lowestNumber = list[i];  
    
         }  

         }  
   return(lowestNumber); 

  }
  
        

    
    
} // AssignmentTen class






Is This A Good Question/Topic? 0

Replies To: Find smallest number in java array

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Find smallest number in java array

Posted 26 January 2011 - 04:56 PM

you might try this:


int[] aList = randomFill(size,numPositions);

int smallestNumber = findSmallest(aList);

System.out.println(smallestNumber);



Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Find smallest number in java array

Posted 26 January 2011 - 05:13 PM

View Postdarek9576, on 26 January 2011 - 04:56 PM, said:

you might try this:


int[] aList = randomFill(size,numPositions);

int smallestNumber = findSmallest(aList);

System.out.println(smallestNumber);




That got the program to run, although its now returning the lowest number as 0 for some reason >_<
Was This Post Helpful? 0

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Find smallest number in java array

Posted 26 January 2011 - 05:30 PM

Because size of your array is 10 and number of positions you fill in is 10 meaning the rest is filled in with zeros. Inside main() do:


int size = 10;
int numPositions = 10;




Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Find smallest number in java array

Posted 27 January 2011 - 06:46 AM

why do you need two variables, numPositions and size? as it seems they serve for the same purpose. you can use only size. and therefore, pass only one parameter (size) to randomFill.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1