Storing Student Grades in a Two-Dimensional array in Class GradeBook

Figure 7.21 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 Figures 7.227.23. 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.24, 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.

Welcome to the grade book for
CS101 Introduction to C++ Programming!

The grades are:

            Test 1  Test 2  Test 3  Average
Student  1      87      96      70    84.33
Student  2      68      87      90    81.67
Student  3      94     100      90    94.67
Student  4     100      81      82    87.67
Student  5      83      65      85    77.67
Student  6      78      87      65    76.67
Student  7      85      75      83    81.00
Student  8      91      94     100    95.00
Student  9      76      72      84    77.33
Student 10      87      93      73    84.33

Lowest grade in the grade book is 65
Highest grade in the grade book is 100

Overall grade distribution:
  0-9:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: ***
70-79: ******
80-89: ***********
90-99: *******
  100: ***

Fig. 7.21. Output of GradeBook that uses two-dimensional arrays.


 1   // Fig. 7.22: GradeBook.h
 2   // Definition of class GradeBook that uses a
 3   // two-dimensional array to store test grades.
 4   // Member functions are defined in GradeBook.cpp
 5   #include <array>
 6   #include <string>
 7
 8   // GradeBook class definition
 9   class GradeBook
10   {
11   public:
12      // constants
13      static const size_t students = 10; // number of students
14      static const size_t tests = 3; // number of tests       
15
16      // constructor initializes course name and array of grades
17      GradeBook( const std::string &,
18         std::array< std::array< int, tests >, students > & );
19
20      void setCourseName( const std::string & ); // set the course name
21      std::string getCourseName() const; // retrieve the course name
22      void displayMessage() const; // display a welcome message
23      void processGrades() const; // perform operations on the grade data
24      int getMinimum() const; // find the minimum grade in the grade book
25      int getMaximum() const; // find the maximum grade in the grade book
26      double getAverage( const std::array< int, tests > & ) const;
27      void outputBarChart() const; // output bar chart of grade distribution
28      void outputGrades() const; // output the contents of the grades array
29   private:
30      std::string courseName; // course name for this grade book
31      std::array< std::array< int, tests >, students > grades; // 2D array
32   }; // end class GradeBook


Fig. 7.22. Definition of class GradeBook that uses a two-dimensional array to store test grades.


 1   // Fig. 7.23: GradeBook.cpp
 2   // Member-function definitions for class GradeBook that
 3   // uses a two-dimensional array to store grades.
 4   #include <iostream>
 5   #include <iomanip> // parameterized stream manipulators
 6   using namespace std;
 7
 8   // include definition of class GradeBook from GradeBook.h
 9   #include "GradeBook.h" // GradeBook class definition
