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.
- Lines 20–80: Initialization — sets constants, dimensions arrays, prints header.
- Lines 90–140: Data entry loop, terminated by sentinel value
L=9999. - Lines 150–230: Main dispatch — optional data print/correct, then calls all computation subroutines.
- Lines 330–520: Subroutine — print and optionally correct data items.
- Lines 540–600: Subroutine — compute arithmetic mean (
S1). - Lines 620–720: Subroutine — sort data array in ascending order.
- Lines 740–810: Subroutine — compute median (
S2). - Lines 830–1030: Subroutine — compute mode (
S3), supporting multiple modes. - Lines 1050–1120: Subroutine — compute standard deviation (
S4) and variance (S5). - 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, andC9are 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 fullNEW. - Variable name collision —
C0vsCO: Line 940 usesLET C0=1(digit zero) while lines 960 and 920 useCO(letter O). In Sinclair BASIC, variable names are single characters, soC0andCOare distinct variables. The counter is incremented asCObut reset asC0, 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
S1as “SUM” but by that pointS1has already been divided byI1in 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 arraySis 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, notI1-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 |
Content
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.
