Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(Added Java example.)
m (Spelling/grammar/aesthetics)
Line 1: Line 1:
{{task}}
{{task}}


Write a program to find the mean (arithmetic average) of a numeric vector.
Write a program to find the mean (arithmetic average) of a numeric vector. The program should work on a zero-length vector (with an answer of 0).
The program should work on a zero-length vector (with an answer of 0).


=={{header|J}}==
=={{header|J}}==
Line 8: Line 7:
mean=: +/ % #
mean=: +/ % #


That is, sum divided by the number of items. The verb also works on higher-ranked arrays. For example:
That is, sum divided by the number of items. The verb also works on higher-ranked arrays. For example:


mean 3 1 4 1 5 9
mean 3 1 4 1 5 9
Line 18: Line 17:
0.58243 0.402948 0.477066 0.511155
0.58243 0.402948 0.477066 0.511155


The computation can also be written as a loop. It is shown here for comparison only and
The computation can also be written as a loop. It is shown here for comparison only and is highly non-preferred compared to the version above.
is highly non-preferred compared to the version above.


mean1=: 3 : 0
mean1=: 3 : 0