Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Averages-Arithmetic-mean/00-META.yaml
Normal file
3
Task/Averages-Arithmetic-mean/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Averages/Arithmetic_mean
|
||||
note: Probability and statistics
|
||||
12
Task/Averages-Arithmetic-mean/00-TASK.txt
Normal file
12
Task/Averages-Arithmetic-mean/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;Task
|
||||
|
||||
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>
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
{x{+=<:2:x/%<:d:~$<:01:~><:02:~><:03:~><:04:~><:05:~><:06:~><:07:~><:08:
|
||||
~><:09:~><:0a:~><:0b:~><:0c:~><:0d:~><:0e:~><:0f:~><:10:~><:11:~><:12:~>
|
||||
<:13:~><:14:~><:15:~><:16:~><:17:~><:18:~><:19:~><:ffffffffffffffff:~>{x
|
||||
{+>}:8f:{&={+>{~>&=x<:ffffffffffffffff:/#:8f:{{=<:19:x/%
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
F average(x)
|
||||
R sum(x) / Float(x.len)
|
||||
|
||||
print(average([0, 0, 3, 1, 4, 1, 5, 9, 0, 0]))
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
AVGP CSECT
|
||||
USING AVGP,12
|
||||
LR 12,15
|
||||
SR 3,3 i=0
|
||||
SR 6,6 sum=0
|
||||
LOOP CH 3,=AL2(NN-T-1) for i=1 to nn
|
||||
BH ENDLOOP
|
||||
L 2,T(3) t(i)
|
||||
MH 2,=H'100' scaling factor=2
|
||||
AR 6,2 sum=sum+t(i)
|
||||
LA 3,4(3) next i
|
||||
B LOOP
|
||||
ENDLOOP LR 5,6 sum
|
||||
LA 4,0
|
||||
D 4,NN sum/nn
|
||||
XDECO 5,Z edit binary
|
||||
MVC U,Z+10 descale
|
||||
MVI Z+10,C'.'
|
||||
MVC Z+11(2),U
|
||||
XPRNT Z,80 output
|
||||
XR 15,15
|
||||
BR 14
|
||||
T DC F'10',F'9',F'8',F'7',F'6',F'5',F'4',F'3',F'2',F'1'
|
||||
NN DC A((NN-T)/4)
|
||||
Z DC CL80' '
|
||||
U DS CL2
|
||||
END AVGP
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
ArithmeticMean: PHA
|
||||
TYA
|
||||
PHA ;push accumulator and Y register onto stack
|
||||
|
||||
|
||||
LDA #0
|
||||
STA Temp
|
||||
STA Temp+1 ;temporary 16-bit storage for total
|
||||
|
||||
LDY NumberInts
|
||||
BEQ Done ;if NumberInts = 0 then return an average of zero
|
||||
|
||||
DEY ;start with NumberInts-1
|
||||
AddLoop: LDA (ArrayPtr),Y
|
||||
CLC
|
||||
ADC Temp
|
||||
STA Temp
|
||||
LDA Temp+1
|
||||
ADC #0
|
||||
STA Temp+1
|
||||
DEY
|
||||
CPY #255
|
||||
BNE AddLoop
|
||||
|
||||
LDY #-1
|
||||
DivideLoop: LDA Temp
|
||||
SEC
|
||||
SBC NumberInts
|
||||
STA Temp
|
||||
LDA Temp+1
|
||||
SBC #0
|
||||
STA Temp+1
|
||||
INY
|
||||
BCS DivideLoop
|
||||
|
||||
Done: STY ArithMean ;store result here
|
||||
PLA ;restore accumulator and Y register from stack
|
||||
TAY
|
||||
PLA
|
||||
RTS ;return from routine
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: avg \ a -- avg(a)
|
||||
dup ' n:+ 0 a:reduce
|
||||
swap a:len nip n:/ ;
|
||||
|
||||
\ test:
|
||||
[ 1.0, 2.3, 1.1, 5.0, 3, 2.8, 2.01, 3.14159 ] avg . cr
|
||||
[ ] avg . cr
|
||||
[ 10 ] avg . cr
|
||||
bye
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(defun mean-r (xs)
|
||||
(if (endp xs)
|
||||
(mv 0 0)
|
||||
(mv-let (m j)
|
||||
(mean-r (rest xs))
|
||||
(mv (+ (first xs) m) (+ j 1)))))
|
||||
|
||||
(defun mean (xs)
|
||||
(if (endp xs)
|
||||
0
|
||||
(mv-let (n d)
|
||||
(mean-r xs)
|
||||
(/ n d))))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
PROC mean = (REF[]REAL p)REAL:
|
||||
# Calculates the mean of qty REALs beginning at p. #
|
||||
IF LWB p > UPB p THEN 0.0
|
||||
ELSE
|
||||
REAL total := 0.0;
|
||||
FOR i FROM LWB p TO UPB p DO total +:= p[i] OD;
|
||||
total / (UPB p - LWB p + 1)
|
||||
FI;
|
||||
|
||||
main:(
|
||||
[6]REAL test := (1.0, 2.0, 5.0, -5.0, 9.5, 3.14159);
|
||||
print((mean(test),new line))
|
||||
)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
begin
|
||||
% procedure to find the mean of the elements of a vector. %
|
||||
% As the procedure can't find the bounds of the array for itself, %
|
||||
% we pass them in lb and ub %
|
||||
real procedure mean ( real array vector ( * )
|
||||
; integer value lb
|
||||
; integer value ub
|
||||
) ;
|
||||
begin
|
||||
real sum;
|
||||
assert( ub > lb ); % terminate the program if there are no elements %
|
||||
sum := 0;
|
||||
for i := lb until ub do sum := sum + vector( i );
|
||||
sum / ( ( ub + 1 ) - lb )
|
||||
end mean ;
|
||||
|
||||
% test the mean procedure by finding the mean of 1.1, 2.2, 3.3, 4.4, 5.5 %
|
||||
real array numbers ( 1 :: 5 );
|
||||
for i := 1 until 5 do numbers( i ) := i + ( i / 10 );
|
||||
r_format := "A"; r_w := 10; r_d := 2; % set fixed point output %
|
||||
write( mean( numbers, 1, 5 ) );
|
||||
end.
|
||||
|
|
@ -0,0 +1 @@
|
|||
avg[list]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
X←3 1 4 1 5 9
|
||||
(+/X)÷⍴X
|
||||
3.833333333
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
cat mean.awk
|
||||
#!/usr/local/bin/gawk -f
|
||||
|
||||
# User defined function
|
||||
function mean(v, i,n,sum) {
|
||||
for (i in v) {
|
||||
n++
|
||||
sum += v[i]
|
||||
}
|
||||
if (n>0) {
|
||||
return(sum/n)
|
||||
} else {
|
||||
return("zero-length input !")
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
# fill a vector with random numbers
|
||||
for(i=0; i < 10; i++) {
|
||||
vett[i] = rand()*10
|
||||
}
|
||||
print mean(vett)
|
||||
print mean(nothing)
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
|
||||
|
||||
PROC Mean(INT ARRAY a INT count REAL POINTER result)
|
||||
INT i
|
||||
REAL x,sum,tmp
|
||||
|
||||
IntToReal(0,sum)
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
IntToReal(a(i),x)
|
||||
RealAdd(sum,x,tmp)
|
||||
RealAssign(tmp,sum)
|
||||
OD
|
||||
IntToReal(count,tmp)
|
||||
RealDiv(sum,tmp,result)
|
||||
RETURN
|
||||
|
||||
PROC Test(INT ARRAY a INT count)
|
||||
INT i
|
||||
REAL result
|
||||
|
||||
Mean(a,count,result)
|
||||
Print("mean(")
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
PrintI(a(i))
|
||||
IF i<count-1 THEN
|
||||
Put(',)
|
||||
FI
|
||||
OD
|
||||
Print(")=")
|
||||
PrintRE(result)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
INT ARRAY a1=[1 2 3 4 5 6]
|
||||
INT ARRAY a2=[1 10 100 1000 10000]
|
||||
INT ARRAY a3=[9]
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
Test(a1,6)
|
||||
Test(a2,5)
|
||||
Test(a3,1)
|
||||
Test(a3,0)
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function mean(vector:Vector.<Number>):Number
|
||||
{
|
||||
var sum:Number = 0;
|
||||
for(var i:uint = 0; i < vector.length; i++)
|
||||
sum += vector[i];
|
||||
return vector.length == 0 ? 0 : sum / vector.length;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Mean_Main is
|
||||
type Vector is array (Positive range <>) of Float;
|
||||
function Mean (Item : Vector) return float with pre => Item'length > 0;
|
||||
function Mean (Item : Vector) return Float is
|
||||
Sum : Float := 0.0;
|
||||
begin
|
||||
for I in Item'range loop
|
||||
Sum := Sum + Item(I);
|
||||
end loop;
|
||||
return Sum / Float(Item'Length);
|
||||
end Mean;
|
||||
A : Vector := (3.0, 1.0, 4.0, 1.0, 5.0, 9.0);
|
||||
begin
|
||||
Put(Item => Mean (A), Fore => 1, Exp => 0);
|
||||
New_Line;
|
||||
-- test for zero length vector
|
||||
Put(Item => Mean(A (1..0)), Fore => 1, Exp => 0);
|
||||
New_Line;
|
||||
end Mean_Main;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
real
|
||||
mean(list l)
|
||||
{
|
||||
real sum, x;
|
||||
|
||||
sum = 0;
|
||||
for (, x in l) {
|
||||
sum += x;
|
||||
}
|
||||
|
||||
sum / ~l;
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
o_form("%f\n", mean(list(4.5, 7.25, 5r, 5.75)));
|
||||
|
||||
0;
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
PROC mean(l:PTR TO LONG)
|
||||
DEF m, i, ll
|
||||
ll := ListLen(l)
|
||||
IF ll = 0 THEN RETURN 0.0
|
||||
m := 0.0
|
||||
FOR i := 0 TO ll-1 DO m := !m + l[i]
|
||||
m := !m / (ll!)
|
||||
ENDPROC m
|
||||
|
||||
PROC main()
|
||||
DEF s[20] : STRING
|
||||
WriteF('mean \s\n',
|
||||
RealF(s,mean([1.0, 2.0, 3.0, 4.0, 5.0]), 2))
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
on average(listOfNumbers)
|
||||
set len to (count listOfNumbers)
|
||||
if (len is 0) then return missing value
|
||||
|
||||
set sum to 0
|
||||
repeat with thisNumber in listOfNumbers
|
||||
set sum to sum + thisNumber
|
||||
end repeat
|
||||
|
||||
return sum / len
|
||||
end average
|
||||
|
||||
average({2500, 2700, 2400, 2300, 2550, 2650, 2750, 2450, 2600, 2400})
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
|
||||
use framework "Foundation"
|
||||
|
||||
on average(listOfNumbers)
|
||||
if ((count listOfNumbers) is 0) then return missing value
|
||||
|
||||
set arrayOfNumbers to current application's class "NSArray"'s arrayWithArray:(listOfNumbers)
|
||||
return (arrayOfNumbers's valueForKeyPath:("@avg.self")) as real
|
||||
end average
|
||||
|
||||
average({2500, 2700, 2400, 2300, 2550, 2650, 2750, 2450, 2600, 2400})
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
arr: [1 2 3 4 5 6 7]
|
||||
|
||||
print average arr
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
mean([1, 2, 3])
|
||||
mean(1..10)
|
||||
mean([])
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
i = 10
|
||||
Loop, % i {
|
||||
Random, v, -3.141592, 3.141592
|
||||
list .= v "`n"
|
||||
sum += v
|
||||
}
|
||||
MsgBox, % i ? list "`nmean: " sum/i:0
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
mean = 0
|
||||
sum = 0;
|
||||
FOR i = LBOUND(nums) TO UBOUND(nums)
|
||||
sum = sum + nums(i);
|
||||
NEXT i
|
||||
size = UBOUND(nums) - LBOUND(nums) + 1
|
||||
PRINT "The mean is: ";
|
||||
IF size <> 0 THEN
|
||||
PRINT (sum / size)
|
||||
ELSE
|
||||
PRINT 0
|
||||
END IF
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
REM specific functions for the array/vector types
|
||||
|
||||
REM Byte Array
|
||||
DEF FN_Mean_Arithmetic&(n&())
|
||||
= SUM(n&()) / (DIM(n&(),1)+1)
|
||||
|
||||
REM Integer Array
|
||||
DEF FN_Mean_Arithmetic%(n%())
|
||||
= SUM(n%()) / (DIM(n%(),1)+1)
|
||||
|
||||
REM Float 40 array
|
||||
DEF FN_Mean_Arithmetic(n())
|
||||
= SUM(n()) / (DIM(n(),1)+1)
|
||||
|
||||
REM A String array
|
||||
DEF FN_Mean_Arithmetic$(n$())
|
||||
LOCAL I%, S%, sum, Q%
|
||||
S% = DIM(n$(),1)
|
||||
FOR I% = 0 TO S%
|
||||
Q% = TRUE
|
||||
ON ERROR LOCAL Q% = FALSE
|
||||
IF Q% sum += EVAL(n$(I%))
|
||||
NEXT
|
||||
= sum / (S%+1)
|
||||
|
||||
REM Float 64 array
|
||||
DEF FN_Mean_Arithmetic#(n#())
|
||||
= SUM(n#()) / (DIM(n#(),1)+1)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Avg ← +´÷≠
|
||||
|
||||
Avg 1‿2‿3‿4
|
||||
|
|
@ -0,0 +1 @@
|
|||
2.5
|
||||
|
|
@ -0,0 +1 @@
|
|||
(3 24 18 427 483 49 14 4294 2 41) dup len <- sum ! -> / itod <<
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
define m(a[], n) {
|
||||
auto i, s
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
s += a[i]
|
||||
}
|
||||
return(s / n)
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
&:0\:!v!:-1<
|
||||
@./\$_\&+\^
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
:mean(vec)
|
||||
vec.fold_left(0, (x, y -> x + y)) / vec.length()
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
(mean1=
|
||||
sum length n
|
||||
. 0:?sum:?length
|
||||
& whl
|
||||
' ( !arg:%?n ?arg
|
||||
& 1+!length:?length
|
||||
& !n+!sum:?sum
|
||||
)
|
||||
& !sum*!length^-1
|
||||
);
|
||||
|
||||
(mean2=
|
||||
sum length n
|
||||
. 0:?sum:?length
|
||||
& !arg
|
||||
: ?
|
||||
( #%@?n
|
||||
& 1+!length:?length
|
||||
& !n+!sum:?sum
|
||||
& ~
|
||||
)
|
||||
?
|
||||
| !sum*!length^-1
|
||||
);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
( :?test
|
||||
& 1000000:?Length
|
||||
& whl'(!Length+-1:?Length:>0&!Length !test:?test)
|
||||
& out$mean1$!test
|
||||
& out$mean2$!test
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
mean = { list |
|
||||
true? list.empty?, 0, { list.reduce(0, :+) / list.length }
|
||||
}
|
||||
|
||||
p mean 1.to 10 #Prints 5.5
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
blsq ) {1 2 2.718 3 3.142}av
|
||||
2.372
|
||||
blsq ) {}av
|
||||
NaN
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include <vector>
|
||||
|
||||
double mean(const std::vector<double>& numbers)
|
||||
{
|
||||
if (numbers.size() == 0)
|
||||
return 0;
|
||||
|
||||
double sum = 0;
|
||||
for (std::vector<double>::iterator i = numbers.begin(); i != numbers.end(); i++)
|
||||
sum += *i;
|
||||
return sum / numbers.size();
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
double mean(const std::vector<double>& numbers)
|
||||
{
|
||||
if (numbers.empty())
|
||||
return 0;
|
||||
return std::accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
template <typename Iterator>
|
||||
double mean(Iterator begin, Iterator end)
|
||||
{
|
||||
if (begin == end)
|
||||
return 0;
|
||||
return std::accumulate(begin, end, 0.0) / std::distance(begin, end);
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(new[] { 1, 2, 3 }.Average());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
double average = 0;
|
||||
|
||||
double[] numArray = { 1, 2, 3, 4, 5 };
|
||||
average = Average(numArray);
|
||||
|
||||
Console.WriteLine(average); // Output is 3
|
||||
|
||||
// Alternative use
|
||||
average = Average(1, 2, 3, 4, 5);
|
||||
|
||||
Console.WriteLine(average); // Output is still 3
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static double Average(params double[] nums)
|
||||
{
|
||||
double d = 0;
|
||||
|
||||
foreach (double num in nums)
|
||||
d += num;
|
||||
return d / nums.Length;
|
||||
}
|
||||
}
|
||||
24
Task/Averages-Arithmetic-mean/C/averages-arithmetic-mean.c
Normal file
24
Task/Averages-Arithmetic-mean/C/averages-arithmetic-mean.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
|
||||
double mean(double *v, int len)
|
||||
{
|
||||
double sum = 0;
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
sum += v[i];
|
||||
return sum / len;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double v[] = {1, 2, 2.718, 3, 3.142};
|
||||
int i, len;
|
||||
for (len = 5; len >= 0; len--) {
|
||||
printf("mean[");
|
||||
for (i = 0; i < len; i++)
|
||||
printf(i ? ", %g" : "%g", v[i]);
|
||||
printf("] = %g\n", mean(v, len));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
FUNCTION MEAN(some-table (ALL))
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. find-mean.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i PIC 9(4).
|
||||
|
||||
01 summ USAGE FLOAT-LONG.
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 nums-area.
|
||||
03 nums-len PIC 9(4).
|
||||
03 nums USAGE FLOAT-LONG
|
||||
OCCURS 0 TO 1000 TIMES
|
||||
DEPENDING ON nums-len.
|
||||
|
||||
01 result USAGE FLOAT-LONG.
|
||||
|
||||
PROCEDURE DIVISION USING nums-area, result.
|
||||
IF nums-len = 0
|
||||
MOVE 0 TO result
|
||||
GOBACK
|
||||
END-IF
|
||||
|
||||
DIVIDE FUNCTION SUM(nums (ALL)) BY nums-len GIVING result
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
Mean.
|
||||
|
||||
Chef has no way to detect EOF, so rather than interpreting
|
||||
some arbitrary number as meaning "end of input", this program
|
||||
expects the first input to be the sample size. Pass in the samples
|
||||
themselves as the other inputs. For example, if you wanted to
|
||||
compute the mean of 10, 100, 47, you could pass in 3, 10, 100, and
|
||||
47. To test the "zero-length vector" case, you need to pass in 0.
|
||||
|
||||
Ingredients.
|
||||
0 g Sample Size
|
||||
0 g Counter
|
||||
0 g Current Sample
|
||||
|
||||
Method.
|
||||
Take Sample Size from refrigerator.
|
||||
Put Sample Size into mixing bowl.
|
||||
Fold Counter into mixing bowl.
|
||||
Put Current Sample into mixing bowl.
|
||||
Loop Counter.
|
||||
Take Current Sample from refrigerator.
|
||||
Add Current Sample into mixing bowl.
|
||||
Endloop Counter until looped.
|
||||
If Sample Size.
|
||||
Divide Sample Size into mixing bowl.
|
||||
Put Counter into 2nd mixing bowl.
|
||||
Fold Sample Size into 2nd mixing bowl.
|
||||
Endif until ifed.
|
||||
Pour contents of mixing bowl into baking dish.
|
||||
|
||||
Serves 1.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defn mean [sq]
|
||||
(if (empty? sq)
|
||||
0
|
||||
(/ (reduce + sq) (count sq))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defn mean [sq]
|
||||
(if (empty? sq)
|
||||
0
|
||||
(float (/ (reduce + sq) (count sq)))))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class Rosetta
|
||||
def mean(ns as List<of number>) as number
|
||||
if ns.count == 0
|
||||
return 0
|
||||
else
|
||||
sum = 0.0
|
||||
for n in ns
|
||||
sum += n
|
||||
return sum / ns.count
|
||||
|
||||
def main
|
||||
print "mean of [[]] is [.mean(List<of number>())]"
|
||||
print "mean of [[1,2,3,4]] is [.mean([1.0,2.0,3.0,4.0])]"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
mean = (array) ->
|
||||
return 0 if array.length is 0
|
||||
sum = array.reduce (s,i,0) -> s += i
|
||||
sum / array.length
|
||||
|
||||
|
||||
alert mean [1]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defun mean (&rest sequence)
|
||||
(when sequence
|
||||
(/ (reduce #'+ sequence) (length sequence))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun mean (list)
|
||||
(when list
|
||||
(/ (loop for i in list sum i)
|
||||
(length list))))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
dim a[3, 1, 4, 1, 5, 9]
|
||||
|
||||
arraysize s, a
|
||||
|
||||
for i = 0 to s - 1
|
||||
|
||||
let t = t + a[i]
|
||||
|
||||
next i
|
||||
|
||||
print t / s
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# Crystal will return NaN if an empty array is passed
|
||||
def mean(arr) : Float64
|
||||
arr.sum / arr.size.to_f
|
||||
end
|
||||
23
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-1.d
Normal file
23
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-1.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
real mean(Range)(Range r) pure nothrow @nogc {
|
||||
real sum = 0.0;
|
||||
int count;
|
||||
|
||||
foreach (item; r) {
|
||||
sum += item;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
return 0.0;
|
||||
else
|
||||
return sum / count;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
int[] data;
|
||||
writeln("Mean: ", data.mean);
|
||||
data = [3, 1, 4, 1, 5, 9];
|
||||
writeln("Mean: ", data.mean);
|
||||
}
|
||||
10
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-2.d
Normal file
10
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-2.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
real mean(Range)(Range r) pure nothrow @nogc {
|
||||
return r.sum / max(1.0L, r.count);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("Mean: ", (int[]).init.mean);
|
||||
writeln("Mean: ", [3, 1, 4, 1, 5, 9].mean);
|
||||
}
|
||||
14
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-3.d
Normal file
14
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-3.d
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import std.stdio, std.conv, std.algorithm, std.math, std.traits;
|
||||
|
||||
CommonType!(T, real) mean(T)(T[] n ...) if (isNumeric!T) {
|
||||
alias E = CommonType!(T, real);
|
||||
auto num = n.dup;
|
||||
num.schwartzSort!(abs, "a > b");
|
||||
return num.map!(to!E).sum(0.0L) / max(1, num.length);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writefln("%8.5f", mean((int[]).init));
|
||||
writefln("%8.5f", mean( 0, 3, 1, 4, 1, 5, 9, 0));
|
||||
writefln("%8.5f", mean([-1e20, 3, 1, 4, 1, 5, 9, 1e20]));
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
num mean(List<num> l) => l.reduce((num p, num n) => p + n) / l.length;
|
||||
|
||||
void main(){
|
||||
print(mean([1,2,3,4,5,6,7]));
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
1 2 3 5 7 zsn1k[+z1<+]ds+xln/p
|
||||
3.6
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
[[Nada Mean: ]Ppq]sq
|
||||
zd0=qsn [stack length = n]sz
|
||||
1k [precision can be altered]sz
|
||||
[+z1<+]ds+x[Sum: ]Pp
|
||||
ln/[Mean: ]Pp
|
||||
[Sample size: ]Plnp
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
$ dc sample.dc amean.cd
|
||||
Sum: 18
|
||||
Mean: 3.6
|
||||
Sample size: 5
|
||||
$
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
program AveragesArithmeticMean;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses Types;
|
||||
|
||||
function ArithmeticMean(aArray: TDoubleDynArray): Double;
|
||||
var
|
||||
lValue: Double;
|
||||
begin
|
||||
Result := 0;
|
||||
|
||||
for lValue in aArray do
|
||||
Result := Result + lValue;
|
||||
if Result > 0 then
|
||||
Result := Result / Length(aArray);
|
||||
end;
|
||||
|
||||
begin
|
||||
Writeln(Mean(TDoubleDynArray.Create()));
|
||||
Writeln(Mean(TDoubleDynArray.Create(1,2,3,4,5)));
|
||||
end.
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
func avg(args...) {
|
||||
var acc = .0
|
||||
var len = 0
|
||||
for x in args {
|
||||
len += 1
|
||||
acc += x
|
||||
}
|
||||
acc / len
|
||||
}
|
||||
|
||||
avg(1, 2, 3, 4, 5, 6)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def meanOrZero(numbers) {
|
||||
var count := 0
|
||||
var sum := 0
|
||||
for x in numbers {
|
||||
sum += x
|
||||
count += 1
|
||||
}
|
||||
return sum / 1.max(count)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
AveVal(SET OF INTEGER s) := AVE(s);
|
||||
|
||||
//example usage
|
||||
|
||||
SetVals := [14,9,16,20,91];
|
||||
AveVal(SetVals) //returns 30.0 ;
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
[Averages/Arithmetic mean - Rosetta Code]
|
||||
|
||||
[EDSAC program (Initial Orders 2) to find and print the average of
|
||||
a sequence of 35-bit fractional values.
|
||||
Values are read from tape, preceded by an integer count.]
|
||||
|
||||
[Library subroutine M3, runs at load time and is then overwritten.
|
||||
Prints header; here, last character sets teleprinter to figures.]
|
||||
PF GK IF AF RD LF UF OF E@ A6F G@ E8F EZ PF
|
||||
*!!!!!COUNT!!!!!!AVERAGE@&#.. [PZ]
|
||||
|
||||
[Main routine: must be at even address]
|
||||
T214K GK
|
||||
[0] PF PF [average value]
|
||||
[2] PF PF [reciprocal of data count]
|
||||
[4] PF [data count]
|
||||
[5] PD [17-bit constant 1; also serves as '0' for printing]
|
||||
[6] @F [carriage return]
|
||||
[7] &F [line feed]
|
||||
[8] !F [space]
|
||||
[9] MF [dot (in figures mode)]
|
||||
[10] K4096F [teleprinter null]
|
||||
[Entry and outer loop]
|
||||
[11] A11@
|
||||
G56F [call library subroutine R4, sets 0D := data count N]
|
||||
SD E64@ [exit if N = 0]
|
||||
T4F [clear acc]
|
||||
AF T4@ [load and save N (assumed < 2^16)]
|
||||
[18] A18@ G156F [print N (clears acc)]
|
||||
TD [clear whole of 0D, including sandwich bit]
|
||||
T4D [same for 4D]
|
||||
A4@ S2F [acc := N - 2]
|
||||
G66@ [jump to special action if N = 1]
|
||||
A2F [restore N after test]
|
||||
T5F [store N in 4D high word]
|
||||
A5@ T1F [store 1 in 0D high word]
|
||||
[29] A29@ G120F [call library subroutine D6, sets 0D := 0D/4D]
|
||||
AD T2#@ [load and save 1/N]
|
||||
T#@ [clear average]
|
||||
S4@ [load -N]
|
||||
[Inner loop]
|
||||
[35] T4@ [update negative loop counter]
|
||||
[36] A36@ G78F [read next datum to 0D (clears acc)]
|
||||
H2#@ [mult reg := 1/N]
|
||||
VD [acc := datum/N]
|
||||
A#@ T#@ [add into average]
|
||||
A4@ A5@ [increment negative loop counter]
|
||||
G35@ [loop until counter = 0]
|
||||
[45] O8@ O8@ [print 2 spaces]
|
||||
[Print the average value.
|
||||
NB: Library subroutine P1 requires non-negative input and prints only the
|
||||
digits after the decimal point. Formatting has to be done by the caller.]
|
||||
[47] A#@ [load average (order also serves as minus sign)]
|
||||
G52@ [jump if average < 0]
|
||||
TD [pass average to subroutine P1]
|
||||
O65@ [print plus sign (or could be space)]
|
||||
E56@ [join common code]
|
||||
[52] TD [average < 0; clear acc]
|
||||
S#@ TD [pass abs(average) to subroutine P1]
|
||||
O47@ [print minus sign]
|
||||
[56] O5@ O9@ [common code: print '0.']
|
||||
[58] A58@ G192F [call P1 to print abs(average)]
|
||||
P8F [8 decimal places]
|
||||
O6@ O7@ [print CR, LF]
|
||||
E11@ [loop back always (because acc = 0)]
|
||||
[Jump to here if data count = 0, means end of data]
|
||||
[64] O10@ [print null to flush teleprinter buffer]
|
||||
[65] ZF [halt the machine (order also serves as plus sign)]
|
||||
[Jump to here if data count = 1]
|
||||
[66] TF [clear acc]
|
||||
[67] A67@ G78F [read datum to 0D]
|
||||
AD T#@ [average := datum]
|
||||
E45@ [jump to print the average]
|
||||
|
||||
[The following puts the entry address into location 50,
|
||||
so that it can be accessed via the X parameter (see end of program).
|
||||
This is done in case the data is input from a separate tape.]
|
||||
T50K P11@ T11Z
|
||||
|
||||
[Library subroutine R4.
|
||||
Input of one signed integer, returned in 0D.]
|
||||
T56K
|
||||
GKA3FT21@T4DH6@E11@P5DJFT6FVDL4FA4DTDI4FA4FS5@G7@S5@G20@SDTDT6FEF
|
||||
|
||||
[Library subroutine R3.
|
||||
Input of one long signed decimal fraction, returned in 0D.]
|
||||
T78K
|
||||
GKT45KP26@TZA3FTHTDT4DA6HT9@H1HS4HT6FIFAFS4HE7HT7FV4DL8FADT4DA6FA5HG8@
|
||||
H2#HN4DLDYFTDT28#ZPFT27ZTFP610D@524DP5DPDIFS4HG37@S4DT4DT7FA1HT9@E18@
|
||||
|
||||
[Library subroutine D6 - Division, accurate, fast.
|
||||
36 locations, workspace 6D and 8D.
|
||||
0D := 0D/4D, where 4D <> 0, -1.]
|
||||
T120K
|
||||
GKA3FT34@S4DE13@T4DSDTDE2@T4DADLDTDA4DLDE8@RDU4DLDA35@
|
||||
T6DE25@U8DN8DA6DT6DH6DS6DN4DA4DYFG21@SDVDTDEFW1526D
|
||||
|
||||
[Library subroutine P7: print strictly positive integer in 0D.]
|
||||
T156K
|
||||
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSF
|
||||
L4FT4DA1FA27@G11@T28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@
|
||||
|
||||
[Library subroutine P1: print non-negative fraction in 0D, without '0.']
|
||||
T192K
|
||||
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F
|
||||
|
||||
[==========================================================================
|
||||
On the original EDSAC, the following (without the whitespace and comments)
|
||||
might have been input on a separate tape.]
|
||||
|
||||
E25K TX GK
|
||||
EZ [define entry point]
|
||||
PF [acc = 0 on entry]
|
||||
|
||||
[Counts and data values to be read by library subroutines R3 and R4 respectively.
|
||||
Note (1) Sign comes *after* value (2) In the data, leading '0.' is omitted.]
|
||||
7+ 1-2-3-4-5+2-3-
|
||||
1+ 987654321+
|
||||
9+ 01+04+09+16+25+36+49+64+81+
|
||||
9+ 01-04+09-16+25-36+49-64+81-
|
||||
[Daily minimum temperature (unit = 10 deg. C), Cambridge, UK, January 2000]
|
||||
31+ 34+14+49+00+04+48+05+48+23-35-07-75+19+03+
|
||||
26+27+17-06-52+22-17+18+15+03-33-11-04-01-44+89+95+
|
||||
0+
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fun mean = real by some real values
|
||||
real sum
|
||||
int count
|
||||
for each real value in values
|
||||
sum += value
|
||||
++count
|
||||
end
|
||||
return when(count == 0, 0.0, sum / count)
|
||||
end
|
||||
writeLine(mean())
|
||||
writeLine(mean(3,1,4,1,5,9))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
func mean . f[] r .
|
||||
for i = 1 to len f[]
|
||||
s += f[i]
|
||||
.
|
||||
r = s / len f[]
|
||||
.
|
||||
f[] = [ 1 2 3 4 5 6 7 8 ]
|
||||
call mean f[] r
|
||||
print r
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
(lib 'math)
|
||||
(mean '(1 2 3 4)) ;; mean of a list
|
||||
→ 2.5
|
||||
(mean #(1 2 3 4)) ;; mean of a vector
|
||||
→ 2.5
|
||||
|
||||
(lib 'sequences)
|
||||
(mean [1 3 .. 10]) ;; mean of a sequence
|
||||
→ 5
|
||||
|
||||
;; error handling
|
||||
(mean 'elvis)
|
||||
⛔ error: mean : expected sequence : elvis
|
||||
(mean ())
|
||||
💣 error: mean : null is not an object
|
||||
(mean #())
|
||||
😐 warning: mean : zero-divide : empty-vector
|
||||
→ 0
|
||||
(mean [2 2 .. 2])
|
||||
😁 warning: mean : zero-divide : empty-sequence
|
||||
→ 0
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import extensions;
|
||||
|
||||
extension op
|
||||
{
|
||||
average()
|
||||
{
|
||||
real sum := 0;
|
||||
int count := 0;
|
||||
|
||||
var enumerator := self.enumerator();
|
||||
|
||||
while (enumerator.next())
|
||||
{
|
||||
sum += enumerator.get();
|
||||
count += 1;
|
||||
};
|
||||
|
||||
^ sum / count
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var array := new int[]{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
console.printLine(
|
||||
"Arithmetic mean of {",array.asEnumerable(),"} is ",
|
||||
array.average()).readChar()
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
defmodule Average do
|
||||
def mean(list), do: Enum.sum(list) / length(list)
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defun mean (lst)
|
||||
(/ (float (apply '+ lst)) (length lst)))
|
||||
(mean '(1 2 3 4))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let ((x '(1 2 3 4)))
|
||||
(calc-eval "vmean($1)" nil (append '(vec) x)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
mean([]) -> 0;
|
||||
mean(L) -> lists:sum(L)/erlang:length(L).
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function mean(sequence s)
|
||||
atom sum
|
||||
if length(s) = 0 then
|
||||
return 0
|
||||
else
|
||||
sum = 0
|
||||
for i = 1 to length(s) do
|
||||
sum += s[i]
|
||||
end for
|
||||
return sum/length(s)
|
||||
end if
|
||||
end function
|
||||
|
||||
sequence test
|
||||
test = {1.0, 2.0, 5.0, -5.0, 9.5, 3.14159}
|
||||
? mean(test)
|
||||
|
|
@ -0,0 +1 @@
|
|||
=AVERAGE(A1:A10)
|
||||
|
|
@ -0,0 +1 @@
|
|||
=AVERAGE(
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
let avg (a:float) (v:float) n =
|
||||
a + (1. / ((float n) + 1.)) * (v - a)
|
||||
|
||||
let mean_series list =
|
||||
let a, _ = List.fold_left (fun (a, n) h -> avg a (float h) n, n + 1) (0., 0) list in
|
||||
a
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
> mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;;
|
||||
val it : float = 10.86666667
|
||||
> mean_series [] ;;
|
||||
val it : float = 0.0
|
||||
|
|
@ -0,0 +1 @@
|
|||
List.average [4;1;7;5;8;4;5;2;1;5;2;5]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
USING: math math.statistics ;
|
||||
|
||||
: arithmetic-mean ( seq -- n )
|
||||
[ 0 ] [ mean ] if-empty ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
( scratchpad ) { 2 3 5 } arithmetic-mean >float
|
||||
3.333333333333333
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
class Main
|
||||
{
|
||||
static Float average (Float[] nums)
|
||||
{
|
||||
if (nums.size == 0) return 0.0f
|
||||
Float sum := 0f
|
||||
nums.each |num| { sum += num }
|
||||
return sum / nums.size.toFloat
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
[[,], [1f], [1f,2f,3f,4f]].each |Float[] i|
|
||||
{
|
||||
echo ("Average of $i is: " + average(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
!vl0=?vl1=?vl&!
|
||||
v< +<>0n; >n;
|
||||
>l1)?^&,n;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
: fmean ( addr n -- f )
|
||||
0e
|
||||
dup 0= if 2drop exit then
|
||||
tuck floats bounds do
|
||||
i f@ f+
|
||||
1 floats +loop
|
||||
0 d>f f/ ;
|
||||
|
||||
create test 3e f, 1e f, 4e f, 1e f, 5e f, 9e f,
|
||||
test 6 fmean f. \ 3.83333333333333
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
real, target, dimension(100) :: a = (/ (i, i=1, 100) /)
|
||||
real, dimension(5,20) :: b = reshape( a, (/ 5,20 /) )
|
||||
real, pointer, dimension(:) :: p => a(2:1) ! pointer to zero-length array
|
||||
real :: mean, zmean, bmean
|
||||
real, dimension(20) :: colmeans
|
||||
real, dimension(5) :: rowmeans
|
||||
|
||||
mean = sum(a)/size(a) ! SUM of A's elements divided by SIZE of A
|
||||
mean = sum(a)/max(size(a),1) ! Same result, but safer code
|
||||
! MAX of SIZE and 1 prevents divide by zero if SIZE == 0 (zero-length array)
|
||||
|
||||
zmean = sum(p)/max(size(p),1) ! Here the safety check pays off. Since P is a zero-length array,
|
||||
! expression becomes "0 / MAX( 0, 1 ) -> 0 / 1 -> 0", rather than "0 / 0 -> NaN"
|
||||
|
||||
bmean = sum(b)/max(size(b),1) ! multidimensional SUM over multidimensional SIZE
|
||||
|
||||
rowmeans = sum(b,1)/max(size(b,2),1) ! SUM elements in each row (dimension 1)
|
||||
! dividing by the length of the row, which is the number of columns (SIZE of dimension 2)
|
||||
colmeans = sum(b,2)/max(size(b,1),1) ! SUM elements in each column (dimension 2)
|
||||
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function Mean(array() As Double) As Double
|
||||
Dim length As Integer = Ubound(array) - Lbound(array) + 1
|
||||
If length = 0 Then
|
||||
Return 0.0/0.0 'NaN
|
||||
End If
|
||||
Dim As Double sum = 0.0
|
||||
For i As Integer = LBound(array) To UBound(array)
|
||||
sum += array(i)
|
||||
Next
|
||||
Return sum/length
|
||||
End Function
|
||||
|
||||
Function IsNaN(number As Double) As Boolean
|
||||
Return Str(number) = "-1.#IND" ' NaN as a string in FB
|
||||
End Function
|
||||
|
||||
Dim As Integer n, i
|
||||
Dim As Double num
|
||||
Print "Sample input and output"
|
||||
Print
|
||||
Do
|
||||
Input "How many numbers are to be input ? : ", n
|
||||
Loop Until n > 0
|
||||
Dim vector(1 To N) As Double
|
||||
Print
|
||||
For i = 1 to n
|
||||
Print " Number #"; i; " : ";
|
||||
Input "", vector(i)
|
||||
Next
|
||||
Print
|
||||
Print "Mean is"; Mean(vector())
|
||||
Print
|
||||
Erase vector
|
||||
num = Mean(vector())
|
||||
If IsNaN(num) Then
|
||||
Print "After clearing the vector, the mean is 'NaN'"
|
||||
End If
|
||||
Print
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1 @@
|
|||
mean[x] := length[x] > 0 ? sum[x] / length[x] : undef
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
local fn MeanAverageOfNumberArray( numberArr as CFArrayRef ) as CFStringRef
|
||||
CFStringRef result = NULL
|
||||
if len(numberArr) == 0 then result = @"Mean undefined for empty array." : exit fn
|
||||
result = fn StringWithFormat( @"Mean average of %d numbers: %@", len(numberArr), fn ObjectValueForKeyPath( numberArr, @"@avg.self" ) )
|
||||
end fn = result
|
||||
|
||||
CFArrayRef numberArray
|
||||
numberArray = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10]
|
||||
print fn MeanAverageOfNumberArray( numberArray )
|
||||
numberArray = @[@3, @1, @4, @1, @5, @9]
|
||||
print fn MeanAverageOfNumberArray( numberArray )
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
Mean := function(v)
|
||||
local n;
|
||||
n := Length(v);
|
||||
if n = 0 then
|
||||
return 0;
|
||||
else
|
||||
return Sum(v)/n;
|
||||
fi;
|
||||
end;
|
||||
|
||||
Mean([3, 1, 4, 1, 5, 9]);
|
||||
# 23/6
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
R (n) P ;
|
||||
0
|
||||
1, n rep (i)
|
||||
R P +
|
||||
]
|
||||
n div
|
||||
P
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
DIM a%(10)
|
||||
FOR i%=0 TO 10
|
||||
a%(i%)=i%*2
|
||||
PRINT "element ";i%;" is ";a%(i%)
|
||||
NEXT i%
|
||||
PRINT "mean is ";@mean(a%)
|
||||
'
|
||||
FUNCTION mean(a%)
|
||||
LOCAL i%,size%,sum
|
||||
' find size of array,
|
||||
size%=DIM?(a%())
|
||||
' return 0 for empty arrays
|
||||
IF size%<=0
|
||||
RETURN 0
|
||||
ENDIF
|
||||
' find sum of all elements
|
||||
sum=0
|
||||
FOR i%=0 TO size%-1
|
||||
sum=sum+a%(i%)
|
||||
NEXT i%
|
||||
' mean is sum over size
|
||||
RETURN sum/size%
|
||||
ENDFUNC
|
||||
65
Task/Averages-Arithmetic-mean/Go/averages-arithmetic-mean.go
Normal file
65
Task/Averages-Arithmetic-mean/Go/averages-arithmetic-mean.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func mean(v []float64) (m float64, ok bool) {
|
||||
if len(v) == 0 {
|
||||
return
|
||||
}
|
||||
// an algorithm that attempts to retain accuracy
|
||||
// with widely different values.
|
||||
var parts []float64
|
||||
for _, x := range v {
|
||||
var i int
|
||||
for _, p := range parts {
|
||||
sum := p + x
|
||||
var err float64
|
||||
switch ax, ap := math.Abs(x), math.Abs(p); {
|
||||
case ax < ap:
|
||||
err = x - (sum - p)
|
||||
case ap < ax:
|
||||
err = p - (sum - x)
|
||||
}
|
||||
if err != 0 {
|
||||
parts[i] = err
|
||||
i++
|
||||
}
|
||||
x = sum
|
||||
}
|
||||
parts = append(parts[:i], x)
|
||||
}
|
||||
var sum float64
|
||||
for _, x := range parts {
|
||||
sum += x
|
||||
}
|
||||
return sum / float64(len(v)), true
|
||||
}
|
||||
|
||||
func main() {
|
||||
for _, v := range [][]float64{
|
||||
[]float64{}, // mean returns ok = false
|
||||
[]float64{math.Inf(1), math.Inf(1)}, // answer is +Inf
|
||||
|
||||
// answer is NaN, and mean returns ok = true, indicating NaN
|
||||
// is the correct result
|
||||
[]float64{math.Inf(1), math.Inf(-1)},
|
||||
|
||||
[]float64{3, 1, 4, 1, 5, 9},
|
||||
|
||||
// large magnitude numbers cancel. answer is mean of small numbers.
|
||||
[]float64{1e20, 3, 1, 4, 1, 5, 9, -1e20},
|
||||
|
||||
[]float64{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11},
|
||||
[]float64{10, 20, 30, 40, 50, -100, 4.7, -11e2},
|
||||
} {
|
||||
fmt.Println("Vector:", v)
|
||||
if m, ok := mean(v); ok {
|
||||
fmt.Printf("Mean of %d numbers is %g\n\n", len(v), m)
|
||||
} else {
|
||||
fmt.Println("Mean undefined\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
def avg = { list -> list == [] ? 0 : list.sum() / list.size() }
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
println avg(0..9)
|
||||
println avg([2,2,2,4,2])
|
||||
println avg ([])
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
mean :: (Fractional a) => [a] -> a
|
||||
mean [] = 0
|
||||
mean xs = sum xs / Data.List.genericLength xs
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
meanReals :: (Real a, Fractional b) => [a] -> b
|
||||
meanReals = mean . map realToFrac
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
{-# LANGUAGE BangPatterns #-}
|
||||
|
||||
import Data.List (foldl') --'
|
||||
|
||||
mean
|
||||
:: (Real n, Fractional m)
|
||||
=> [n] -> m
|
||||
mean xs =
|
||||
let (s, l) =
|
||||
foldl' --'
|
||||
f
|
||||
(0, 0)
|
||||
xs
|
||||
in realToFrac s / l
|
||||
where
|
||||
f (!s, !l) x = (s + x, l + 1)
|
||||
|
||||
main :: IO ()
|
||||
main = print $ mean [1 .. 100]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
REAL :: vec(100) ! no zero-length arrays in HicEst
|
||||
|
||||
vec = $ - 1/2 ! 0.5 ... 99.5
|
||||
mean = SUM(vec) / LEN(vec) ! 50
|
||||
END
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defn arithmetic-mean [xs]
|
||||
(if xs
|
||||
(/ (sum xs) (len xs))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
x = [3,1,4,1,5,9]
|
||||
print,mean(x)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue