-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeBook.cpp
More file actions
270 lines (216 loc) · 6.9 KB
/
GradeBook.cpp
File metadata and controls
270 lines (216 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Implementation of member function of the GradeBook class
#include <iostream>
#include <iomanip> // parametrized io manipulator
#include "GradeBook.h"
using namespace std;
// constructor of the class
GradeBook::GradeBook(string nameOfCourse, string nameOfInstructor, const int gradesArray[][tests]){
setCourseName(nameOfCourse);
setInstructorName(nameOfInstructor);
// // Initialize grade counts to 0
// aCount = 0;
// bCount = 0;
// cCount = 0;
// dCount = 0;
// fCount = 0;
// initialize maximum grade to 0
maximumGrade = 0;
for (int grade = 0; grade < students; grade++){
for (int test = 0; test < tests; test++){
grades[grade][test] = gradesArray[grade][test];
}
}
}
// function to set courseName
void GradeBook::setCourseName(string name){
if (name.length() < 25){
courseName = name;
} else {
courseName = name.substr(0, 25);
cerr << "Name " << name << " exceeds maximum length (25).\n"
<< "Limiting length to 25 characters." << endl;
}
}
// function to get courseName
string GradeBook::getCourseName(){
return courseName;
}
// function to display welcome message to the user
void GradeBook::displayMessage(){
cout << "Welcome to the grade book for " << getCourseName() << "!" << endl;
cout << "This course is presented by: " << getInstructorName() << endl;
}
// function to set the instructorName
void GradeBook::setInstructorName(string name){
instructorName = name;
}
// function to get the instructorName
string GradeBook::getInstructorName(){
return instructorName;
}
// determine class average of 10 grades entered by the user
void GradeBook::determineClassAverage(){
int total; // sum of grades entered by user
int gradeCounter; // number of grades entered by user
int grade; // grade value entered by user
double average; // average of grades
// initialize variables
total = 0;
gradeCounter = 0;
cout << "Enter grade or -1 to quit: ";
cin >> grade;
while (grade != -1){
total += grade;
gradeCounter++;
cout << "Enter grade or -1 to quit: ";
cin >> grade;
}
if (gradeCounter != 0){
average = static_cast<double>(total) / gradeCounter; // integer division for now
cout << "\nTotal of all " << gradeCounter << " grades is " << total << endl;
cout << "Class average is " << setprecision(2) << fixed << average << endl;
}else {
cout << "No grades were entered" << endl;
}
}
// // Input arbitrary number of grades from the user: upgrade grade counters
// void GradeBook::inputGrades(){
// int grade; // grade entered by the user
// cout << "Enter the letter grades." << endl
// << "Enter the EOF character to end input." << endl;
// while ((grade = cin.get()) != EOF ){
// switch (grade){
// case 'A':
// case 'a':
// ++aCount;
// break;
// case 'B':
// case 'b':
// ++bCount;
// break;
// case 'C':
// case 'c':
// ++cCount;
// break;
// case 'D':
// case 'd':
// ++dCount;
// break;
// case 'F':
// case 'f':
// ++fCount;
// break;
// case '\n':
// case '\t':
// case ' ':
// break; // ignore whitespaces
// default:
// cout << "Incorrect letter grade entered."
// << " Enter a new grade. " << endl;
// break;
// }
// }
// }
// void GradeBook::displayGradeReport(){
// cout << "\n\nNumber of students who received each letter grade:"
// << "\nA: " << aCount
// << "\nB: " << bCount
// << "\nC: " << cCount
// << "\nD: " << dCount
// << "\nF: " << fCount
// << endl;
// }
void GradeBook::inputGrades(){
// grades entered by the user
int grade1, grade2, grade3;
cout << "Enter three integer grades: ";
cin >> grade1 >> grade2 >> grade3;
maximumGrade = maximum(grade1, grade2, grade3);
}
void GradeBook::displayGradeReport(){
cout << "Maximum of grades entered: " << maximumGrade << endl;
}
int GradeBook::maximum(int x, int y, int z){
int maximumvalue = x;
if (y > maximumvalue){
maximumvalue = y;
}
if (z > maximumvalue){
maximumvalue = z;
}
return maximumvalue;
}
void GradeBook::processGrades(){
outputGrades();
cout << "\nLowest grade is " << getMinimum() <<
"\nHighest grade is " << getMaximum() << endl;
outputBarChart();
}
int GradeBook::getMinimum(){
int lowGrade = 100;
for (int grade = 0; grade < students; ++grade){
for (int test = 0; test < tests; test++){
if (grades[grade][test] < lowGrade){
lowGrade = grades[grade][test];
}
}
}
return lowGrade;
}
int GradeBook::getMaximum(){
int highGrade = 0;
for (int grade = 0; grade < students; ++grade){
for (int test = 0; test < tests; test++){
if (grades[grade][test] > highGrade){
highGrade = grades[grade][test];
}
}
}
return highGrade;
}
double GradeBook::getAverage(const int setOfGrades[], const int grades){
int total = 0;
for (int grade = 0; grade < grades; ++grade){
total += setOfGrades[grade];
}
return static_cast<double>(total) / grades;
}
void GradeBook::outputBarChart(){
cout << "\nGrade distribution: " << endl;
// store frequency of grades in ranges of 10
const int frequencySize = 11;
int frequency[frequencySize] = {}; // initialize to 0
for (int student =0; student < students; ++student){
for (int test = 0; test < tests; test++){
++frequency[grades[student][test]/10];
}
}
for (int count = 0; count < frequencySize; ++count){
if (count == 0)
cout << " 0-9: ";
else if (count == 10)
cout << " 100: ";
else
cout << count * 10 << "-" << (count * 10) + 9 << ": ";
for (int stars = 0; stars < frequency[count]; ++stars){
cout << "*";
}
cout << endl;
}
}
void GradeBook::outputGrades(){
cout << "\nThe grades are:\n\n";
cout << " ";
for (int test = 0; test < tests; test++){
cout << "Test " << test + 1 << " ";
}
cout << "Average" << endl;
for (int student = 0; student < students; student++){
cout << "Student " << setw(2) << student + 1;
for ( int test = 0; test < tests; ++test){
cout << setw(8) << grades[student][test];
}
double average = getAverage(grades[student], tests);
cout << setw(9) << setprecision(2) << fixed << average << endl;
}
}