Statistical Measures

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

This program computes a comprehensive set of descriptive statistics for a user-supplied dataset of up to 100 values. It calculates arithmetic mean, median, mode (including multiple modes), standard deviation, variance, minimum, maximum, and range. Data entry ends when the user inputs the sentinel value 9999, and an interactive correction loop lets the user amend any numbered item before processing. Sorting is performed in-place using a straightforward O(n²) selection-style sort, and the mode is detected by scanning the sorted array for runs of equal values using a parallel count array.


Program Analysis

Program Structure

The program is organized as a main routine with six subroutines, each handling a distinct processing stage. The main routine (lines 20–310) drives the flow sequentially, calling each subroutine in order.

  1. Lines 20–80: Initialization — sets constants, dimensions arrays, prints header.
  2. Lines 90–140: Data entry loop, terminated by sentinel value L=9999.
  3. Lines 150–230: Main dispatch — optional data print/correct, then calls all computation subroutines.
  4. Lines 330–520: Subroutine — print and optionally correct data items.
  5. Lines 540–600: Subroutine — compute arithmetic mean (S1).
  6. Lines 620–720: Subroutine — sort data array in ascending order.
  7. Lines 740–810: Subroutine — compute median (S2).
  8. Lines 830–1030: Subroutine — compute mode (S3), supporting multiple modes.
  9. Lines 1050–1120: Subroutine — compute standard deviation (S4) and variance (S5).
  10. Lines 1140–1360: Subroutine — print all results.

Data Entry and Correction

The entry loop (lines 90–130) pre-fills each slot with L (9999) at line 100, then immediately overwrites it with INPUT D(I) at line 110. The sentinel check at line 120 exits the loop early. The actual count of entered values is stored in I1 = I - 1 at line 140.

The correction subroutine (lines 330–520) reprints the numbered list and prompts for an item number to change. Entering 0 exits the correction loop (line 480). This loop repeats until the user explicitly answers “N” to the correction prompt.

Sorting Algorithm

The sort subroutine (lines 640–710) uses a nested-loop comparison sort. For each pair (I, J) where J > I, it swaps elements if D(I) > D(J), using DO as a temporary variable. This is effectively a selection/bubble hybrid with O(n²) complexity, appropriate for the 100-item maximum.

Median Calculation

After sorting, the median subroutine (lines 760–800) checks whether I1 is even by comparing INT(I1/2) with I1/2. For an even count it averages the two middle elements; for an odd count it selects the central element. Note the off-by-one pattern: for odd I1, T6 = INT(I1/2) and the median is D(T6+1), which correctly indexes the middle element in 1-based arrays.

Mode Calculation and Multiple Modes

The mode subroutine (lines 850–1030) exploits the sorted order to count consecutive runs. A sentinel D(I1+1) = L is appended to force a final run flush. The count of each run is stored in parallel array C, and C9 tracks the maximum run length. A second pass then collects all data values whose run count equals C9, storing successive modes in S3 with counter K. The results loop at line 1250–1270 prints all K-1 modes.

Notable Bugs and Anomalies

  • Uninitialized accumulators: S1, T8, CO, and C9 are never explicitly initialized to zero; the program relies on the interpreter’s default of zero for unset numeric variables. This works in practice but is fragile if the program were restarted without a full NEW.
  • Variable name collision — C0 vs CO: Line 940 uses LET C0=1 (digit zero) while lines 960 and 920 use CO (letter O). In Sinclair BASIC, variable names are single characters, so C0 and CO are distinct variables. The counter is incremented as CO but reset as C0, meaning the reset never affects the variable being counted — this is a genuine bug that will cause incorrect mode counts across different run values.
  • SUM printed as MEAN: Line 1200 labels S1 as “SUM” but by that point S1 has already been divided by I1 in the mean subroutine (line 590), so it actually prints the mean twice — once labelled “SUM” and again labelled “MEAN” at line 1220.
  • DIM S(M4) at line 75: The array S is dimensioned to 10 but never used anywhere in the program; it appears to be a vestigial artifact.
  • Variance label: The standard deviation computed at line 1110 is the population standard deviation (dividing by I1, not I1-1), which is mathematically consistent with the population variance at line 1100.

Key Variables Summary

VariableRole
D()Data array, max 100 elements
I1Actual count of entered values
S1Arithmetic mean
S2Median
S3Mode (last found)
S4Standard deviation
S5Variance
C9Maximum run frequency (for mode)
KNumber of modes found
LSentinel value (9999)
X$Decorative separator line

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10051 – 10121.

Related Products

Related Articles

Related Content

Image Gallery

Statistical Measures

