RosettaCodeData/Task/Cumulative-standard-deviation/Loglan82/cumulative-standard-deviation.loglan82
2026-04-30 12:34:36 -04:00

50 lines
1,005 B
Text

program CumulDev;
(* Cumulative standard deviation *)
unit SAMPLE: class;
var
n: integer,
sum: real,
sum_of_squares: real;
unit add: procedure (p: real);
begin
n := n + 1;
sum := sum + p;
sum_of_squares := sum_of_squares + p * p
end add;
begin (* constructor *)
n := 0;
sum := 0.0;
sum_of_squares := 0.0
end;
var
test: arrayof real,
s: SAMPLE,
i: integer;
unit deviation: function (s: SAMPLE): real;
var
quot: real;
begin
quot := s.sum / s.n;
result := sqrt(s.sum_of_squares / s.n - quot * quot)
end deviation;
begin
array test dim (1 : 8);
test(1) := 2; test(2) := 4; test(3) := 4; test(4) := 4;
test(5) := 5; test(6) := 5; test(7) := 7; test(8) := 9;
s := new SAMPLE;
writeln("N ITEM AVG STDDEV");
for i := 1 to upper(test)
do
call s.add(test(i));
write(i: 1, " ", test(i): 3: 1, " ");
write(s.sum / i: 5: 3, " ");
writeln(deviation(s): 5: 3);
od;
kill(s);
end CumulDev.