Averages/Arithmetic mean
This implementation uses Zen C's standard library Vec type. To handle the edge case of an empty vector, the function checks if the vector is empty and returns 0.0 to avoid division by zero.
import "std/vec.zc"
fn mean(v: Vec<f64>*) -> f64 {
if v.is_empty() {
return 0.0;
}
let sum: f64 = 0.0;
for x in *v {
sum += x;
}
return sum / (f64)v.length();
}
fn main() {
let numbers = Vec<f64>::new();
numbers.push(10.0);
numbers.push(20.0);
numbers.push(30.0);
numbers.push(40.0);
let m = mean(&numbers);
println "Numbers: 10, 20, 30, 40";
println "Mean: ";
let empty = Vec<f64>::new();
let m_empty = mean(&empty);
println "Empty mean: ";
}
Output:
Numbers: 10, 20, 30, 40
Mean: 25.000000
Empty mean: 0.000000
Attribution: This is a community solution for the Rosetta Code task Averages/Arithmetic mean in Zen C.
This article uses material from the Rosetta Code article Averages/Arithmetic mean, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.