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

81 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.emplace_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 len = student_scores.size();
for (int i{0}; i < len; i++) {
res.emplace_back(std::to_string(i + 1) + "." + " " + 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) {
int len = student_scores.size();
for (int i{0}; i < len; i++) {
if (student_scores[i] == 100) {
return student_names[i];
}
}
return "";
}