#pragma once
#include <array>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class GradeBook
{
public:
static const size_t students{ 10 };//number of students
static const size_t tests{ 3 };//number of tests
//two argument contructor initializes courseName and grades array
GradeBook(const string& name, array<array<int, tests>, students>&
gradesArray)
: courseName(name), grades(gradesArray)
{
}
//function to set the courseName
void setCourseName(const string& name)
{
courseName = name;
}
//function to retrieve the course name
const string& getCourseName() const
{
return courseName;
}
//displaying a welcome message
void displayMessage() const
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!"
<< std::endl;//displaying a welcome message
}
//perform various operations on the data
void processGrade() const
{
outputGrades();//output grades array
cout << "\nLowest grade in the book is " << getMinimum()
<< "\nHighest grade in the grade book is " << getMaximum()
<< endl;
outputBarChart();//display grade distritution chart
}
//find minimum grade in the entire gradebook
int getMinimum() const
{
int lowGrade{ 100 };
//loop thru rows of grades array
for (auto const& student : grades)
{
//loop thru columns of grades array
for (auto const& grade : student)
{
if (grade < lowGrade)
{
lowGrade = grade;
}
}
}
return lowGrade;//return lowest grade
}
// find the maximum in the entire gradebook
int getMaximum() const
{
int highGrade{ 0 };
//loop thru rows of grades array
for (auto const& student : grades)
{
//loop thru column of grades array
for (auto const& grade : student)
{
if (grade > highGrade)
{
highGrade = grade;
}
}
}
return highGrade;//return the highest grades
}
//determine average grade for particular set of grades
double getAverage(const array<int, tests>& setOfGrades) const
{
int total{ 0 };
for (int grade : setOfGrades)
{
total = total + grade;
}
return static_cast<double>(total) / setOfGrades.size();
}
//output bar chart displaying grade distribution
void outputBarChart() const
{
int testNumber = 0;
cout << "\nOverall grade distribution:" << endl;
const size_t frequencySize{ 11 };
array<unsigned int, frequencySize> frequency{};
for (auto const& student : grades)
{
for (auto const& test : student)
{
testNumber = ++frequency[test / 10];
cout << endl << testNumber << endl;
}
}
for (size_t count{ 0 }; count < frequencySize; ++count)
{
if (0 == count)
{
cout << " 0 -9:";
}
else if (10 == count)
{
cout << " 10:";
}
else
{
cout << count * 10 << "-" << (count * 10) + 9 << ": ";
}
for (unsigned int stars{ 0 }; stars < frequency[count]; ++stars)
{
cout << '*';
}
cout << endl;
}
}
//for each grade frequency, print bar in chart
void outputGrades() const
{
cout << "\nThe grades are:\n\n";
cout << " ";
for (size_t test{ 0 }; test < tests; ++test)
{
cout << "Test " << test + 1 << " ";
}
cout << "Average" << endl;
for (size_t student{ 0 }; student < grades.size(); ++student)
{
cout << "Student " << setw(2) << student + 1;
for (size_t test{ 0 }; test < grades[student].size(); ++test)
{
cout << setw(8) << grades[student][test];
}
double average{ getAverage(grades[student]) };
cout << setw(9) << setprecision(2) << fixed << average << endl;
}
}
private:
string courseName;
array<array<int, tests>, students> grades;
};
Source file
//Creates GradeBook object using a two-dimensional array of grades
#include <array>
#include "GradeBook.h"//GradeBook class definition
using namespace std;
int main()
{
//two dimentional array of student grades
array<array<int, GradeBook::tests>, GradeBook::students> grades{
{87, 96, 70},
{68, 87, 90},
{94, 100, 90},
{100, 81, 82},
{83, 65, 85},
{78, 87, 65},
{85, 75, 83},
{91, 94, 100},
{76, 72, 84},
{87, 93, 73}};
GradeBook myGradeBook("CS101 Introduction to C++ Programming", grades);
myGradeBook.displayMessage();
myGradeBook.processGrade();
return 0;
}
This is an example from the book, but it's so weird that an example from book has error. So, I want to ask how to solve this problem.
Aucun commentaire:
Enregistrer un commentaire