Task[edit]
Write a program to find the mean (arithmetic average) of a numeric vector.

In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.

See also
Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation






begin
    % procedure to find the mean of the elements of a vector.                %
    % As the procedure can't find the bounds of the array for itself,        %
    % we pass them in lb and ub          %
    real procedure mean ( real    array vector ( * )
                        ; integer value lb
                        ; integer value ub
                        ) ;
    begin
        real sum;
        assert( ub > lb ); % terminate the program if there are no elements  %
        sum := 0;
        for i := lb until ub do sum := sum + vector( i );
        sum / ( ( ub + 1 ) - lb )
    end mean ;
 
    % test the mean procedure by finding the mean of 1.1, 2.2, 3.3, 4.4, 5.5 %
    real array numbers ( 1 :: 5 );
    for i := 1 until 5 do numbers( i ) := i + ( i / 10 );
    r_format := "A"; r_w := 10; r_d := 2; % set fixed point output           %
    write( mean( numbers, 1, 5 ) );
end.