/** 
 * Using a 3D array, we find the average quiz score for all students
 * in a term, and then average across terms. We built on the
 * Per-Student Average code. We added terms as a third dimension to
 * the array. We refactored the code by renaming the variable "row" to
 * "student", and the variable "col" to "quiz" for clarity. Since we
 * added the terms as the first dimension of the 3D array, it would
 * have been confusing to have a second dimension named row and a
 * third dimension named col. We also refactored by renaming the old
 * variable "average" into "averageQuizPerStudent", since the new code
 * has many variables that average across different quantities.
 * 
 * Written together in class Thu Mar 9 2006
 */
public class AverageAcrossTerms
{
    public static void main(String[] args)
    {
        double[][][] scores = {
                {   {95, 82, 13, 96},
                    {51, 68, 63, 57}, 
                    {73, 71, 84, 78}, 
                    {50, 50, 50, 50},
                    {99, 70, 32, 12}
                },
                {   {75, 88, 25, 47},
                    {91, 38, 83, 56}, 
                    {73, 71, 94, 78}, 
                    {60, 40, 55, 52},
                    {100, 100, 100, 50}
                }
        };
        double averageQuizPerStudent;
        double averageStudentPerTerm;
        double averageStudentForAllTerms;
        
        averageStudentForAllTerms = 0;
	// loop through the terms
        for (int term=0; term < scores.length; term++)
        {
            averageStudentPerTerm = 0;
            // loop through the students
            for (int student = 0; student < scores[term].length; student++)
            {
                averageQuizPerStudent = 0;
                // loop through the quiz scores
                for (int quiz = 0; quiz < scores[term][student].length; quiz++)
                {
                    averageQuizPerStudent = averageQuizPerStudent + scores[term][student][quiz];
                }
                averageQuizPerStudent = averageQuizPerStudent / scores[term][student].length;
                averageStudentPerTerm = averageStudentPerTerm + averageQuizPerStudent;
                System.out.println("for term "+term+" average of student  " + student + " across multiple quizzes is " + averageQuizPerStudent);
            }
            averageStudentPerTerm = averageStudentPerTerm / scores[term].length;
            averageStudentForAllTerms = averageStudentForAllTerms + averageStudentPerTerm;
            System.out.println("average student per term "+averageStudentPerTerm);
        }
        averageStudentForAllTerms = averageStudentForAllTerms / scores.length;
        System.out.println("average student for all terms "+averageStudentForAllTerms);
    }
}

