/**
 * GradeAnalyzer.java
 *
 * Computer Science S-111
 * 
 * Demonstrates the use of an array to store and process a collection of data. 
 * -- specifically a collection of grades.
 */

import java.util.*;

public class GradeAnalyzer {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        System.out.print("How many grades? ");
        int maxNumGrades = console.nextInt();
        int[] grades = new int[maxNumGrades];

        int total = 0;
        int numGrades = 0;

        while (numGrades < maxNumGrades) {
            System.out.print("Enter a grade (or -1 to quit): ");
            grades[numGrades] = console.nextInt();
            if (grades[numGrades] == -1) {
                break;
            }
            total += grades[numGrades];        
            numGrades++;
        }
        
        if (numGrades > 0) {
            double average = (double)total / numGrades;
            System.out.println("average grade = " + average);
        }
        
        int max = maxGrade(grades);
        System.out.println("max grade = " + max);
        
        int[] counts = getCounts(grades, max);
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] > 0) {
                System.out.println(counts[i] + " grade(s) of " + i);
            }
        }
    }
    
    /*
     * maxGrade - returns the maximum grade in the array that is passed
     * in as a parameter
     */
    public static int maxGrade(int[] grades) {
        int max = grades[0];
        for (int i = 1; i < grades.length; i++) {
            if (grades[i] > max)
                max = grades[i];
        }

        return max;
    }
    
    /*
     * getCounts - creates and returns an array containing counts of the number
     * of times that each grade appears in the grades array.  The second
     * parameter specifies the maximum possible grade, and the array
     * that is constructed has a length of maxGrade + 1.
     * 
     * Element i in the returned array stores the number of times
     * that grade i appears in the grades array.
     */
    public static int[] getCounts(int[] grades, int maxGrade) {
        int[] counts = new int[maxGrade + 1];
        
        for (int i = 0; i < grades.length; i++) {
            counts[grades[i]]++;
        }

        return counts;
    }
}