10
11   // two-argument constructor initializes courseName and grades array
12   GradeBook::GradeBook( const string &name,
13      std::array< std::array< int, tests >, students > &gradesArray )
14      : courseName( name ), grades( gradesArray )
15   {
16   } // end two-argument GradeBook constructor
17
18   // function to set the course name
19   void GradeBook::setCourseName( const string &name )
20   {
21      courseName = name; // store the course name
22   } // end function setCourseName
23
24   // function to retrieve the course name
25   string GradeBook::getCourseName() const
26   {
27      return courseName;
28   } // end function getCourseName
29
30   // display a welcome message to the GradeBook user
31   void GradeBook::displayMessage() const
32   {
33      // this statement calls getCourseName to get the
34      // name of the course this GradeBook represents
35      cout << "Welcome to the grade book for " << getCourseName() << "!"
36         << endl;
37   } // end function displayMessage
38
39   // perform various operations on the data
40   void GradeBook::processGrades() const
41   {
42      // output grades array
43      outputGrades();
44
45      // call functions getMinimum and getMaximum
46      cout << " Lowest grade in the grade book is " << getMinimum()
47         << " Highest grade in the grade book is " << getMaximum() << endl;
48
49      // output grade distribution chart of all grades on all tests
50      outputBarChart();
51   } // end function processGrades
52
53   // find minimum grade in the entire gradebook
54   int GradeBook::getMinimum() const
55   {
56      int lowGrade = 100; // assume lowest grade is 100
57
58      // loop through rows of grades array
59      for ( auto const &student : grades )
60      {
61         // loop through columns of current row
62         for ( auto const &grade : student )
63         {
64            // if current grade less than lowGrade, assign it to lowGrade
65            if ( grade < lowGrade )
66               lowGrade = grade; // new lowest grade
67         } // end inner for
68      } // end outer for
69
70      return lowGrade; // return lowest grade
71   } // end function getMinimum
72
73   // find maximum grade in the entire gradebook
74   int GradeBook::getMaximum() const
75   {
76      int highGrade = 0; // assume highest grade is 0
77
78      // loop through rows of grades array
79      for ( auto const &student : grades )
80      {
81         // loop through columns of current row
82         for ( auto const &grade : student )
83         {
84            // if current grade greater than highGrade, assign to highGrade
85            if ( grade > highGrade )
86               highGrade = grade; // new highest grade
87         } // end inner for
88      } // end outer for
89
90      return highGrade; // return highest grade
91   } // end function getMaximum
92
93   // determine average grade for particular set of grades
94   double GradeBook::getAverage( const array<int, tests> &setOfGrades ) const
95   {
96      int total = 0; // initialize total
97
98      // sum grades in array
99      for ( int grade : setOfGrades )
100        total += grade;
101
102     // return average of grades
103     return static_cast< double >( total ) / setOfGrades.size();
104  } // end function getAverage
105
106  // output bar chart displaying grade distribution
107  void GradeBook::outputBarChart() const
108  {
109     cout << " Overall grade distribution:" << endl;
110
111     // stores frequency of grades in each range of 10 grades
112     const size_t frequencySize = 11;
113     array< unsigned int, frequencySize > frequency = {}; // init to 0s
114
115     // for each grade, increment the appropriate frequency
116     for ( auto const &student : grades )
117        for ( auto const &test : student )
118           ++frequency[ test / 10 ];
119
120     // for each grade frequency, print bar in chart
121     for ( size_t count = 0; count < frequencySize; ++count )
122     {
123        // output bar label ("0-9:", ..., "90-99:", "100:" )
124        if ( 0 == count )
125           cout << "  0-9: ";
126        else if ( 10 == count )
127           cout << "  100: ";
128        else
129           cout << count * 10 << "-" << ( count * 10 ) + 9 << ": ";
130
131        // print bar of asterisks
132        for ( unsigned int stars = 0; stars < frequency[ count ]; ++stars )
133           cout << '*';
134
135        cout << endl; // start a new line of output
136     } // end outer for
137  } // end function outputBarChart
138
139  // output the contents of the grades array
140  void GradeBook::outputGrades() const
141  {
142     cout << " The grades are: ";
143     cout << "            "; // align column heads
144
145     // create a column heading for each of the tests
146     for ( size_t test = 0; test < tests; ++test )
147        cout << "Test " << test + 1 << "  ";
148
149     cout << "Average" << endl; // student average column heading
150
151     // create rows/columns of text representing array grades
152     for ( size_t student = 0; student < grades.size(); ++student )
153     {
154        cout << "Student " << setw( 2 ) << student + 1;
155
156        // output student's grades
157        for ( size_t test = 0; test < grades[ student ].size(); ++test )
158           cout << setw( 8 ) << grades[ student ][ test ];
159
160        // call member function getAverage to calculate student's average;
161        // pass row of grades as the argument
162        double average = getAverage( grades[ student ] );
163        cout << setw( 9 ) << setprecision( 2 ) << fixed << average << endl;
164     } // end outer for
165  } // end function outputGrades


Fig. 7.23. Member-function definitions for class GradeBook that uses a two-dimensional array to store grades.


 1   // Fig. 7.24: fig07_24.cpp
 2   // Creates GradeBook object using a two-dimensional array of grades.
 3   #include <array>
 4   #include "GradeBook.h" // GradeBook class definition
 5   using namespace std;
 6
 7   // function main begins program execution
 8   int main()
 9   {
10      // two-dimensional array of student grades
11      array< array< int, GradeBook::tests >, GradeBook::students > grades =
12         { 87, 96, 70,
13           68, 87, 90,
14           94, 100, 90,
15           100, 81, 82,
16           83, 65, 85,
17           78, 87, 65,
18           85, 75, 83,
19           91, 94, 100,
20           76, 72, 84,
21           87, 93, 73 };
22
23      GradeBook myGradeBook(
24         "CS101 Introduction to C++ Programming", grades );
25      myGradeBook.displayMessage();
26      myGradeBook.processGrades();
27   } // end main


Fig. 7.24. Creates a GradeBook object using a two-dimensional array of grades, then invokes member function processGrades to analyze them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.218.84