Project Exam Statistics (version II) Here is what your program will do: first it
ID: 3908013 • Letter: P
Question
Project Exam Statistics (version II) Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file. Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89 DR123 100 AJ222 98 SW111 45 12AB1 82 516BC 99 2ABCD 100 333XY 92 TY4XZ 45 AC234 78 9QWE9 45 JP200 89 AK323 100 The program should do the following: Read the contents of the file into two parallel arrays. Sort the arrays in ascending order by student ID Write the sorted arrays to another output file named scoresOut1.txt Sort the arrays in descending order by score Write the sorted arrays to an output file named scoresOut2.txt Display the highest score in the array and the ids of the students with that score Display the lowest score in the array and the ids of the students with that score Display the total number of students in the array Display the class averageExplanation / Answer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
ifstream fin("scores.txt");
if (!fin){
cout << "Error in opening file ";
return 0;
}
string id[100];
int score[100];
string id1[100];
int score1[100];
int count = 0;
while (fin >> id[count] >> score[count]){
id1[count] = id[count];
score1[count] = score[count];
count++;
}
fin.close();
for (int i = 0; i<count; i++){
for (int j = i+1; j<count; j++){
if (id[i] > id[j]){
string tempid = id[i];
int tempscore = score[i];
id[i] = id[j];
score[i] = score[j];
id[j] = tempid;
score[j] = tempscore;
}
}
}
ofstream out("scoresOut1.txt");
for (int i = 0; i<count; i++){
out << id[i] << " " << score[i] << endl;
}
for (int i = 0; i<count; i++){
for (int j = i+1; j<count; j++){
if (score1[i] < score1[j]){
string tempid = id1[i];
int tempscore = score1[i];
id1[i] = id[j];
score1[i] = score1[j];
id1[j] = tempid;
score1[j] = tempscore;
}
}
}
out.close();
ofstream out1("scoresOut2.txt");
for (int i = 0; i<count; i++){
out1 << id1[i] << " " << score1[i] << endl;
}
out1.close();
cout << "Maximum score:" << score1[0] << endl;
for (int i = 0; i<count; i++){
if (score1[i] == score1[0]){
cout << id1[i] << endl;
}
}
cout << "Minimum score:" << score1[count-1] << endl;
for (int i = 0; i<count; i++){
if (score1[i] == score1[count-1]){
cout << id1[i] << endl;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.