exercism/cpp/making-the-grade/making_the_grade.cpp

80 lines
2 KiB
C++

#include <array>
#include <cstdio>
#include <string>
#include <vector>
// Round down all provided student scores.
std::vector<int> round_down_scores(std::vector<double> student_scores) {
return std::vector<int>(student_scores.begin(), student_scores.end());
}
// Count the number of failing students out of the group provided.
int count_failed_students(std::vector<int> student_scores) {
int count{0};
for (auto score : student_scores) {
if (score <= 40)
count++;
}
return count;
}
// 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) {
std::vector<int> res;
for (auto score : student_scores) {
if (score >= threshold)
res.push_back(score);
}
return res;
}
// Create a list of grade thresholds based on the provided highest grade.
std::array<int, 4> letter_grades(int highest_score) {
int step{(highest_score - 40) / 4};
std::array<int, 4> grades{41, 0, 0, 0};
for (int i{1}; i < 4; ++i) {
grades[i] += grades[i - 1] + step;
}
return grades;
}
// Organize the student's rank, name, and grade information in ascending order.
std::vector<std::string>
student_ranking(std::vector<int> student_scores,
std::vector<std::string> student_names) {
std::vector<std::string> res;
int rank = 1;
for (int i{0}; i < student_scores.size(); i++, rank++) {
res.push_back(std::to_string(rank) + "." + " " + student_names[i] + ": " +
std::to_string(student_scores[i]));
}
return res;
}
// 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) {
for (int i{0}; i < student_scores.size(); i++) {
if (student_scores[i] == 100) {
return student_names[i];
}
}
return "";
}