#include <fstream>
#include <iostream>
#include <vector>
#include "printFunctions.h"
usingnamespace std;
//You will need to write these functions.
void readScores(vector< vector<double> > &scores);
void calcGrades(vector< vector<double> > &scores, vector<double> &grades);
void calcAverages(vector< vector<double> > &scores, vector<double> &averages);
vector<int> getFailing(vector<double> &grades);
int main()
{
//Declare Variables:
vector< vector<double> > scores;
vector<double> grades, averages;
vector<int> failing;
//Call the function to read in the scores.
readScores(scores);
//Call the function to calculate the overall grades.
calcGrades(scores, grades);
//Call the function to calculate the per-assignment averages.
calcAverages(scores, averages);
//Call the function to display the average grades.
printAverages(averages);
//Call the function to look for failing students.
failing = getFailing(grades);
//Call the function to print the failing grades.
printFailing(failing);
}
/* Store the 5 scores
* for a student into a vector and then place that vector into the scores
* vector. Such that for n students the scores vector should be n by 5.*/
void readScores(vector< vector<double> > &scores)
{
//Declare variables
double p1, p2, p3, e1, e2;
//Open up "scores.txt"
ifstream infile("scores.txt");
//Loop through all of the Lines.
while(! infile.fail()){
infile >> p1 >> p2 >> p3 >> e1 >> e2;
vector<double> student(5);
student[0]=p1;
student[1]=p2;
student[2]=p3;
student[3]=e1;
student[4]=e2;
scores.push_back(student);
}
//Store each score for this student in a vector.
//Push Back that student onto scores.
//Close filestream.
infile.close();
}
/* From the vector of scores calculate each students grade and place it in
* the vector of grades in its respective position. */
void calcGrades(vector< vector<double> > &scores, vector<double> &grades)
{
//Loop through scores.
for(int i=0; i<scores.size(); i++){
vector<double> grade(scores.size());
grade[i] = (scores[i][0]+scores[i][1]+scores[i][2])*(40/75)+(scores[i][3]+scores[i][4])*(60/200);
grades.push_back(grade[i]);
}
//Push Back grades.
}
/* For each of the assignments, calculate the average score and place it into
* the averages vector. Averages should be of length 5.*/
void calcAverages(vector< vector<double> > &scores, vector<double> &averages)
{
int n;
for(int i=0; i<scores.size(); i++){
averages[0]=averages[0]+scores[i][0];
averages[1]=averages[1]+scores[i][1];
averages[2]=averages[2]+scores[i][2];
averages[3]=averages[3]+scores[i][3];
averages[4]=averages[4]+scores[i][4];
n=i+1;
}
averages[0] = averages[0]/n;
averages[1] = averages[1]/n;
averages[2] = averages[2]/n;
averages[3] = averages[3]/n;
averages[4] = averages[4]/n;
}
/* This function takes in the vector of student grades and returns a vector
* of the indices of students who are failing. */
vector<int> getFailing(vector<double> &grades)
{
//Declare variables
vector<int> failing;
//Loop through grades
for(int i=0; i<grades.size(); i++){
if(grades[i]<50){
failing.push_back(i);
}
}
//Check if the student is failing.
//Return a vector of failing students.
return failing;
}
In function calcAverages what is the value of averages.size() ?
By the way there are a few errors in the code. Integer division at line 76 will be a problem. Hint: what is the value of 40/75 when both values are integers.
There is a logic error in the while loop at line 47.
Hint: you need to check the file status after attempting to read from it, not before.