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

Write a C++ program to calculate students’ average test scores and their grades.

ID: 3760164 • Letter: W

Question

Write a C++ program to calculate students’ average test scores and their grades. Your program must use three arrays, sized to handle 50 students with five scores each: • a one-dimensional array to store the students’ names, • a parallel two-dimensional array to store the test scores, and • a parallel one-dimensional array to store grades. Your program must contain at least the following functions: • A function to read and store data into two arrays, • A function to calculate the average test score and grade, and • A function to output the results. Each line in input file will consist of a student name, followed by five integer scores, followed by a newline character. A space character separates each field. While the input file is guaranteed to be well formed (i.e. no missing data), there is no assurance how many students may be listed in the file. The file may be empty, or it may have more than fifty students. Code to the specification, not the sample input file.

If the input file is empty, your program should not print anything. Set the exit code appropriately

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


using namespace std;



bool readStudentData(ifstream &inFile, string &name, int &score1, int &score2, int &score3, int &score4, int &score5) {

   string line;

   if(getline (inFile,line)) {

       string word;

       istringstream iss(line, istringstream::in);
       int count = 0;
   try {
       while( iss >> word )
       {
           //cout << word << endl;
           if(count == 0)
               name = word;
           if(count == 1) {
               istringstream(word) >> score1;
           }
           if(count == 2) {
               istringstream(word) >> score1;
           }
           if(count == 1) {
               istringstream(word) >> score2;
           }
           if(count == 3) {
               istringstream(word) >> score3;
           }
           if(count == 4) {
               istringstream(word) >> score4;
           }
           if(count == 5) {
               istringstream(word) >> score5;
           }
                  count++;


       }
           //cout << name << " "<<score1 << " "<<score2 << " "<<score3 << " "<<score4 << " "<<score5 <<endl;

       return true;
   } catch (int e)
      {
         return false;
      }
}
   return false;
}


float getAverage(int score1, int score2, int score3, int score4, int score5) {
   float avg = (float) (score1+score2+score3+score4+score5 ) / 5;
   return avg;
}

char computeGrade(float average) {
//90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F.
   if(average >= 90 && average <= 100)
       return 'A';
   else if(average >= 80 && average < 90)
       return 'B';
   else if(average >= 70 && average < 80)
           return 'C';
   else if(average >= 60 && average < 70)
           return 'D';
   else
       return 'E';
}



int main() {
       string line;
          ifstream myfile ("D:/ravi/Cheg/student.txt");


       if (myfile.is_open())
       {
           string names[50];
           int scores[50][5];
           char grades[50];
           bool isValid = false;
           string name;
           int score1 = 0, score2 =0 , score3 =0 , score4 =0, score5 =0;
           int count = 0;
           while ( myfile.good())
           {
               isValid = readStudentData(myfile,name, score1, score2, score3, score4, score5);
               if(isValid) {
                   float avg = getAverage(score1, score2, score3, score4, score5);
                   char chgrade = computeGrade(avg);
                   names[count] = name;
                   scores[count][0] =score1;
                   scores[count][1] =score2;
                   scores[count][2] =score3;
                   scores[count][3] =score4;
                   scores[count][4] =score5;
                   grades[count] = chgrade;
                   count++;
                   cout << name << " "<<score1 << " "<<score2 << " "<<score3 << " "<<score4 << " "<<score5    << " , Average: "<<avg << " , Grade: "<<chgrade << endl;
               }

           }
           myfile.close();
       }

       else
           cout << "Unable to open file";

}

-----------output-------------

Smith 93 89 91 75 92 , Average: 88 , Grade: B
Johnson 82 68 96 99 97 , Average: 88.4 , Grade: B
Frank 95 88 96 88 97 , Average: 92.8 , Grade: A
Kenshi 94 90 95 99 89 , Average: 93.4 , Grade: A
Himaru 99 92 89 95 95 , Average: 94 , Grade: A
Allenet 85 79 90 93 99 , Average: 89.2 , Grade: B
Brown 99 89 92 94 99 , Average: 94.6 , Grade: A

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