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

1) Write and run a C++ program that takes a file (grades.txt) containing the dif

ID: 3620371 • Letter: 1

Question

1)      Write and run a C++ program that takes a file (grades.txt) containing the different grades of the students and prints out the following information:

1)      the student’s name,

2)      his score on the Midterm,

3)      his overall homework grade obtained by calculating the average of the 3 higher homework grades over the 5 homework grades given in the input file

4)      his score on the final exam,

5)      his course average calculated as follows: 30% for the midterm, 40 % for the final exam and 30 % for the homework, and finally

6)      his letter grade for the course determined as follows:

            average                      grade for course        

            90 – 100                                  A

            80 - <<90                                 B

            70 - <<80                                 C

            below 70                                  F          where << means less than, but not equal to

SAMPLE INPUT FILE :

Fatma, Alhammadi

90 100

10 10 10 2 1

Mariam, ElMazrouei

90 90

1 2 3 4 5

Mohamed, Hamad

100 100

10 10 10 10 10

(In this example, the second input is for the midterm grade and the third one is for the final exam grade.)

SAMPLE OUTPUT:

Name                               Midterm HMAvg   Final    Avg        Grade

Fatma, Alhammadi            90        100      100        97                     A

Mariam, ElMazrouei        90        80           100      91       A

Mohammad, Hamad          100     100      100      100                       A

# include <iostream>

# include <string>

# include <fstream>

using namespace std;

struct studentType

{

string firstname;

string lastname;

double midterm;

double final;

double homework[5];

double hmavg;

double average;

char grade;

};

int arraySize();

void copyFromFileToArray (studentType *s, int n);

void displayData (studentType *s, int n);

void homeworkAverage(studentType *s, int n);

void averageAll (studentType *s, int n);

void grade (studentType *s, int n);

void arrangeAlphabetically(studentType *s, int n);

void arrangeByGrades(studentType *s, int n);

int main()

{

int n = arraySize();

studentType *s = NULL;

s = new studentType[n];

copyFromFileToArray(s, n);

homeworkAverage(s, n);

averageAll(s, n);

grade(s, n);

cout << " - Press 1: to print the data in alphabetical order ";

cout << " - Press 2: to print the data according to their grades ";

cout << " Your Choice is: ";

int choice; cin >> choice;

switch (choice)

{

case 1:

arrangeAlphabetically(s, n);

break;

case 2:

arrangeByGrades(s, n);

break;

}

displayData(s, n);

return 0;

}

int arraySize()

{

ifstream inFile;

inFile.open("grades.txt");

int size = 0;

string fn, ln;

double m, f;

double h[5];

while (!inFile.fail())

{

inFile >> fn;

inFile >> ln;

inFile >> m;

inFile >> f;

for (int j=0; j<5; j++)

inFile >> h[j];

size++;

}

inFile.close();

return (size-1);

}

void copyFromFileToArray(studentType *s, int n)

{

ifstream inFile;

inFile.open("grades.txt");

for(int i=0; i<n; i++)

{

inFile >> s[i].firstname;

inFile >> s[i].lastname;

inFile >> s[i].midterm;

inFile >> s[i].final;

for (int j=0; j<5; j++)

inFile >> s[i].homework[j];

}

inFile.close();

}

void homeworkAverage(studentType *s, int n)

{

double max, hmSum;

int indexMax;

for(int k=0; k<n; k++)

{

for (int i=0; i<4; i++)

{

max = s[k].homework[i];

indexMax = i;

for (int j=i+1; j<5; j++)

{

if (s[k].homework[j]>max)

{

max = s[k].homework[j];

indexMax = j;

double temp = s[k].homework[i];

s[k].homework[i] = s[k].homework[indexMax];

s[k].homework[indexMax] = temp;

}

}

}

}

for (int i=0; i<n; i++)

{

hmSum = s[i].homework[0]+s[i].homework[1]+s[i].homework[2];

s[i].hmavg = ((hmSum/3)/10)* 100;

}

}

void averageAll (studentType *s, int n)

{

for (int i=0; i<n; i++)

{

s[i].average = ((s[i].midterm/100*30) + (s[i].hmavg/100*30) + (s[i].final/100*40));

}

}

void displayData (studentType *s, int n)

{

cout << " Name Midterm HMAvg Final Avg Grade ";

cout << "___________________________________________________________ ";

for (int i=0; i<n; i++)

{

cout << s[i].firstname << " ";

cout << s[i].lastname << " ";

cout << s[i].midterm << " ";

cout << s[i].hmavg << " ";

cout << s[i].final << " ";

cout << s[i].average << " ";

cout << s[i].grade;

cout << endl << endl;

}

cout << endl;

}

void grade(studentType *s, int n)

{

char grade;

for (int i=0; i<n; i++)

{

if(s[i].average<=100 && s[i].average>=90)

grade = 'A';

else if(s[i].average<90 && s[i].average>=80)

grade = 'B';

else if(s[i].average<80 && s[i].average>=70)

grade = 'C';

else if(s[i].average<70)

grade = 'F';

s[i].grade = grade;

}

}

void arrangeAlphabetically(studentType *s, int n)

{

string first;

int indexFirst;

for(int i=0; i<n-1; i++)

{

first = s[i].firstname;

for(int j=i+1; j<n; j++)

{

if(s[j].firstname[0]<first[0])

{

first = s[j].firstname;

indexFirst = j;

studentType temp = s[i];

s[i] = s[indexFirst];

s[indexFirst] = temp;

}

}

}

}

void arrangeByGrades(studentType *s, int n)

{

studentType min;

int indexMin;

for(int i=0; i<n-1; i++)

{

min.grade = s[i].grade;

for(int j=i+1; j<n; j++)

{

if(s[j].grade<min.grade)

{

min.grade = s[j].grade;

indexMin = j;

studentType temp = s[i];

s[i] = s[indexMin];

s[indexMin] = temp;

}

}

}

Explanation / Answer

Please rate -- thanks! Your function with slight modification: void arrangeByGrades(studentType *s, int n) { studentType min; int indexMin; for(int i=0; i