I am getting a segmentation fault, why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <fstream>
#include <iostream>
#include <vector>
#include "printFunctions.h"

using namespace 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.
Last edited on
Topic archived. No new replies allowed.