2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,11 @@
|
|||
Write a program to find the [[wp:arithmetic mean|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.
|
||||
{{task heading}}
|
||||
|
||||
See also: [[Median]], [[Mode]]
|
||||
Write a program to find the [[wp:arithmetic mean|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.
|
||||
|
||||
{{task heading|See also}}
|
||||
|
||||
{{Related tasks/Statistical measures}}
|
||||
|
||||
<br><hr>
|
||||
|
|
|
|||
|
|
@ -1,21 +1,17 @@
|
|||
# work around a gawk bug in the length extended use:
|
||||
# so this is a more non-gawk compliant way to get
|
||||
# how many elements are in an array
|
||||
function elength(v)
|
||||
{
|
||||
l=0
|
||||
for(el in v) l++
|
||||
return l
|
||||
}
|
||||
cat mean.awk
|
||||
#!/usr/local/bin/gawk -f
|
||||
|
||||
function mean(v)
|
||||
{
|
||||
if (elength(v) < 1) { return 0 }
|
||||
sum = 0
|
||||
for(i=0; i < elength(v); i++) {
|
||||
# User defined function
|
||||
function mean(v, i,n,sum) {
|
||||
for (i in v) {
|
||||
n++
|
||||
sum += v[i]
|
||||
}
|
||||
return sum/elength(v)
|
||||
if (n>0) {
|
||||
return(sum/n)
|
||||
} else {
|
||||
return("zero-length input !")
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
|
|
@ -24,4 +20,5 @@ BEGIN {
|
|||
vett[i] = rand()*10
|
||||
}
|
||||
print mean(vett)
|
||||
print mean(nothing)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
REM COLLECTION IN DATA STATEMENTS, EMPTY DATA IS THE END OF THE COLLECTION
|
||||
0 READ V$
|
||||
1 IF LEN(V$) = 0 THEN END
|
||||
2 N = 0
|
||||
3 S = 0
|
||||
4 FOR I = 0 TO 1 STEP 0
|
||||
5 S = S + VAL(V$)
|
||||
6 N = N + 1
|
||||
7 READ V$
|
||||
8 IF LEN(V$) THEN NEXT
|
||||
9 PRINT S / N
|
||||
10000 DATA1,2,2.718,3,3.142
|
||||
63999 DATA
|
||||
|
||||
REM COLLECTION IN AN ARRAY, ITEM 0 IS THE SIZE OF THE COLLECTION
|
||||
A(0) = 5 : A(1) = 1 : A(2) = 2 : A(3) = 2.718 : A(4) = 3 : A(5) = 3.142
|
||||
N = A(0) : IF N THEN S = 0 : FOR I = 1 TO N : S = S + A(I) : NEXT : ? S / N
|
||||
|
|
@ -1,12 +1 @@
|
|||
((main {
|
||||
(2 3 5 7 11 13 17 19 23)
|
||||
avg !
|
||||
%d nl <<})
|
||||
|
||||
(avg {
|
||||
dup
|
||||
<- sum ! ->
|
||||
len
|
||||
cudiv })
|
||||
|
||||
(sum { <- 0 -> {+} each }))
|
||||
(3 24 18 427 483 49 14 4294 2 41) dup len <- sum ! -> / itod <<
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
defmodule RC do
|
||||
def mean(list), do: Enum.sum(list) / Enum.count(list)
|
||||
defmodule Average do
|
||||
def mean(list), do: Enum.sum(list) / length(list)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
(sample => {
|
||||
|
||||
// mean :: [Num] => (Num | NaN)
|
||||
let mean = lst => {
|
||||
let lng = lst.length;
|
||||
|
||||
return lng ? (
|
||||
lst.reduce((a, b) => a + b, 0) / lng
|
||||
) : NaN;
|
||||
};
|
||||
|
||||
return mean(sample);
|
||||
|
||||
})([1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
|
@ -0,0 +1 @@
|
|||
5
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
val nums = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
|
||||
println("average = %f".format(nums.average()))
|
||||
|
|
@ -1,22 +1,27 @@
|
|||
/*REXX program finds the averages/arithmetic mean of several lists (vectors).*/
|
||||
/*REXX program finds the averages/arithmetic mean of several lists (vectors) or CL input*/
|
||||
parse arg @.1; if @.1='' then do; #=6 /*vector from the C.L.?*/
|
||||
@.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 = /*a null value. */
|
||||
do j=1 for 6
|
||||
say 'numbers = ' @.j; say "average = " avg(@.j); say copies('═',60)
|
||||
@.6 = /* ◄─── a null value. */
|
||||
end
|
||||
else #=1 /*number of CL vectors.*/
|
||||
do j=1 for #
|
||||
say ' numbers = ' @.j
|
||||
say ' average = ' avg(@.j)
|
||||
say copies('═', 79)
|
||||
end /*t*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
avg: procedure; parse arg x; #=words(x); $=0 /*#: number of items. */
|
||||
if #==0 then return 'N/A: ───[null vector.]' /*No words? Return N/A*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
avg: procedure; parse arg x; #=words(x) /*#: number of items.*/
|
||||
if #==0 then return 'N/A: ───[null vector.]' /*No words? Return N/A*/
|
||||
$=0
|
||||
do k=1 for #; _=word(x,k) /*obtain a number. */
|
||||
if datatype(_,'N') then do; $=$+_; iterate; end /*if numeric, then add*/
|
||||
say left('',40) "***error*** non-numeric: " _; #=#-1 /*error; adjust number*/
|
||||
end /*k*/
|
||||
|
||||
do k=1 for #; _=word(x,k) /*obtain a number.*/
|
||||
if datatype(_,'N') then do; $=$+_; iterate; end /*if numeric, add.*/
|
||||
say left('',20) "***error!*** non-numeric: " _; #=#-1 /*error; adjust #.*/
|
||||
end /*k*/
|
||||
|
||||
if #==0 then return 'N/A: ───[no numeric values.]' /*No nums? Return N/A.*/
|
||||
return $/max(1,#) /*return the average. */
|
||||
if #==0 then return 'N/A: ───[no numeric values.]' /*No nums? Return N/A*/
|
||||
return $ / # /*return the average. */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
1 2 3 5 7
|
||||
AMEAN
|
||||
<< DEPTH DUP 'N' STO ->LIST ΣLIST N / >>
|
||||
3.6
|
||||
Loading…
Add table
Add a link
Reference in a new issue