Averages/Arithmetic mean: Difference between revisions

m
(10 intermediate revisions by 8 users not shown)
Line 329:
=={{header|APL}}==
{{works with|APL2}}
<syntaxhighlight lang="apl"> X←3 1 4 1 5 9
X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</syntaxhighlight>
end</syntaxhighlight>
 
{{works with|langur|0.6.6Dyalog APL}}
A proper function definition:
<syntaxhighlight lang="rpl/2apl">1 2 3 5 7
Avg←{(+⌿⍵)÷≢⍵}
Avg 1 2 3 4 5 6
3.5
end</syntaxhighlight>
 
Using [[tacit programming]]:
3.6</syntaxhighlight lang="apl">
Avg← +⌿÷≢
Avg 1 2 3 4 5 6
3.5
</syntaxhighlight>
'''N.B.:''' the symbol for [https://aplwiki.com/wiki/Tally Tally (≢)] doesn't display correctly on Chrome-based browsers at the moment.
 
=={{header|AppleScript}}==
Line 859 ⟶ 877:
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">'versiondim a[3, 1:, using4, 1, an5, array9]
 
dim a[3, 1, 4, 1, 5, 9]
 
arraysize s, a
Line 871 ⟶ 887:
next i
 
print t / s</syntaxhighlight>
{{out| Output}}<pre>3.83</pre>
 
end</syntaxhighlight>
 
<syntaxhighlight lang="basic">'version 2: without using an array
 
input "how many numbers to average? ", n
 
for i = 1 to n
 
input "enter a number: ", s
let a = a + s
 
next i
 
print "average: ", a / n
 
end</syntaxhighlight>
 
=={{header|Crystal}}==
Line 1,045:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">func mean . f[] r .
proc mean . f[] r .
for i = 1 to len f[]
for si += 1 to len f[i]
r = s / lens += f[i]
.
r = s / len f[]
for ir = 1s to/ len f[]
.
f[] = [ 1 2 3 4 5 6 7 8 ]
call mean f[] r
print r
</syntaxhighlight>
Line 1,233 ⟶ 1,234:
 
=={{header|Elena}}==
ELENA 56.0x:
<syntaxhighlight lang="elena">import extensions;
 
Line 1,247 ⟶ 1,248:
while (enumerator.next())
{
sum += *enumerator.get();
count += 1;
};
Line 1,980 ⟶ 1,981:
We could use fold() to write a function that takes an array and calculates the mean.
 
<syntaxhighlight lang="langur">val .mean = ffn(.x) { fold(ffn{+}, .x) / len(.x) }
{{works with|langur|0.6.6}}
<syntaxhighlight lang="langur">val .mean = f(.x) fold(f{+}, .x) / len(.x)
 
writeln " custom: ", .mean([7, 3, 12])
Line 3,392:
 
=={{header|RPL}}==
This is based on the dc version above.
This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.
{{works with|HP|48G}}
≪ DUP 'N' STO →LIST ΣLIST N / 'N' PURGE ≫ '<span style="color:blue">AMEAN</span>' STO
or,by using the stack instead of a temporary variable:
≪ →LIST ΣLIST LASTARG SIZE / ≫ '<span style="color:blue">AMEAN</span>' STO
 
CLEAR 1 2 3 5 7 DEPTH <span style="color:blue">AMEAN</span>
<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< CLEAR DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>
 
===Hard-working approach===
Line 3,576 ⟶ 3,577:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func avg(Array list) {
list.len > 0 || return 0;
list.sum / list.len;
}
 
say avg([Math.infInf, Math.infInf]);
say avg([3,1,4,1,5,9]);
say avg([1e+20, 3, 1, 4, 1, 5, 9, -1e+20]);
say avg([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0.11]);
say avg([10, 20, 30, 40, 50, -100, 4.7, -1100]);</syntaxhighlight>
{{out}}
<pre>inf
Inf
3.833333333333333333333333333333333333333
3.83333333333333333333333333333333333333333333333
2.875
3.674
-130.6625</pre>
</pre>
 
=={{header|Slate}}==
Line 4,113 ⟶ 4,116:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
885

edits