I wanted to
- Create the class Student. Each student has a name and a grade. Include appropriate member functions to manipulate student objects.
- Replace the grades data member with students data member, which is an array of students of Student class. That is, you need to change 1) the type of the array from integer to student, and 2) the name of the array from grades to students.
- Make GradeBook class and all its member functions work for the new type of array.**
Gradebook.cpp
#include<iostream>
#include<string>
#include"Gradebook.h"
#include<array>
using namespace std;
int main()
{
string courseName;
cout <<"Enter course name:";
cin >> courseName;
cout << "===============================Entering student information===============================" << endl;
cout <<"Enter the name and grade for 10 students"<<endl;
array<int, 10> studentGrades{ 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
GradeBook myGradeBook(courseName,studentGrades);
myGradeBook.setCourseName(courseName);
myGradeBook.processGrades();
}
Gradebook.h
#pragma once
#include<string>
#include<array>
class GradeBook {
public:
GradeBook(std::string& cName,std::array<int,10>& sGrades) :
courseName{ cName }, studentGrades{ sGrades } {
}
std::string getCourseName() const {
return courseName;
}
void setCourseName(const std::string& cName) {
courseName = cName;
}
void processGrades() const {
outputGrades();
std::cout << "\nClass average: " << getAverage() << std::endl;
std::cout << "\nClass maximum: " << getMaximum() << std::endl;
std::cout << "\nClass minimum: " << getMinimum() << std::endl;
std::cout << "Bar Chart:\n";
outputBarChart();
}
int getMaximum() const {
int highGrade{ 0 };
//range-based for loop
for (int grade : studentGrades) {
if (highGrade < grade) {
highGrade = grade;
}
}
enter code here
return highGrade;
}
int getMinimum() const {
int lowGrade{ 100 };
for (int grade : studentGrades) {
if (lowGrade > grade) {
lowGrade = grade;
}
}
return lowGrade;
}
double getAverage() const {
int sum{ 0 };
for (int grade : studentGrades) {
sum += grade;
}
return static_cast<double>(sum) / studentGrades.size();
}
void outputGrades() const {
std::cout << "\n The grades are: \n\n";
for (size_t i{ 0 }; i < studentGrades.size(); ++i)
{
std::cout <<"Student #"<< i + 1 << " grade: "<< studentGrades.at(i) << std::endl;
}
}
void outputBarChart() const {
std::cout << "\nGrade distribution:\n";
std::array<int, 11> frequency{};
for (int grade : studentGrades) {
++frequency[grade / 10];
}
for (size_t i{ 0 }; i < frequency.size(); ++i)
{
if (i == 0) {
std::cout << " 0-9:";
}
else if (i == 10) {
std::cout << " 100:";
}
else {
std::cout << i * 10 << "-" << (i*10) + 9 << ":";
}
for (unsigned stars{ 0 }; stars < frequency[i]; ++stars) {
std::cout << '*';
}
std::cout << std::endl;
}
}
private:
std::string courseName;
std::array<int, 10> studentGrades;
std::array<int, 10> studentNames;
};
std::vectorwould be a lot easier.