In: Computer Science
I have implemented GradeBook class as per the given description.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
#include<fstream>
using namespace std;
class GradeBook{
private:
//VARIABLES TO STORE THE courseName,nbOfStudents,grades
string courseName;
int nbOfStudents;
double **grades;
public:
/* A no-arg constructor that initializes the nbStudent to 5,
the courseName to empty string and the array grades to an
array of 5 rows and 3 columns.
*/
GradeBook(){
courseName="";
nbOfStudents=5;
grades=new double*[nbOfStudents];
for(int i=0;i<nbOfStudents;i++)
grades[i]=new double[3];
}
/*A constructor that takes as parameters the number of students,
the course name and the array grades and sets the corresponding
values by calling setGradeBook
*/
GradeBook(int nofSt,string name,double **g){
courseName=name;
nbOfStudents=nofSt;
grades=new double*[nbOfStudents];
for(int i=0;i<nbOfStudents;i++)
grades[i]=new double[3];
setGradeBook(g);
}
/*A copy constructor that takes an object of class GradeBook and copies the data to the instantiated object.*/
GradeBook(const GradeBook &g){
courseName=g.courseName;
nbOfStudents=g.nbOfStudents;
grades=new double*[nbOfStudents];
for(int i=0;i<nbOfStudents;i++)
grades[i]=new double[3];
setGradeBook(g.grades);
}
// destructor that deletes the array grades.
~GradeBook(){
delete[] grades;
}
//- setGradeBook that takes all parameters and sets the attributes values.
void setGradeBook(double **g){
int i=0,j=0;
for(i=0;i<nbOfStudents;i++)
for(j=0;j<3;j++)
grades[i][j]=g[i][j];
}
//- getMaximum that returns the highest grade in the array grades.
double getAverage(double *g){
return (g[0]+g[1]+g[2])/3;
}
/*- getAverage that takes a one dimensional array
representing the three grades of a student, one row in the array grades,
and returns the average of the student.*/
double getMaximum(double **g){
int i=0,j=0;
double max=0;
for(i=0;i<nbOfStudents;i++)
for(j=0;j<3;j++)
{
if(grades[i][j]>max)
max=grades[i][j];
}
return max;
}
//display the result ot the require stream
void display(ostream &out){
out<<"The Grade Book"<<endl<<"Course Name : "<<courseName<<endl<<"Number Of Studnets : "<<nbOfStudents<<endl;
out<<setw(8)<<left<<"Test 1"<<setw(8)<<"Test 2"<<setw(8)<<"Final"<<setw(8)<<"Agerage"<<endl;
for(int i=0;i<nbOfStudents;i++){
for(int j=0;j<3;j++)
{
out<<setw(8)<<grades[i][j];
}
out<<setw(8)<<setprecision(4)<<getAverage(grades[i]);
out<<endl;
}
out<<"Maximum Grade is :"<<getMaximum(grades);
}
};
int main(){
double **grades;
ofstream out("output.txt");
grades=new double*[5];
srand(time(0));
//generate random grades
for(int i=0;i<5;i++)
grades[i]=new double[3];
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
grades[i][j]=40+rand()%60;
//create the grade book
GradeBook g(5,"CPP",grades);
//display the output
g.display(cout);
//save the output to a file
g.display(out);
cout<<"\nThis Output is saved into a file 'output.txt'";
}