MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / arithmetic_mean

Function arithmetic_mean

Examples/Modules/Chapter 11/Ex11_07/math.cpp:13–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11namespace math::averages
12{
13 double arithmetic_mean(std::span<const double> data)
14 {
15 // The arithmetic mean, the most commonly used average,
16 // is defined as the sum of all elements divided by the number of elements.
17 double sum {};
18 for (auto value : data)
19 sum += value;
20
21 return data.empty()
22 ? std::numeric_limits<double>::quiet_NaN() // Or std::nan("")
23 : sum / data.size();
24 }
25}
26
27// Option 2: define in nested namespace blocks

Callers 1

mainFunction · 0.70

Calls 2

emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected