#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int size=100;
struct studentData {
string studentFirstName; //stores student's first name
string studentLastname; //stores student's last name
int examGrade; //stores student's exam grades
char letterGrade; //stores student's letter grade
};
studentData student [size]; //sets an array called student of the type StudentData
void getData (int listSize, studentData sList [], ifstream &);
void calculatedGrade (studentData sList[], int listSize);
int highestScore (const studentData slist[], int listSize);
void printStudentNameResult (const studentData sList [], int listSize);
double average (studentData student[], int n);
double standardDeviation (studentData students [], int n, double avg);
int main () {
ifstream inFile;
inFile.open("student.txt");
if (!inFile) {
cerr << "Error opening file." << endl;
exit (1);
}
int n=getData (ifstream, student);
calculatedGrade (student, n);
printStudentNameResult (student, n);
int index=highestScore (student, n);
cout << "The student with the highest score is:" << student[index]. studentFirstName << " " << student[index].studentLastName << endl;
double avg= average (student, n);
cout << "The average grade in the class is:" << avg << endl;
double SD= standardDeviation (student, n, avg);
cout << "The Standard deviation was:" << SD << endl;
return 0;
}
int getData (ifstream & inData, studentData student []) {
int i=0;
inData >> student[i].studentFirstName;
while (i < 100 && !inFile.eof()) {
inData >> student[i].studentLastName;
inData >> student[i].examGrade;
i++;
inData >> student[i].studentFirstName;
}
return i; // i is the number of students
}
void calculatedGrade (studentData student [], int n) {
for (int i=0; i<n; i++) {
if (student[i].examGrade >= 90)
student[i].letterGrade = 'A';
else if (student[i].examGrade >= 80)
student[i].letterGrade = 'B';
else if (student[i].examGrade >= 70)
student[i].letterGrade = 'C';
else if (student[i].examGrade <= 60)
student[i].letterGrade = 'F';
}
}
void printStudentNameResult (studentData student [], int n) {
for (int i=0; i<n; i++) {
cout << student[i].studentFirstName << " " << student[i].studentLastName << " " << student[i].letterGrade << endl;
}
}
int highestScore (studentData student [], int n) {
int highestTest= student[0].examGrade;
int index=0;
for (int i=0; i<n; i++) {
if(student[i].examGrade > highestTest) {
highestTest=student[i]. examGrade;
index=i;
}
}
return index;
}
double average (studentData student[], int n) {
int sum=0;
for (int i=0; i<n; i++) {
sum+=student[i].examGrade;
}
double average = double (sum)/n;
return average;
}
double standardDeviation (studentData students [], int n, double avg) {
int sum=0;
for (int i=0; i<n; i++) {
pow (student[i].examGrade-avg, 2);
}
double result= double(sum)/(n-1);
result= sqrt (result);
return result;
}
Comments
Post a Comment