This program computes a rolling moving average alongside the mean and standard deviation of a user-defined set of numbers. The user specifies how many values to track (N), then enters them one at a time; after each full set the program displays all N values, their mean, and their population standard deviation. A sliding-window technique is implemented at line 370: each element X(J-1) is overwritten with X(J), shifting the array left so the oldest value is discarded when a new one is entered next cycle. The standard deviation uses the computational formula √(SS/N − (SX/N)²), avoiding a two-pass algorithm by accumulating both the sum and sum-of-squares in a single loop.
Program Analysis
Program Structure
The program is divided into four logical phases, loosely reflected in its line-number bands:
- Initialisation (lines 5–10): Title display and reset of counter
K. - Data entry (lines 100–240): Prompts for the window size
N, allocates arrayX(N), and collectsNvalues one at a time. - Statistics and display (lines 300–410): Computes sum
SXand sum-of-squaresSSin a single pass, prints all values, mean, and standard deviation. - Loop-back (line 420):
GOTO 210jumps directly to the “NEXT NUMBER?” prompt, bypassing the dimension and initial fill steps, creating the rolling window.
Rolling Window Mechanism
The sliding-window behaviour is achieved entirely within the statistics loop at line 370:
IF J>1 THEN LET X(J-1)=X(J)copies each element one position to the left.- After the loop completes,
X(1)throughX(N-1)hold the values that were in positions 2 through N, andX(N)still holds the most recent entry. - When the program loops back to line 210,
Kis already equal toN(the counter was left at its final value), soINPUT X(K)overwrites onlyX(N)— the slot vacated conceptually by the shift — with the newest value.
This is an elegant in-place shift that avoids any pointer arithmetic or modular indexing, using only basic array assignment.
Statistical Method
Mean and population standard deviation are computed using the one-pass computational formula:
- Mean:
SX/NwhereSXis the sum of all values. - Std Dev:
SQR(SS/N-(SX/N)**2)whereSSis the sum of squared values — equivalent to √(E[X²] − (E[X])²).
Note that ** is used for exponentiation (line 350 and 410), which is valid ZX81/TS1000 BASIC syntax as an alternative to ^. This single-pass approach is numerically efficient for small N but can suffer from catastrophic cancellation with large values or large N — acceptable for the intended hobbyist use case.
Key BASIC Idioms
| Line | Idiom | Purpose |
|---|---|---|
120 | DIM X(N) | Dynamic array sizing based on user input |
320 | PRINT "LAST ";N;" NUMBERS",,,, | Trailing commas advance print position / add blank lines |
410 | Trailing commas after std dev | Provides visual spacing before the next input prompt |
420 | GOTO 210 | Re-enters data entry loop without re-dimensioning |
Counter Behaviour and Potential Anomaly
Variable K is initialised to 0 at line 10 and incremented at line 200 before each input. After the first full pass through lines 200–240, K equals N. When the program loops back to line 210 via line 420, K is not reset. The input at line 220 therefore writes directly into X(N) on every subsequent cycle — which is exactly the intended behaviour given the left-shift at line 370. However, K never advances beyond N again, so the IF K<N THEN GOTO 200 guard at line 240 is never triggered after the first fill; the program always falls through to CLS and the statistics block, accepting exactly one new value per cycle.
Save Record
Line 500 saves the program under the name 1004 (the trailing inverse zero is the auto-run flag). Line 510 then immediately RUNs, a common pattern to restart the program after saving.
Content
Source Code
5 PRINT "%M%O%V%I%N%G% %A%V%E%R%A%G%E"
6 PRINT "CALC MEAN/STD DEV"
10 LET K=0
100 PRINT "HOW MANY NOS.?"
110 INPUT N
120 DIM X(N)
200 LET K=K+1
210 PRINT "NEXT NUMBER?";
220 INPUT X(K)
230 PRINT X(K)
240 IF K<N THEN GOTO 200
250 CLS
300 LET SX=0
310 LET SS=0
320 PRINT "LAST ";N;" NUMBERS",,,,
330 FOR J=1 TO N
340 LET SX=SX+X(J)
350 LET SS=SS+X(J)**2
360 PRINT " ";X(J)
370 IF J>1 THEN LET X(J-1)=X(J)
380 NEXT J
400 PRINT ,,"MEAN = ";SX/N
410 PRINT ,,"STD DEV = ";SQR (SS/N-(SX/N)**2),,,,,
420 GOTO 210
500 SAVE "1004%0"
510 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
