Moving Average

This file is part of and Timex Sinclair Public Domain Library Tape 1001. Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000

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:

  1. Initialisation (lines 5–10): Title display and reset of counter K.
  2. Data entry (lines 100–240): Prompts for the window size N, allocates array X(N), and collects N values one at a time.
  3. Statistics and display (lines 300–410): Computes sum SX and sum-of-squares SS in a single pass, prints all values, mean, and standard deviation.
  4. Loop-back (line 420): GOTO 210 jumps 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) through X(N-1) hold the values that were in positions 2 through N, and X(N) still holds the most recent entry.
  • When the program loops back to line 210, K is already equal to N (the counter was left at its final value), so INPUT X(K) overwrites only X(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/N where SX is the sum of all values.
  • Std Dev: SQR(SS/N-(SX/N)**2) where SS is 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

LineIdiomPurpose
120DIM X(N)Dynamic array sizing based on user input
320PRINT "LAST ";N;" NUMBERS",,,,Trailing commas advance print position / add blank lines
410Trailing commas after std devProvides visual spacing before the next input prompt
420GOTO 210Re-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

Appears On

Assembled by Tim Ward from many sources. Contains programs 10001 – 10050.

Related Products

Related Articles

Related Content

Image Gallery

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.

People

No people associated with this content.

Scroll to Top