title: How do you find the maximum and minimum for a random array?
body: <pre><code>   public class ArrayCalculations
     {
        public static void main(String[]  args)
        {
  
           int sum = 0, smallest, largest;
           double average;
           int[] array = new int[20];
  
           for(int i = 0; i &lt; array.length; i++)
           {
  
              array[i] = (int)(Math.random() * 500 + 1);
              System.out.println(array[i]);
              sum += array[i];
              smallest = array[0];
              largest = array[];
              if(array[i]&lt;smallest)
              {
                 smallest = array[i];
              }
              if(array[i]&gt;largest)
              {
              largest = array[i];
              }
  
           }
           average = 1.0*sum/array.length;
  
           System.out.println("Sum: " + sum);
           System.out.println("Average: " + average);
           System.out.println("Smallest: " + smallest);
           System.out.println("Largest: " + largest);
        }
     }
  </code></pre>
  
  <p>It keeps saying that smallest and largest may not have been initialized. I am not sure how to fix it. I tried setting smallest and largest to the first number outside of the for loop but that was before the all the numbers in the array were initialized so that didn't work...</p>