Source Code

  20 PRINT "STATISTICAL MEASURES",,,
  30 LET X$="********************************"
  40 LET M=100
  50 LET M4=10
  60 LET L=9999
  70 DIM D(M)
  75 DIM S(M4)
  80 PRINT "ENTER DATA--END WITH  ";L;" OR ENTER"
  90 FOR I=1 TO M
 100 LET D(I)=L
 110 INPUT D(I)
 120 IF D(I)=L THEN GOTO 140
 130 NEXT I
 140 LET I1=I-1
 150 PRINT "SHALL I PRINT DATA ITEMS(Y/N)";
 160 INPUT A$
 170 IF A$="Y" THEN GOSUB 330
 180 GOSUB 540
 190 GOSUB 620
 200 GOSUB 740
 210 GOSUB 830
 220 GOSUB 1050
 230 GOSUB 1140
 240 REM %P%R%O%G%R%A%M% %T%E%R%M%I%N%A%T%I%O%N
 270 PRINT 
 280 PRINT 
 290 PRINT "PROCESSING COMPLETE"
 300 PRINT 
 310 STOP 
 330 REM %P%R%I%N%T% %D%A%T%A
 350 PRINT 
 360 PRINT X$
 370 PRINT " NR.";TAB (5);"DATA"
 380 FOR I=1 TO I1
 390 PRINT I;TAB (5);D(I)
 400 NEXT I
 410 PRINT "WANT TO CORRECT DATA Y/N?";
 420 LET A$=""
 430 INPUT A$
 440 IF A$<>"Y" THEN GOTO 520
 450 PRINT "ENTER ITEM NR TO CHANGE";
 460 LET N=0
 470 INPUT N
 480 IF N=0 THEN GOTO 410
 490 PRINT "ENTER CORRECTED DATA";
 500 INPUT D(N)
 510 GOTO 410
 520 RETURN 
 540 REM %A%R%I%T%H%M%E%T%I%C% %M%E%A%N% %=% %S%1
 560 FOR I=1 TO I1
 570 LET S1=S1+D(I)
 580 NEXT I
 590 LET S1=S1/I1
 600 RETURN 
 620 REM %S%O%R%T% %D%A%T%A
 640 FOR I=1 TO I1-1
 650 FOR J=I+1 TO I1
 660 IF D(I)<=D(J) THEN GOTO 700
 670 LET DO=D(I)
 680 LET D(I)=D(J)
 690 LET D(J)=DO
 700 NEXT J
 710 NEXT I
 720 RETURN 
 740 REM %M%E%D%I%A%N% %=% %S%2
 760 LET T6=INT (I1/2)
 770 IF T6<>I1/2 THEN GOTO 800
 780 LET S2=(D(T6)+D(T6+1))/2
 790 GOTO 810
 800 LET S2=D(T6+1)
 810 RETURN 
 830 REM %M%O%D%E% %=% %S%3
 850 LET T5=L
 860 LET K=1
 870 DIM C(I1)
 880 LET D(I1+1)=L
 890 FOR I=1 TO I1+1
 900 IF D(I)=T5 THEN GOTO 960
 910 LET T5=D(I)
 920 LET C(I-1)=CO
 930 IF CO>C9 THEN LET C9=CO
 940 LET C0=1
 950 GOTO 970
 960 LET CO=CO+1
 970 NEXT I
 980 FOR I=1 TO I1
 990 IF C(I)<>C9 THEN GOTO 1020
\n1000 LET S3=D(I)
\n1010 LET K=K+1
\n1020 NEXT I
\n1030 RETURN 
\n1050 REM %S%T%D% %D%E%V%I%A%T%I%O%N%(%S%4%)%/%V%A%R%I%A%N%C%E% %(%S%5%)
\n1070 FOR I=1 TO I1
\n1080 LET T8=T8+(D(I)-S1)*(D(I)-S1)
\n1090 NEXT I
\n1100 LET S5=T8/I1
\n1110 LET S4=SQR (S5)
\n1120 RETURN 
\n1140 REM %P%R%I%N%T% %R%E%S%U%L%T%S
\n1160 PRINT X$
\n1170 PRINT 
\n1180 PRINT 
\n1190 PRINT "NUMBER = ";I1
\n1200 PRINT "SUM = ";S1
\n1210 PRINT 
\n1220 PRINT "MEAN = ";S1
\n1230 PRINT "MEDIAN = ";S2
\n1240 PRINT "MODE = ";
\n1250 FOR I=1 TO K-1
\n1260 PRINT S3,(I);
\n1270 NEXT I
\n1280 PRINT 
\n1290 PRINT "MINIMUM = ";D(1)
\n1300 PRINT "MAXIMUM = ";D(I1)
\n1310 PRINT "RANGE = ";D(I1)-D(1)
\n1320 PRINT "STANDARD DEVIATION = ";S4
\n1330 PRINT "VARIANCE = ";S5
\n1340 PRINT 
\n1350 PRINT X$
\n1360 RETURN 
\n1370 CLEAR 
\n1380 SAVE "1007%2"
\n1390 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