******IN C++************** Create a class, Student, with the following private a
ID: 3704759 • Letter: #
Question
******IN C++**************
Create a class, Student, with the following private attributes: name (string), and an array or vector to hold 5 grades (int). The class has public set and get methods as well as a public method to calculate and return the student’s average grade (as float).The main program will create an array or vector to handle up to 30 students. The data for all students will be read from file studentData.txt (which you will create) and stored into the array or vector of student objects. You must have between 6 and 30 students in your file (read until eof).Your program will process the array or vector of students and write to the console the student’s name, all grades, and average grade, using a column format. Allow 35 spaces for name, and eight spaces for each grade.After all students are listed, write summary data, one per line, of how many students were in the class, the highest and lowest average grade, and the average grade for the entire class.
Explanation / Answer
PLEASE REFER BELOW CODE
1 ) CREATE student_class.h AND PASTE BELOW CODE
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//Student class decleration
class Student
{
private:
string name; //name of student
int grades[5]; //grades of student
public:
void set_name(string name); //set name of student
string get_name(); //get name of student
void set_grades(int *arr); //set grades of student
int *get_grades(); //get grades of student
float avg_grade(int *arr); //avergae grade of student
};
2) CREATE student_class.cpp AND PASTE BELOW CODE
#include "student_class.h"
void Student::set_name(string Name)
{
name = Name; //copying string Name to name
}
string Student::get_name()
{
return name;
}
void Student::set_grades(int *arr)
{
//copying given array to class array
for(int i = 0; i < 5; i++)
{
grades[i] = arr[i];
}
}
int * Student::get_grades()
{
return grades;
}
float Student::avg_grade(int *arr)
{
int sum = 0;
for(int i = 0; i < 5; i++)
sum += arr[i];
//return avergae of grade
return (sum/5.0);
}
3) CREATE main.cpp AND PASTE BELOW CODE
#include "student_class.h"
int main()
{
Student S[30]; //create array of object
std::ifstream infile("studentData.txt "); //opening file using stream
string stud_name;
int g[5], count_student = 0;
int *temp;
float avg_grades = 0.0;
float highest_grade,lowest_grade;
if(infile) // if file is present
{
//reading file line by line
while(infile >> stud_name >> g[0] >> g[1] >> g[2] >> g[3] >> g[4])
{
//storing data into object
S[count_student].set_name(stud_name);
S[count_student].set_grades(g);
count_student++;
}
}
else
{
cout<<"File not present"<<endl;
return 0;
}
highest_grade = S[0].avg_grade(S[0].get_grades());
lowest_grade = S[0].avg_grade(S[0].get_grades());
cout<<"Students Data"<<endl;
cout<<"=================================================================="<<endl<<endl;
for(int i = 0; i < count_student; i++)
{
//print data
cout<<S[i].get_name()<<" ";
temp = S[i].get_grades();
for(int k = 0; k < 5; k++)
cout<<temp[k]<<" ";
cout<<S[i].avg_grade(S[i].get_grades())<<endl;
//find average of all grades and highest and lowest avge grade
avg_grades += S[i].avg_grade(S[i].get_grades());
if(S[i].avg_grade(S[i].get_grades()) < lowest_grade)
lowest_grade = S[i].avg_grade(S[i].get_grades());
else if(S[i].avg_grade(S[i].get_grades()) > highest_grade)
highest_grade = S[i].avg_grade(S[i].get_grades());
}
cout<<"=================================================================="<<endl<<endl;
cout<<"Summary: "<<endl<<endl;
cout<<"=================================================================="<<endl<<endl;
cout<<"Number of students in class : "<<count_student<<endl;
cout<<"Highest Average Grade : "<<highest_grade<<endl;
cout<<"Lowest Average Grade : "<<lowest_grade<<endl;
cout<<"Average Grade for the entire class : "<<(avg_grades / count_student)<<endl;
cout<<"=================================================================="<<endl<<endl;
infile.close();
return 0;
}
GIVEN INPUT FILE studentData.txt IS
Stephen 23 12 56 34 10
Thomas 78 23 45 89 90
Rickey 99 87 76 67 89
Linda 89 67 45 90 89
John 67 77 88 78 54
Lindsay 23 12 89 88 76
OUTPUT IS
Students Data
==================================================================
Stephen 23 12 56 34 10 27
Thomas 78 23 45 89 90 65
Rickey 99 87 76 67 89 83.6
Linda 89 67 45 90 89 76
John 67 77 88 78 54 72.8
Lindsay 23 12 89 88 76 57.6
==================================================================
Summary:
==================================================================
Number of students in class : 6
Highest Average Grade : 83.6
Lowest Average Grade : 27
Average Grade for the entire class : 63.6667
==================================================================
Process returned 0 (0x0) execution time : 0.071 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.