GradeBook
Using a Two-Dimensional array
In Section 7.6, we presented class GradeBook
(Fig. 7.13), which used a one-dimensional array
to store student grades on a single exam. In most semesters, students take several exams. Professors are likely to want to analyze grades across the entire semester, both for a single student and for the class as a whole.
array
in Class GradeBook
Figure 7.18 shows the output that summarizes 10 students’ grades on three exams. We store the grades as a two-dimensional array
in an object of the next version of class GradeBook
(Fig. 7.19). Each row of the array
represents a single student’s grades for the entire course, and each column represents all the grades the students earned for one particular exam. A client program, such as Fig. 7.20, passes the array
as an argument to the GradeBook
constructor. Since there are 10 students and three exams, we use a ten-by-three array
to store the grades.
GradeBook
’s FunctionsEach of class GradeBook
’s member functions is similar to its counterpart in the earlier one-dimensional array
version of class GradeBook
(Fig. 7.13). Member function getMinimum
(lines 52–66 of Fig. 7.19) determines the lowest grade of all students for the semester. Member function getMaximum
(lines 69–83) determines the highest grade of all students for the semester. Member function getAverage
(lines 86–96) determines a particular student’s semester average. Member function outputBarChart
(lines 99–132) outputs a bar chart of the distribution of all student grades for the semester. Member function output-Grades
(lines 135–161) outputs the two-dimensional array
in a tabular format, along with each student’s semester average.
getMinimum
and getMaximum
Member functions getMinimum
, getMaximum
, outputBarChart
and outputGrades
each loop through array
grades
by using nested range-based for
or counter-controlled for
statements. For example, consider the nested for
statement (lines 56–63) in member function getMinimum
. The outer for
statement loops through the rows that represent each student
and the inner for
loops through the grades of a given student. Each grade is compared with variable lowGrade
in the body of the inner for
statement. If a grade is less than lowGrade
, lowGrade
is set to that grade. This repeats until all rows and columns of grades
have been traversed. When execution of the nested statement is complete, lowGrade
contains the smallest grade in the two-dimensional array
. Member function getMaximum
works similarly to member function getMinimum
.
outputBarChart
Member function outputBarChart
in Fig. 7.19 is nearly identical to the one in Fig. 7.13. However, to output the overall grade distribution for a whole semester, the function uses a nested for
statement (lines 107–111 in Fig. 7.19) to increment the elements of the one-dimensional array
frequency
, based on all the grades in the two-dimensional array
. The rest of the code in the two outputBarChart
member functions is identical.
outputGrades
Member function outputGrades
(lines 135–161) uses nested counter-controlled for
statements to output values of the array
grades
, in addition to each student’s semester average. The output in Fig. 7.18 shows the result, which resembles the tabular format of a professor’s physical grade book. Lines 140–142 print the column headings for each test. We use a counter-controlled for
statement so that we can identify each test with a number. Similarly, the for
statement in lines 147–160 first outputs a row label using a counter variable to identify each student (line 148). Although array
indices start at 0, lines 141 and 148 output test + 1
and student + 1
, respectively, to produce test and student numbers starting at 1 (see Fig. 7.18). The inner for
statement in lines 151–153 of Fig. 7.19 uses the outer for
statement’s counter variable student
to loop through a specific row of array
grades
and output each student’s test grade. Finally, line 157 obtains each student’s semester average by passing the current row of grades
(i.e., grades[student]
) to member function getAverage
.
getAverage
Member function getAverage
(lines 86–96) takes as an argument a one-dimensional array
of test results for a particular student. When line 157 calls getAverage
, the first argument is grades[student]
, which specifies that a particular row of the two-dimensional array
grades
should be passed to getAverage
. For example, based on the array
created in Fig. 7.20, the argument grades[1]
represents the three values (a one-dimensional array
of grades) stored in row 1
of the two-dimensional array
grades
. A two-dimensional array
’s rows are one-dimensional array
s. Member function getAverage
calculates the sum of the array
elements, divides the total by the number of test results and returns the floating-point result as a double
value (Fig. 7.19, line 95).
GradeBook
The program in Fig. 7.20 creates an object of class GradeBook
(Fig. 7.19) using the two-dimensional array
of int
s named grades
(declared and initialized in lines 9–19 of Fig. 7.20). Line 9 accesses class GradeBook
’s static
constants students
and tests
to indicate the size of each dimension of array
grades
. Line 21 passes a course name and grades
to the GradeBook
constructor. Lines 22–23 then invoke myGradeBook
’s displayMessage
and processGrades
member functions to display a welcome message and obtain a report summarizing the students’ grades for the semester, respectively.