#C++ Making the Grade segmentation fault error

7 messages · Page 1 of 1 (latest)

polar cairn
#

So, I have all the tasks for exercise complete and they don't fail as far as I know. I am getting a segmentation fault on compiling.

"'make[2]: *** [CMakeFiles/test_making-the-grade.dir/build.make:70: CMakeFiles/test_making-the-grade] Segmentation fault (core dumped)
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_making-the-grade.dir/all] Error 2
make: *** [Makefile:91: all] Error 2"'

I've thought that it might be that I am trying to access something not in the range of one of the vectors but I've tried **.size() -1 and <= in the while loops to try an see if that fixes it but no luck on that. I'm just stuck since the error isn't very helpful on finding where I'm messing up.
It could be that one of the tasks submits an empty vector as a parameter but i feel like that would be a task failure indicator not a compilation error but i could be wrong on that.

code paste after this as i have hit the character limit for this one post

#
#include <string>
#include <vector>

// Round down all provided student scores.
std::vector<int> round_down_scores(std::vector<double> student_scores) {
    // TODO: Implement round_down_scores
    int i = 0;
    std::vector<int> round_down{};
    while (i < student_scores.size()){
        round_down[i] = static_cast<int>(student_scores.at(i));
        i++;
    }
    return round_down;
}

// Count the number of failing students out of the group provided.
int count_failed_students(std::vector<int> student_scores) {
    // TODO: Implement count_failed_students
    int i = 0;
    int fail = 0;
    while (i < student_scores.size()){
        if (student_scores.at(i) <= 40){
            fail++;
        }
        i++;
    }
    return fail;
}

// Determine how many of the provided student scores were 'the best' based on the provided threshold.
std::vector<int> above_threshold(std::vector<int> student_scores, int threshold) {
    // TODO: Implement above_threshold
    int i = 0;
    std::vector<int> above{};
    while (i < student_scores.size()){
        if (student_scores.at(i) >= threshold){
            above.emplace_back(student_scores.at(i));
        }
        i++;
    }
    return above;
}

// Create a list of grade thresholds based on the provided highest grade.
std::array<int, 4> letter_grades(int highest_score) {
    // TODO: Implement letter_grades
    int range = highest_score - 40;
    int step = range/4;
    int d = 41;
    int c = d + step;
    int b = c + step;
    int a = b + step;
    std::array<int,4> letter_range;
    letter_range[0] = d;
    letter_range[1] = c;
    letter_range[2] = b;
    letter_range[3] = a;
    return letter_range;
}```
#
std::vector<std::string> student_ranking(std::vector<int> student_scores, std::vector<std::string> student_names) {
    // TODO: Implement student_ranking
    int i = 0;
    int rank = 1;
    std::vector<std::string> ranking{};
    while (i < student_scores.size()){
        ranking.emplace_back((std::to_string(rank) + ". " + student_names.at(i) + ": " + std::to_string(student_scores.at(i))));
        i++;
        rank++;
    }
    return ranking;
}

// Create a string that contains the name of the first student to make a perfect score on the exam.
std::string perfect_score(std::vector<int> student_scores, std::vector<std::string> student_names) {
    // TODO: Implement perfect_score
    int i = 0;
    while (i < student_scores.size()){
        if (student_scores.at(i) == 100){
            return student_names.at(i);
            break;
        }
        i++;
    }
    return "";
}```
stiff badger
#

You do round_down[i] = …, but round_down’s capacity at point is zero.

#

You can initialize round_down with the correct number of elements (std::vector<int> round_down(student_scores.size());)

#

In general your while loops can be replaced with for (;;) style loops, but even better would be to use the C++11 for-range loops: for (int score : student_scores)

#

There are other refactorings (using <algorithm>) you can also try if you wanna try newer-ish ways of writing C++ :)