u owe me :) This is in C++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// define a struct to hold student data
struct Student {
string name;
int score;
};
// function to read student data from file
vector<Student> readStudentData(string filename) {
vector<Student> students;
ifstream inputFile(filename);
if (inputFile.is_open()) {
string line;
while (getline(inputFile, line)) {
// split line into name and score
int spacePos = line.find(" ");
string name = line.substr(0, spacePos);
int score = stoi(line.substr(spacePos+1));
// create student struct and add to vector
Student student = {name, score};
students.push_back(student);
}
inputFile.close();
} else {
cout << "Unable to open file" << endl;
}
return students;
}
// function to calculate the average score of a list of scores
double calculateAverageScore(vector<int> scores) {
double sum = 0;
for (int i = 0; i < scores.size(); i++) {
sum += scores[i];
}
return sum / scores.size();
}
// function to find the highest score in a list of scores
int findHighestScore(vector<int> scores) {
int highest = scores[0];
for (int i = 1; i < scores.size(); i++) {
if (scores[i] > highest) {
highest = scores[i];
}
}
return highest;
}
// function to find students who scored below the average
vector<string> findStudentsBelowAverage(vector<Student> students, double averageScore) {
vector<string> belowAverageStudents;
for (int i = 0; i < students.size(); i++) {
if (students[i].score < averageScore) {
belowAverageStudents.push_back(students[i].name);
}
}
return belowAverageStudents;
}
// function to find students who got the highest score
vector<string> findStudentsWithHighestScore(vector<Student> students, int highestScore) {
vector<string> highestScoringStudents;
for (int i = 0; i < students.size(); i++) {
if (students[i].score == highestScore) {
highestScoringStudents.push_back(students[i].name);
}
}
return highestScoringStudents;
}
int main() {
// read student data from file
vector<Student> students = readStudentData("studentscores.txt");
// calculate average score
vector<int> scores;
for (int i = 0; i < students.size(); i++) {
scores.push_back(students[i].score);
}
double averageScore = calculateAverageScore(scores);
// find students who scored below average
vector<string> belowAverageStudents = findStudentsBelowAverage(students, averageScore);
// find highest score
int highestScore = findHighestScore(scores);
// find students who got the highest score
vector<string> highestScoringStudents = findStudentsWithHighestScore(students, highestScore);
// display report
cout << "Average test score: " << averageScore << endl;
cout << "Students who scored below average: ";
for (int i = 0; i < belowAverageStudents.size(); i++) {
cout << belowAverageStudents[i] << " ";
}
cout << endl;