COSC 181 Foundations of Computer Programming Class 35 1 Final Exam Section 0001 Wednesday Dec 10 11 1 30 th Section 0002 Thursday Dec 11 8 10 30 th 2 7 10 Case Study Class GradeBook Using a Two Dimensional Array Class GradeBook One dimensional array Store student grades on a single exam Two dimensional array Store multiple grades for a single student and multiple students for the class as a whole Each row represents a student s grades Each column represents all the grades the students earned for one particular exam 3 1 Fig 7 23 GradeBook h 2 Def in it io n of c lass GradeBook that uses a 3 two dimens iona l array to store tes t grades 4 Member funct ions are def ined in GradeBook cpp 5 inc lude string program uses C Standard Library string class 6 us ing std string fig07 2 7 8 GradeBook class definition 9 c las s GradeBook Outline 3 cpp 10 11 public 12 constants 13 const static int students 10 number of students 14 const static int tests 3 number of tests 1 15 16 constructor initializes course name and array of grades 17 GradeBook string const int tests GradeBook constructor accepts a string and a two dimensional array 4 of 2 18 19 void setCourseName string function to set the course name 20 string getCourseName function to retr ieve the course name 21 void displayMessage display a welcome message 22 void processGrades perform various operations on the grade data 23 int getMinimum find the minimum grade in the grade book 24 int getMax imum find the maximum grade in the grade book 25 double getAverage const int const int find average of grades 26 void outputBarChart output bar chart of grade distribution 27 void outputGrades output the contents of the grades array 28 private 29 string courseName course name for this grade book 30 int grades students tests two dimensional array of grades Outline fig07 2 3 cpp 2 31 end class GradeBook Declare two dimensional array grades 5 of 2 1 2 3 4 Fig 7 24 GradeBook cpp Member funct ion def in i t i o ns for c lass GradeBook that uses a two dimens iona l array to store grades inc lude iostream Outline 5 6 7 8 9 10 11 12 13 14 15 16 us ing std cout us ing std cin us ing std endl us ing std fixed fig07 2 17 18 19 20 21 22 23 24 25 26 27 28 two argument constructor initializes courseName and grades array GradeBook GradeBook string name const intgradesArray tests Use setCourseName name initialize courseName 4 cpp inc lude iomanip parameterized stream manipulators us ing std setprecision sets numeric output precision us ing std setw sets field width 1 include definition of class GradeBook from GradeBook h inc lude GradeBook h of 7 nested for loops to copy elements from gradesArray to grades copy grades from gradeArray to grades for int student 0 student students student for int test 0 test tests test grades student test gradesArray student test end two argument GradeBook constructor 6 29 funct ion to set the course name 30 vo id GradeBook setCourseName string name 31 32 courseName name store the course name 33 end function setCourseName 34 35 funct ion to ret r ie ve the course name 36 string GradeBook getCourseName 37 38 returncourseName 39 end funct ion getCourseName 40 41 disp lay a welcome message to the GradeBook user 42 vo id GradeBook displayMessage 43 44 45 46 th is statement ca l ls Outline fig07 2 4 cpp 2 getCourseName to get the name of the course th is GradeBook represents cout Welcome to the grade book for n getCourseName 47 endl 48 end funct ion disp layMessage 49 50 perfo rm var ious operat ions on the data 51 vo id GradeBook processGrades 52 53 54 55 outputGrades 56 57 ca l l funct ions getMin imum and getMax imum cout nLowest grade in the grade book is getMinimum 58 output grades array nHighest grade in the grade book is getMax imum endl 7 of 7 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 Outline output grade distribution chart of all grades on all tests outputBarChart end function processGrades find minimum grade int GradeBook getMinimum int lowGrade 100 assume lowest grade is 100 fig07 2 4 cpp loop through rows of grades array for int student 0 student students student loop through columns of current row for int test 0 test tests test if current grade less than lowGrade assign it to lowGrade if grades student test lowGrade lowGrade grades student test new lowest grade end inner for end outer for return lowGrade return lowest grade end function getMinimum 3 of 7 Loop through rows and columns of grades to find the lowest grade of any student 8 84 f ind max imum grade Outline 85 int GradeBook getMax imum 86 87 int highGrade 0 assume highest grade is 0 88 89 loop through rows of grades array 90 for int student 0 student students student 91 92 loop through columns of current row 93 for int test 0 test tests test 94 fig07 2 4 cpp 95 if current grade greater than lowGrade assign it to highGrade 96 if grades student test highGrade 97 98 99 end inner for end outer for return highGrade return highest grade Loop through rows and columns of grades to find the highest grade of any student 102 end function getMaximum 103 104 determine average grade for particular set of grades 105double GradeBook getAverage const int setOfGrades const int grades 106 107 int total 0 initialize total 108 109 sum grades in array 110 for int grade 0 grade grades grade 111 of 7 highGrade grades student test new highest grade 100 101 4 total setOfGrades grade 112 9 113 114 return average of grades return static cast double total grades 115 end funct ion getAverage 116 117 output bar chart disp lay ing grade dis t r ibu t io n 118void GradeBook outputBarChart 119 120 121 cout nOvera l l 122 grade dis t r ibu t io n endl stores frequency of grades in each range of 10 grades 123 const int frequencySize 11 124 125 int frequency frequencySize 0 126 for each grade increment the appropriate frequency 127 for int student 0 student students student 128 129 for each grade frequency print bar in chart 133 for int count 0 count frequencySize count 134 output bar label 0 9 if count 0 cout 5 of 7 Calculate the distribution of all student grades 90 99 100 0 9 138 else if count 10 139 140 cout else 141 4 cpp frequency grades student test 10 131 132 137 fig07 2 for int test 0 test tests test 130 135 136 Outline 100 cout count 10 count 10 9 142 10 143 print bar of asterisks 144 for int stars 0 stars frequency count stars cout 145 146 cout endl start a …
View Full Document