/** 10/29/2018
  * Problem: Read in a set of grades
  * 1. calculate average
  * 2. Find max
  * 3. Find min
  * 4. Count number of grades greater than the average.
  * This version is Solution 3 using Arrays
  * We have the advantages of both Solutions 1 and 2:
  * scales up and is easier to program AND data sticks around.
  */
import java.io.*;
import java.util.Scanner;

public class GradesArray
{  
  public static void main(String[]  args) throws IOException {
    
    Scanner infile = new Scanner(new File("grades.txt"));
    // Read in data (which is a header value followed by #s)
    int num = infile.nextInt();
    // final int SIZE = 5;
    double[] grades = new double[num];
    
    // Read data values into the array grades
    for (int i=0; i<grades.length; i++)  {
      grades[i] = infile.nextDouble();
      System.out.print(grades[i] + " ");
    }
    System.out.println();
    
    // this won't work:  System.out.println(grades);
    // rule with arrays: always process them with loops
     
    // task 1: calculate average
    double sum=0.0;
    for (int i=0; i<grades.length; i++)
      sum+=grades[i];
    double average = sum/grades.length;
    System.out.println("average: " + average);
    
    // task 2&3: calculate max and min
    double max = grades[0];
    double min = grades[0];
    for (int i=1; i<grades.length; i++) {
       if (grades[i] > max)
          max = grades[i];
       else if (grades[i] < min)
          min = grades[i];
    }
    
    // task 4: how many grades are higher than average
    int higherAvg=0;
    for (int i=0; i<grades.length; i++) {
       if (grades[i] > average)
          higherAvg++;
    }
 
    System.out.println("max: " + max + " min: " + min
                        + " Higher than avg: " + higherAvg);
    
 }
} 