Start learning free

LESSON 239 OF 667 · HELPER FUNCTIONS

Letter Grades in C++: A Helper Function and a Loop

Practice Read the full explanation

Given a vector of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    C++ · HELPER FUNCTIONS

    Letter Grades in C++, explained

    Problem overview

    The function letterGrades receives a vector of integer scores and returns a vector<string> with one letter per score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".

    The scores [95, 82, 71] give [A, B, C]. The scores [65, 40] give [D, F]. The scores [100, 90, 89] give [A, A, B]: 90 is the lowest A, and 89 is one point short.

    Approach: one function decides, the other repeats

    The lesson splits the task in two. The helper letterGrade answers a single question: which letter does this one score get? The function that is checked, letterGrades, loops over the vector and asks the helper once per score. Each function does one job, and the helper can be tested on its own.

    Inside the helper, the order of the checks does the work. Test the highest range first. A return ends the function, so by the time the check for 80 runs, every score of 90 or more has already left. At that point "at least 80" means "80 to 89", and the upper bound never needs to be written. The same holds for the checks that follow, and the final "F" needs no test: only scores below 60 reach it.

    Take the score 89 from the third example. It fails score >= 90, so the function moves on. It passes score >= 80, so the function returns "B" and stops; the checks for 70 and 60 never run. A score of 40 fails all four tests and reaches the final return "F".

    Step by step

    1. Return "A" when score >= 90. Start from the top range.
    2. Return "B" when score >= 80. Only scores below 90 get here.
    3. Return "C" when score >= 70.
    4. Return "D" when score >= 60.
    5. Return "F" after the last if. Everything below 60 ends up here.
    6. In letterGrades, declare an empty vector<string> named grades. The letters will be added one at a time.
    7. Loop over scores with a range-based for and an int named score. The loop runs once per score.
    8. Append the helper's answer. grades.push_back(letterGrade(score)) adds the letter at the end, which keeps the original order. Both functions are in the same class, so the call needs only the function's name.
    9. Return grades after the loop. Returning inside the loop would send back only the first letter, or nothing at all.

    The solution in C++

    class Solution {
    public:
        string letterGrade(int score) {
            if (score >= 90) {
                return "A";
            }
            if (score >= 80) {
                return "B";
            }
            if (score >= 70) {
                return "C";
            }
            if (score >= 60) {
                return "D";
            }
            return "F";
        }
    
        vector<string> letterGrades(vector<int> &scores) {
            vector<string> grades;
            for (int score : scores) {
                grades.push_back(letterGrade(score));
            }
            return grades;
        }
    };
    

    C++ details:

    • String literals become string. return "A"; is a C-style string literal, which is converted to a std::string because the function's return type is string.
    • The same name in two places. The helper's parameter and the loop variable are both called score. They are separate variables: the call letterGrade(score) copies the loop's value into the helper's parameter.
    • push_back grows the vector. Each call adds one element at the end, so after the loop grades has exactly as many letters as scores has numbers.
    • The vector is taken by reference. The & avoids copying the scores.

    Testing the helper on its own

    Because letterGrade is an ordinary member function of Solution, it can be called directly with a single score: letterGrade(79) returns "C" and letterGrade(80) returns "B". Checking the boundaries of the helper before the loop exists is the main practical gain of the split: when the output of letterGrades is wrong, you know whether the fault is in the grading rule or in the loop that collects the results.

    Why not one long condition per range?

    You could write each test with both ends, such as score >= 80 && score <= 89. It works, but every boundary then appears twice, once as the top of one range and once as the bottom of the next, and a typo in either place opens a gap or an overlap. Because each return removes the scores it handled, the version in the lesson states each boundary exactly once.

    Another option is a chain of else if. It behaves the same here, since every branch returns, and it is a matter of taste. The lesson's form, with plain if statements that return, keeps every test at the same indentation and shows clearly that return ends the function.

    Edge cases and common mistakes

    • Boundaries. The scores 0, 59, 60, 69, 70, 79, 80, 89, 90 and 100 give F, F, D, D, C, C, B, B, A, A. Writing > instead of >= would shift every boundary score down a grade.
    • Checking the lowest range first. If the first test were score >= 60, a score of 95 would return "D". The tests must go from high to low.
    • Returning inside the loop. Only one letter would be produced.
    • A missing final return. A function that can end without returning a value has undefined behaviour in C++. The closing return "F" covers every score that passed no earlier test.

    Testing

    • [95, 82, 71] gives [A, B, C].
    • [65, 40] gives [D, F].
    • [100, 90, 89] gives [A, A, B].
    • [0, 59, 60, 69, 70, 79, 80, 89, 90, 100] gives [F, F, D, D, C, C, B, B, A, A].
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free