Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How would I write a C++ Program to loop through a file (\"quizzes.dat\") (conten

ID: 669117 • Letter: H

Question

How would I write a C++ Program to loop through a file ("quizzes.dat") (contents below) and, for each student, output the student's ID number along with the student's quiz average after the higest and lowest quiz scores have been thrown out?

Students ID Quiz Scores

10234 67
10234 100
10234 53
10234 91
11245 89
11245 46
11245 99
14652 100
14652 56
14652 99
14652 100
14652 96
14652 78
19832 92
19832 78
19832 51
19832 76
19832 89
20014 100
20014 100
20014 100
20014 100
21140 43
21140 56
21140 90
21140 78
21140 63
21140 67
21140 89
22256 10
22256 7
22256 7
22256 2
22256 11
22256 2
22256 2
22256 13
22256 9
27654 83
27654 83
27654 83
27654 83
30021 78
30021 67
30021 92
30021 59
30021 82
30021 58

Explanation / Answer

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct data_student
{
int s_id;
int t_score;
int quizNo;
int H_Score;
int L_Score;
};


int main()
{
vector <data_student *> s_vector;

ifstream fileInput;
fileInput.open("quizzes.dat");

while(!fileInput.eof())
{
  int s_id, score_quiz;
  fileInput>> s_id>>score_quiz;
  
    
  bool b = false;
  for (int i = 0; i < s_vector.size() && !b; i++) // verify the s_id is existing
  {
   data_student * p = s_vector[i];
   if (p->s_id == s_id) // if exist, retrive
   {
    p->quizNo++;
    p->t_score += score_quiz;
    if (p->H_Score < score_quiz) p->H_Score = score_quiz;
    if (p->L_Score > score_quiz) p->L_Score = score_quiz;
    b = true;
   }          
  }
   
  if (!b || s_vector.empty()) // did not retrive it or vectoris empty
  {
   data_student * p = new data_student;
   p->s_id = s_id;
   p->t_score = score_quiz;
   p->H_Score = p->L_Score = score_quiz;
   p->quizNo = 1;
   s_vector.push_back(p);
  }
  
}
fileInput.close();

for (int i = 0; i < s_vector.size(); i++)
{
  data_student * p = s_vector[i];
  cout << "s_id: " << p->s_id << endl;
  cout << " HighScore: " << p->H_Score << endl;
  cout << " Lowest_Score: " << p->L_Score << endl;
  cout << " Average_Score " << p->t_score / p->quizNo << endl << endl;   
}

for (int i = 0; i < s_vector.size(); i++)
{
  data_student * p = s_vector[i];
  delete p;
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote