Kodewick
Start learning →
Basic←→0 / 60

Café stars

#053BasicAlgorithms4 min

Café customers give 1 to 5 stars each. Write one line: use accumulate to add up stars into total. The average is whole-number division, so the fraction is dropped; with no ratings it returns 0 (already in the starter).

Examples
average_stars({5, 4, 4})→4
average_stars({})→0
The functionint average_stars(const vector<int> &stars)
main.cpp
#include <numeric>
#include <vector>
using namespace std;

int average_stars(const vector<int> &stars) {
    if (stars.empty()) {
        return 0;
    }
    int total = 0;
    // your code here: work out total with accumulate
    return total / stars.size();
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.