This program computes the arithmetic mean of a series of numbers entered by the user. It accumulates a count in variable N and a running total in T, then divides T by N when the user enters 0 as a sentinel value to terminate input. After displaying the result, it loops back to line 10 to reset the counters and run again, allowing repeated calculations without restarting. The REM statement at line 5 and the title PRINT at line 22 both use inverse-video characters to display “AVERAGES” as a highlighted heading.
Program Analysis
Program Structure
The program is organized as a simple sequential loop with a sentinel-terminated input phase followed by a results display phase. Control flow is entirely managed by GOTO statements rather than subroutines.
- Initialization (lines 10–20): Variables
N(count),T(total), andA(average) are all set to zero. - Title display (lines 22–23): The heading “AVERAGES” is printed in inverse video, followed by a blank line.
- Input loop (lines 25–60): The user is prompted repeatedly; each non-zero value increments
Nand adds toT, then loops back to line 25. - Calculation and display (lines 70–100): On sentinel input of 0,
Ais computed asT/N, the screen is cleared, and the average is printed. - Outer reset loop (line 110):
GOTO 10resets all variables and repeats the entire process indefinitely.
Variables
| Variable | Purpose |
|---|---|
N | Count of numbers entered |
T | Running total of entered values |
A | Computed arithmetic mean (T/N) |
Z | Most recently entered number / sentinel |
Key BASIC Idioms
- Sentinel value: Entering
0terminates the input loop — a classic idiom for indefinite input without a predetermined count. - Re-initialization via GOTO: Rather than using a separate GOSUB or structuring the reset explicitly,
GOTO 10at line 110 re-executes theLETassignments to reset state, which is concise but relies on the program re-running those lines each cycle. - Inline echo: Line 40 prints
Zimmediately after input, giving the user a confirmation of what was recorded.
Notable Techniques
The inverse-video “AVERAGES” title is used in both the REM at line 5 (visible in the listing) and the PRINT at line 22 (visible at runtime), providing a consistent styled heading. The REM usage here is purely cosmetic for the program listing.
Bugs and Anomalies
- Division by zero: If the user immediately enters
0without providing any numbers, line 70 will attempt to evaluateT/NwithN=0, causing a division-by-zero error. There is no guard against this case. - Negative sentinel ambiguity: The sentinel is hard-coded as
0, meaning zero cannot itself be included as a data value in the series, which is a known limitation of sentinel-based designs. - Redundant variable A: Variable
Ais initialized at line 20 but is only ever assigned at line 70 immediately before use; the initialization is unnecessary. - Lines 120–130: The
SAVEandRUNat lines 120–130 are unreachable during normal execution since the program loops indefinitely via line 110.
Content
Source Code
5 REM %A%V%E%R%A%G%E%S
10 LET N=0
15 LET T=0
20 LET A=0
22 PRINT "%A%V%E%R%A%G%E%S"
23 PRINT
25 PRINT "ENTER A NUMBER(ENTER 0 TO STOP)"
30 INPUT Z
35 IF Z=0 THEN GOTO 70
40 PRINT Z
45 LET N=N+1
50 LET T=T+Z
60 GOTO 25
70 LET A=T/N
80 CLS
90 PRINT "THE AVG. NR. IS ";A
100 PRINT
110 GOTO 10
120 SAVE "1005%3"
130 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
