Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(Fix wrong use of 'tee' in [[UnixPipes. Add UNIX Shell)
m (→‎{{header|UNIX Shell}}: Do not exit when outside of subshell.)
Line 1,393: Line 1,393:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
This example uses <tt>expr</tt>, which only works with integers. This example checks that each string in the list is an integer.
This example uses <tt>expr</tt>, so it only works with integers. It checks that each string in the list is an integer.


<lang bash>mean() {
<lang bash>mean() {
Line 1,407: Line 1,407:
shift
shift
done
done
expr $sum / $count) || exit $?
expr $sum / $count)
else
else
echo 0
echo 0
Line 1,413: Line 1,413:
}
}


printf "test 1: "; mean # 0
printf "test 1: "; mean # 0
printf "test 2: "; mean 300 # 300
printf "test 2: "; mean 300 # 300
printf "test 3: "; mean 300 100 400 # 266
printf "test 3: "; mean 300 100 400 # 266
printf "test 4: "; mean -400 200 # -100
printf "test 4: "; mean -400 400 -1300 200 # -275
printf "test 5: "; mean - # expr: syntax error</lang>
printf "test 5: "; mean - # expr: syntax error
printf "test 6: "; mean 1 2 A 3 # expr: non-numeric argument</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==