Averages/Arithmetic mean: Difference between revisions

m
Spelling/grammar/aesthetics
(Added Java example.)
m (Spelling/grammar/aesthetics)
Line 1:
{{task}}
 
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}}==
Line 8 ⟶ 7:
mean=: +/ % #
 
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
Line 18 ⟶ 17:
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 is highly non-preferred compared to the version above.
is highly non-preferred compared to the version above.
 
mean1=: 3 : 0
Anonymous user