Suppose you have a collection of student records. The records are structures of the following type:
struct StudentInfo
{
string name;
int grade;
};
The records are maintained in a vector<StudentInfo>. Write a program that prompts for and fetches data and builds a vector of student records, then sorts the vector by name, calculates the maximum and minimum grades, and the class average, then prints this summarizing data along with a class roll with grades. (We are not interested in who had the maximum and minimum grade, though, just the maximum, minimum, and average statistics.) Test your program.
C++ program code:
//header section
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// StudentInfo structure
struct StudentInfo
{
string name;
int grade;
};
// Function to prompt for and fetch student data
void fetchStudentData(vector<StudentInfo>& students)
{
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;
for (int i = 0; i < numStudents; i++) {
StudentInfo student;
cout << "Enter the name of student " << i + 1 << ": ";
cin >> student.name;
cout << "Enter the grade of student " << i + 1 << ": ";
cin >> student.grade;
students.push_back(student);
}
}
// Function to compare student records by name
bool compareByName(const StudentInfo& s1, const StudentInfo& s2) {
return s1.name < s2.name;
}
// Function to calculate the maximum grade
int calculateMaxGrade(const vector<StudentInfo>& students) {
int maxGrade = students[0].grade;
for (const auto& student : students) {
if (student.grade > maxGrade) {
maxGrade = student.grade;
}
}
return maxGrade;
}
// Function to calculate the minimum grade
int calculateMinGrade(const vector<StudentInfo>& students)
{
int minGrade = students[0].grade;
for (const auto& student : students) {
if (student.grade < minGrade) {
minGrade = student.grade;
}
}
return minGrade;
}
// Function to calculate the class average
double calculateClassAverage(const vector<StudentInfo>& students)
{
int total = 0;
for (const auto& student : students)
{
total += student.grade;
}
return static_cast<double>(total) / students.size();
}
// Function to print the class roll with grades
void printClassRoll(const vector<StudentInfo>& students)
{
cout << "Class Roll with Grades:" << endl;
for (const auto& student : students)
{
cout << student.name << ": " << student.grade << endl;
}
}
int main() {
vector<StudentInfo> students;
// Fetch student data
fetchStudentData(students);
// Sort the vector by name
sort(students.begin(), students.end(), compareByName);
// Calculate statistics
int maxGrade = calculateMaxGrade(students);
int minGrade = calculateMinGrade(students);
double average = calculateClassAverage(students);
// Print statistics and class roll
cout << "Maximum Grade: " << maxGrade << endl;
cout << "Minimum Grade: " << minGrade << endl;
cout << "Class Average: " << average << endl;
printClassRoll(students);
return 0;
}
Output of the program code:
Enter the number of students: 5
Enter the name of student 1: JOHN
Enter the grade of student 1: 1
Enter the name of student 2: SAM
Enter the grade of student 2: 2
Enter the name of student 3: MARTIN
Enter the grade of student 3: 1
Enter the name of student 4: RONALD
Enter the grade of student 4: 2
Enter the name of student 5: JAMES
Enter the grade of student 5: 2
Maximum Grade: 2
Minimum Grade: 1
Class Average: 1.6
Class Roll with Grades:
JAMES: 2
JOHN: 1
MARTIN: 1
RONALD: 2
SAM: 2