Your solution should work as shown in your example.
double total = 0;
// ...
averageScore = total / numPlayers; // Result of division is of type double.
Here you divide a doubleby int, and the result should be double. Thus, no cast is required.
Some other problems:
- You specified a parameter
scoreas int*, which allows the client to pass a null value (and you do not check this). numPlayers int, , std::size_t.averageScore , . .- const-correctness, ..
const, .
score int[N], . . :.
template <const std::size_t N>
double average(const int (&score)[N]) {
return static_cast<double>(std::accumulate(std::begin(score), std::end(score), 0)) / N;
}
:
int score[5] = { 2, 2, 3, 4, 5};
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << average(score) << std::endl;
score, . std::array.