--- title: "Statistical Measures" id: 57123 type: "computer_media" slug: "statistical-measures" url: "http://localhost/computer_media/statistical-measures/" markdown_url: "http://localhost/computer_media/statistical-measures.md" published_at: "2024-10-03T23:53:20+00:00" modified_at: "2026-04-03T07:58:44+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/72_Stat.png" excerpt: "A complete descriptive statistics suite — mean, median, mode, variance, and more — with interactive data entry and on-the-fly correction before crunching the numbers." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56733 title: "Timex Sinclair Public Domain Library Tape 1002" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1002/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/72_Stat.png" media_type_tags: "Mathematics" --- # Statistical Measures 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 | Variable | Role | | --- | --- | | `D()` | Data array, max 100 elements | | `I1` | Actual count of entered values | | `S1` | Arithmetic mean | | `S2` | Median | | `S3` | Mode (last found) | | `S4` | Standard deviation | | `S5` | Variance | | `C9` | Maximum run frequency (for mode) | | `K` | Number of modes found | | `L` | Sentinel value (9999) | | `X$` | Decorative separator line | ## 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 1000 LET S3=D(I) 1010 LET K=K+1 1020 NEXT I 1030 RETURN 1050 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%) 1070 FOR I=1 TO I1 1080 LET T8=T8+(D(I)-S1)*(D(I)-S1) 1090 NEXT I 1100 LET S5=T8/I1 1110 LET S4=SQR (S5) 1120 RETURN 1140 REM %P%R%I%N%T% %R%E%S%U%L%T%S 1160 PRINT X$ 1170 PRINT 1180 PRINT 1190 PRINT "NUMBER = ";I1 1200 PRINT "SUM = ";S1 1210 PRINT 1220 PRINT "MEAN = ";S1 1230 PRINT "MEDIAN = ";S2 1240 PRINT "MODE = "; 1250 FOR I=1 TO K-1 1260 PRINT S3,(I); 1270 NEXT I 1280 PRINT 1290 PRINT "MINIMUM = ";D(1) 1300 PRINT "MAXIMUM = ";D(I1) 1310 PRINT "RANGE = ";D(I1)-D(1) 1320 PRINT "STANDARD DEVIATION = ";S4 1330 PRINT "VARIANCE = ";S5 1340 PRINT 1350 PRINT X$ 1360 RETURN 1370 CLEAR 1380 SAVE "1007%2" 1390 RUN ```