// 11/4/2019
// introduce array syntax
// solve grades problem with arrays
// (return to the employee pgm and put in arrays)

import java.util.Scanner;

public class GradesArray {
  /** read in a list of grades, and print the maximum*/
  
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    final int SIZE = 5;
    int[] grades = new int[SIZE]; // declares an array with SIZE ints
    // all values are zero
    // how do I access an element in the array?
    // use square brackets (2 meanings to square brackets)
    System.out.println("0th loc is: " + grades[0]);
    grades[0]=88;
    System.out.println("0th loc is: " + grades[0]);
    // reading a value into a loc in an array
    System.out.println("Enter a grade for loc 1: " );
    grades[1] = sc.nextInt();
    // Since grades is an object, we can use the DOT operator
    // and retrieve its length, which is its capacity
    //for (int i=0; i<grades.length; i++) {
      //System.out.println(i + "th loc is: " + grades[i]);
    // reads in values to every location in the array
    for (int i=0; i<grades.length; i++) {
      System.out.println("Enter a grade for loc " + i + ": ");
      grades[i] = sc.nextInt();
    }
    // print all values in the array
    for (int i=0; i<SIZE; i++) {
      System.out.println(i + "th loc is: " + grades[i]);
    }
    // find max elt in array grades
    // outside of loop, initialize max to first element
    int max = grades[0];
    // begin loop at element 1
    for (int i=1; i<grades.length; i++) {
      if (grades[i] > max) // compare each element to max
        max = grades[i];
    }
    // fall out of loop, print max
    System.out.println("max in array is: " + max);
    // outside of loop, initialize sum to zero
    int sum = 0;
    // begin loop at element 0
    for (int i=0; i<grades.length; i++) {
      sum+=grades[i];
    }
    // fall out of loop, divide by aray's size
    double average = (double)sum/grades.length;
    System.out.println("average is: " + average);
    // How many grades are bigger than the average?
    int count = 0; // initialize a count to zero
    // Array index out of bounds causes an exception
    // which is a runtime error
    for (int i=0; i<=grades.length; i++) {
      // check each elt, if it is larger than average, count it.
      if (grades[i]>average)
        count++;
    }
    System.out.println(count + " elts are bigger than average.");
    
    // Enhanced for loop (no need for index i, just
    // process each elt in the array
    // general form: for (datatype elt : arrayname)
    for (int grade : grades) { // for every grade in grades 
       System.out.println("in enhanced for loop, grade "
                            + "= " + grade);
    }
    // array intialization
    int[] numbers = {55, 66, 77, 88, 99};
    System.out.println(numbers[0]);
  }
}