Averages/Arithmetic mean: Difference between revisions

→‎{{header|REXX}}: changed the method of dealing with a null vector and also a vector with non-numeric values. -- ~~~~
(→‎{{header|Python}}: Update examples to better show the effect of fsum)
(→‎{{header|REXX}}: changed the method of dealing with a null vector and also a vector with non-numeric values. -- ~~~~)
Line 1,790:
The vectors (list) can contain any valid numbers.
<lang rexx>/*REXX pgm finds the averages/arithmetic mean of several lists (vectors)*/
/*REXX pgm finds the averages/arithmetic mean of several lists (vectors)*/
@.1 = 10 9 8 7 6 5 4 3 2 1
@.2 = 10 9 8 7 6 5 4 3 2 1 0 0 0 0 .11
@.3 = '10 20 30 40 50 -100 4.7 -11e2'
@.4 = '1 2 3 4 five 6 7 8 9 10.1. ±2'
@.5 = 'World War I & World War II'
@.6 = ''
do j=1 for 56
say 'numbers = ' @.j; say 'average = ' avg(@.j); say
end /*t*/
Line 1,802:
/*──────────────────────────────────AVG subroutine──────────────────────*/
avg: procedure; parse arg x; w=words(x); s=0; $=left('',20)
if w==0 then return 'N/A: ───[null vector.]'
do k=1 for w; _=word(x,k)
if datatype(_,'N') then do; s=s+_; iterate; end
say $ '***error!*** non-numeric: ' _; w=w-1 /*adjust W*/
end /*k*/
if w==0 then return 'N/A: ───[no numeric values.]'
return s/max(1,w)</lang>
'''output'''
Line 1,823 ⟶ 1,825:
***error!*** non-numeric: ±2
average = 5
 
numbers = World War I & World War II
***error!*** non-numeric: World
***error!*** non-numeric: War
***error!*** non-numeric: I
***error!*** non-numeric: &
***error!*** non-numeric: World
***error!*** non-numeric: War
***error!*** non-numeric: II
average = N/A: ───[no numeric values.]
 
numbers =
average = 0N/A: ───[null vector.]
</pre>