194k views
1 vote
DO NOT DESIGN A CLASS, THIS MUST BE A PROCEDURAL DESIGN.

You are given a list of students’ names and their test scores on a file (StudentData.txt in the replit
directory). The file has one name and one test score per line. Assume that the number of records on the
file is not known in advance (i.e., your program should run with any number of records without having
to be recompiled). Design a program that:
 Reads the student names and test score records from the data file into parallel arrays or an
array of structs
 Given a list of scores, calculate and return the average of the scores in the list
 Given a list of scores, find and return the highest score in the list
 Given the average score, the list(s) of student names and scores, return a list of students who
scored below average
 Given the highest score, the list(s) of student names and scores, return a list of students with the
highest score
 Write a report that displays the average test score and prints the names of all the students who
scored blow average and then displays the highest score and the names of students who got the
highest score.


This is the text file (studentscores.txt):
Olivia 68
Noah 74
Emma 62
Liam 92
Amelia 99
Oliver 100
Ava 65
Elijah 64
Sophia 93
Lucas 99
Isabella 88
Mateo 77
Mia 90
Gold 56
Luna 99
Levi 96
Charlotte 95
Ethan 77
Evelyn 83
James 59
Harper 64
Asher 90
Ella 66
Leo 93
Gianna 91
Luca 59
Aurora 67
Benjamin 94
Scarlett 58
Grayson 65
Nova 71
Aiden 61
Ellie 53
Ezra 87

User Zabrina
by
7.8k points

1 Answer

4 votes

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;

User DerKorb
by
8.3k points