In: Computer Science
create a C++ Program
1. Ask and get a course name
2. Create an array of students of size 10,
3. Initialize the elements of the students array of appropriate names and grades
4. Create an object of class GradeBook (provide the course name and the created student array, in 3 above, as arguments to the constructor call. The arguments are used to initialize the data members of the class GradeBook.
Desired Output:
=========================================================
Enter course name: Object Oriented Programming
=====================Entering Students' Information===============================
Enter the name and grade for 10 students
student # 1 name: John
Student # 1 grade : 100
student # 2 name: Mark
Student # 2 grade : 100
student # 3 name: Jesus
Student # 3 grade : 89
student # 4 name: Tony
Student # 4 grade : 87
student # 5 name: Leo
Student # 5 grade : 79
student # 6 name: Don
Student # 6 grade : 75
student # 7 name: Devin
Student # 7 grade : 83
student # 8 name: Xavier
Student # 8 grade : 90
student # 9 name: jerry
Student # 9 grade : 25
student # 10 name: Jones
Student # 10 grade : 46
============================================================================
Welcome to the grade book for
Object Oriented Programming!
=====================After Processing Class's Grade===============================
The grades are:
Jonh : 100
Mark : 100
Jesus: 89
Tony : 87
Leo: 79
Don : 75
Devin: 83
Xavier : 90
Jerry : 25
Jones : 46
Class average is 77.40
Lowest grade is 25
Highest grade is 100
Grade distribution:
0-9:
10-19:
20-29: *
30-39:
40-49: *
50-59:
60-69:
70-79: **
80-89: ***
90-99: *
100: **
Press any key to continue . . .
SAMPLE CODE!!!!!!!!!!!!!!!!!!!!!!!!
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; } } 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; }; |
GradeBookDriver.cpp
#include<iostream> #include<string> #include"GradeBook.h" #include<array> using namespace std; int main() { string courseName = "COSC 1337 Object Oriented Programming"; array<int, 10> studentGrades{ 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; GradeBook myGradeBook(courseName,studentGrades); myGradeBook.setCourseName(courseName); myGradeBook.processGrades(); } |
GradeBookDriver.cpp
#include<bits/stdc++.h>
#include "GradeBook.h"
using namespace std;
int main()
{
array<pair<string,int>,10> a;
string courseName;
cout<<"Enter CourseName\t";
cin>>courseName;
cout<<"\n Enter the name and grade for 10 students\n";
string x;
int y;
for(int i=0;i<10;i++)
{
cin>>x>>y;
a[i]={x,y};
}
GradeBook g(courseName,a);
g.processGrades();
}
GradeBook.h
#include<bits/stdc++.h>
using namespace std;
class GradeBook{
private:
string courseName;
array<pair<string,int>,10> studentGrades;
public:
GradeBook(string Cname,array<pair<string,int>,10> &a)
{
courseName=Cname;
studentGrades=a;
}
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 (auto grade : studentGrades) {
if (highGrade < grade.second) {
highGrade = grade.second;
}
}
return highGrade;
}
int getMinimum() const {
int lowGrade=100;
for (auto grade : studentGrades) {
if (lowGrade > grade.second) {
lowGrade = grade.second;
}
}
return lowGrade;
}
double getAverage() const {
int sum=0;
for (auto grade : studentGrades) {
sum += grade.second;
}
return static_cast<double>(sum) / studentGrades.size();
}
void outputGrades() const {
std::cout << "\n The grades are: \n\n";
for(int i=0;i<studentGrades.size();i++)
{
cout<<"student #" <<i+1<<"name: "<<studentGrades[i].first<<endl;
cout<<"student #" <<i+1<<"grade: "<<studentGrades[i].second<<endl;
}
}
void outputBarChart() const {
std::cout << "\nGrade distribution:\n";
std::array<int, 11> frequency{};
for (auto grade : studentGrades) {
++frequency[grade.second / 10];
}
for (int 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;
}
}
};
Just make array of pair of string and int instead of normally pair.