In: Computer Science
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3 L4 L5 L6 L7
L8 L9 P0 P1 P2 E0 E1 E2 FI ATT
------------------------------------------------------------------------------------------------------------------------------------------
Kevin Smith 90 100 100 100 98 97 87 100 85 87 89 100 100 100 100 90
100 98 90 100 98 98 98 90 90 98 88 0.00
Morgan Kelly 80 100 65 67 69 71 100 100 100 67 95 85 87 89 100 65
67 69 71 100 98 98 98 65 67 69 71 0.10
Isaac Newton 100 90 100 90 100 90 100 90 100 100 100 100 100 100
100 100 100 100 100 100 98 98 98 90 90 98 88 0.00
Cole Jones 100 100 100 87 73 75 77 79 81 87 89 91 73 75 77 79 81
100 100 100 98 100 65 67 69 71 63 0.05
Angela Allen 100 100 100 87 89 91 93 95 100 100 100 100 100 100 100
95 97 100 98 98 98 90 73 75 77 79 81 0.02
David Cooper 56 58 60 62 64 100 100 100 87 73 75 77 100 100 77 79
81 100 100 100 98 70 72 74 76 78 88 0.00
Nancy Bailey 100 87 89 91 93 95 100 100 100 100 100 100 91 93 95 97
100 98 100 100 98 98 98 90 90 98 88 0.00
Emily Synder 65 67 69 71 62 64 73 75 77 58 60 62 79 66 68 70 72 81
74 76 78 90 90 74 76 98 88 0.00
Lori Austin 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 98 98 98 90 90 98 88 0.02
Jenny Howard 56 58 60 62 71 62 64 73 100 66 68 70 72 74 76 78 100
100 100 60 62 79 66 68 70 68 70 0.03
Anne Lewis 100 86 58 60 100 71 62 64 73 94 66 68 90 72 74 76 78 67
68 69 70 71 98 88 76 78 68 0.04
Nick Johnson 100 100 89 91 73 75 77 79 81 100 100 100 98 100 100 95
85 87 89 100 98 98 98 80 76 78 98 0.01
Nick Spickler 100 93 95 97 100 98 98 98 90 100 89 91 93 95 97 100
100 89 91 93 95 97 98 98 90 90 98 0.00
Joy Williams 75 77 58 60 62 79 66 68 70 72 81 100 100 71 62 64 73
94 66 98 90 90 98 68 90 88 77 0.00
Barbara Hood 100 67 95 85 87 89 91 93 95 97 85 87 100 100 100 71 62
64 73 94 66 68 98 98 90 90 88 0.00
Joe Hoarn 62 63 64 65 66 67 68 69 70 71 100 81 100 100 71 62 64 73
100 100 98 98 64 73 94 66 68 0.08
Payton Bardzell 100 100 100 97 87 67 95 85 87 89 91 93 95 97 100
100 100 95 85 87 89 100 98 90 90 78 98 0.00
Kim Ludwig 71 62 64 73 75 77 58 60 62 79 66 68 70 72 81 100 100 79
66 68 70 72 98 98 90 90 98 0.09
Susan Honks 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 90 90 88 100 100 100 100 0.00
Write the program in C++ read the grades of all the students from the file above. For each student’s scores,
The program most be define arrays to keep scores for quizzes, labs, projects, midterms. Secondly, read in the grades for each student and define a function to calculate and return the average grade of quizzes, labs, projects and midterms. Sample prototype of this function: float getAverage(float gradeArray[], int arraySize); // a function to calculate average of array. Also, define a function to calculate and return the final letter grade of this student. Note the total grade, average project grade, and the attendance will be taken into consideration when the final grade is calculated. Last importantly, output this student’s first name, last name, and final letter grade in a nice format to a file named letter120.dat. Each student’s data is in one line. This file should be in the same location as data that is given from the above file. The format should be as following:
First Name Last Name Final Grade
Nick Johnson A
Anne Lewis B
Writing the program in C++
Use constants to keep the percentage values in the above table.
COMMENT YOUR CODE.
Give meaningful variable, array, and function names based on this program.
Arrays and functions are implemented properly.
The grade is calculated correctly and output file is generated properly.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
float getFinalGrade(float totalGrade, float averageProjectGrade, float att);
//Function to calculate the average grade
float getAverage(vector<int> grades, const int size) {
float avg = 0.0;
for (auto iterator: grades) {
avg += *iterator;
}
return (avg/size);
}
int main() {
//Declarations
int noOfStudents;
const vector<string> studentFirstNames, studentLastNames;
const int noOfQuizzes = 10, noOfLabs = 10, noOfProjects = 3, noOfMidterms = 3;
const vector<vector<int>> quizzes, labs, projects, midterms;
const vector<float> totalGrade, attendance;
numberOfStudents = 0;
//File declare and open input file
fstream inputFile;
inputFile.open("input.dat", ios::in);
/If file is open read and store input in arrays
if (inputFile.is_open()) {
string line;
while (getline(inputFile, line)) {
++noOfStudents;
//Split string by whitespaces
string token = strtok(line, " ");
int i = 0;
//Arrays to store quiz grades of each student
vector<int> quiz, lab, project, midterm;
while (token != NULL) {
if (i == 0) {
studentFirstNames.push_back(token);
} else if (i == 1) {
studentLastNames.push_back(token);
} else if (i <= 11) {
quiz.push_back(stoi(token));
} else if (i <= 21) {
lab.push_back(stoi(token));
} else if (i <= 24) {
project.push_back(stoi(token));
} else if (i <= 27) {
midterm.push_back(stoi(token));
} else if (i == 28) {
totalGrade.push_back(stoi(token));
} else {
attendance.push_back(stoi(token));
}
++i;
token = strtoken(line, " ");
}
//Add all the arrays into respectives
quizzes.push_back(quiz);
labs.push_back(lab);
projects.push_back(project);
midterms.push_back(midterm);
}
//Close the input file
inputFile.close();
}
//If file does not open display error and exit
else {
cout<<"There was a problem in reading data from file!!!";
return 0;
}
vector<float> quizAverages, labAverages, projectAverages, midtermAverages;
int iterator;
for (iterator = 1; iterator <= noOfStudents; ++iterator) {
//Calculate averages by calling getAverage function
quizAverages.push_back(getAverage(quizzes[iterator], noOfQuizzes));
labAverages.push_push(getAverage(labs[iterator], noOfLabs));
projectAverages.push_back(getAverage(projects[iterator], noOfProjects));
midtermAverages.push_back(getAverage(midterms[iterator], noOfMidterms));
}
//Array to store finalGrade
vector<string> finalGrade;
int iterator;
for (iterator = 1; iterator <= noOfStudents; ++i) {
//Calculate final grade by getFinalGrade function
//Hope you can write this as there has been no logic specifies
finalGrades.push_back(getFinalGrade(totalGrade[iterator], projectAverages[iterator], attendance[iterator]));
}
//Declare and open output file
fstream outputFile;
outputFile.open("letter120.dat", ios::out);
//If file is not opened display error and exit
if (!outputFile.is_open()) {
cout<<"Problem writing output to file";
return 0;
}
//Wrtie headings first to output
outputFile<<"FIrst Name Last Name Final Grade\n";
//Write line by line to file
for (iterator = 1; iterator <= noOfStudents; ++i) {
outputFile<<studentFirstName[iterator]<<" "<<studentLastName[iterator]<<" "<<finalGrade[iterator];
outputFile<<"\n";
}
return 0;
}
This program reads input from a file (should be in specified format)
Calculates averages and final Grades respectively and then finally writes this output to a file named letter120.dat file