Cumulative standard deviation: Difference between revisions
→{{header|ALGOL 68}}: Changed output to conform to Task requirements and removed "incorrect" notice
(→{{header|ALGOL 68}}: Changed output to conform to Task requirements and removed "incorrect" notice) |
|||
Line 175:
=={{header|ALGOL 68}}==
{{trans|C}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
Line 190 ⟶ 189:
COUNT = STRUCT(CHAR count),
RESET = STRUCT(CHAR reset);
MODE ACTION = UNION ( VALUE, STDDEV, MEAN, VAR, COUNT, RESET );
LONG REAL sum := 0;
LONG REAL sum2 := 0;
INT num := 0;
PROC stat object = (LONG REAL v, ACTION action)LONG REAL:
(
Line 223 ⟶ 222:
ESAC
);
[]LONG REAL v = ( 2,4,4,4,5,5,7,9 );
Line 231 ⟶ 230:
FOR i FROM LWB v TO UPB v DO
sd := stat object(v[i], LOC VALUE);
printf(($"value: "g(0,6)," standard dev := "g(0,6)l$, v[i], sd))▼
OD
▲ printf(($"standard dev := "g(0,6)l$, sd))
)</lang>
{{out}}
<pre>
value: 2.000000 standard dev :=
value: 4.000000 standard dev := 1.000000
value: 4.000000 standard dev := .942809
value: 4.000000 standard dev := .866025
value: 5.000000 standard dev := .979796
value: 5.000000 standard dev := 1.000000
value: 7.000000 standard dev := 1.399708
value: 9.000000 standard dev := 2.000000
</pre>
{{trans|python}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
Line 252 ⟶ 258:
INT num
);
OP INIT = (REF STAT new)REF STAT:
(init OF class stat)(new);
MODE CLASSSTAT = STRUCT(
PROC (REF STAT, LONG REAL #value#)VOID plusab,
Line 263 ⟶ 269:
CLASSSTAT class stat;
plusab OF class stat := (REF STAT self, LONG REAL value)VOID:(
num OF self +:= 1;
Line 269 ⟶ 275:
sum2 OF self +:= value*value
);
OP +:= = (REF STAT lhs, LONG REAL rhs)VOID: # some syntatic sugar #
(plusab OF class stat)(lhs, rhs);
stddev OF class stat := (REF STAT self)LONG REAL:
long sqrt((variance OF class stat)(self));
OP STDDEV = ([]LONG REAL value)LONG REAL: ( # more syntatic sugar #
REF STAT stat = INIT LOC STAT;
Line 283 ⟶ 289:
(stddev OF class stat)(stat)
);
mean OF class stat := (REF STAT self)LONG REAL:
sum OF self/LONG REAL(num OF self);
variance OF class stat := (REF STAT self)LONG REAL:(
LONG REAL m = (mean OF class stat)(self);
sum2 OF self/LONG REAL(num OF self)-m*m
);
count OF class stat := (REF STAT self)LONG REAL:
num OF self;
init OF class stat := (REF STAT self)REF STAT:(
sum OF self := sum2 OF self := num OF self := 0;
self
);
[]LONG REAL value = ( 2,4,4,4,5,5,7,9 );
main:
(
# printf(($"standard deviation operator = "g(0,6)l$, STDDEV value));
#
REF STAT stat = INIT LOC STAT;
FOR i FROM LWB value TO UPB value DO
stat +:= value[i];
printf(($"value: "g(0,6)," standard dev := "g(0,6)l$, value[i], (stddev OF class stat)(stat)))
OD
#
;
printf(($"standard deviation = "g(0,6)l$, (stddev OF class stat)(stat)));
printf(($"mean = "g(0,6)l$, (mean OF class stat)(stat)));
printf(($"variance = "g(0,6)l$, (variance OF class stat)(stat)));
printf(($"count = "g(0,6)l$, (count OF class stat)(stat)))
#
)</lang>▼
)
{{out}}
<pre>
value: 4.000000 standard
value: 4.000000 standard dev := .942809
value: 5.000000 standard dev := .979796
value: 5.000000 standard dev := 1.000000
value: 7.000000 standard dev := 1.399708
value: 9.000000 standard dev := 2.000000
</pre>
|