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

43 lines
895 B
Text

' Cumulative standard deviation
TYPE Sample
N AS INTEGER
Sum AS SINGLE
SumOfSquares AS SINGLE
END TYPE
DECLARE SUB Init (S AS Sample)
DECLARE SUB Add (S AS Sample, BYVAL P AS SINGLE)
DECLARE FUNCTION Deviation! (S AS Sample)
DIM Test(1 TO 8) AS SINGLE
DIM S AS Sample
FOR I = 1 TO 8
READ Test(I)
NEXT I
DATA 2, 4, 4, 4, 5, 5, 7, 9
CALL Init(S)
PRINT "N ITEM AVG STDDEV"
FOR I = LBOUND(Test) TO UBOUND(Test)
CALL Add(S, Test(I))
PRINT USING "# "; I;
PRINT USING " # "; Test(I);
PRINT USING "#.### "; S.Sum / I;
PRINT USING " #.###"; Deviation!(S)
NEXT I
END
SUB Add (S AS Sample, BYVAL P AS SINGLE)
S.N = S.N + 1
S.Sum = S.Sum + P
S.SumOfSquares = S.SumOfSquares + P ^ 2
END SUB
FUNCTION Deviation! (S AS Sample)
Deviation! = SQR(S.SumOfSquares / S.N - (S.Sum / S.N) ^ 2)
END FUNCTION
SUB Init (S AS Sample)
S.N = 0
S.Sum = 0!
S.SumOfSquares = 0!
END SUB