Average

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 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.

  1. Initialization (lines 10–20): Variables N (count), T (total), and A (average) are all set to zero.
  2. Title display (lines 22–23): The heading “AVERAGES” is printed in inverse video, followed by a blank line.
  3. Input loop (lines 25–60): The user is prompted repeatedly; each non-zero value increments N and adds to T, then loops back to line 25.
  4. Calculation and display (lines 70–100): On sentinel input of 0, A is computed as T/N, the screen is cleared, and the average is printed.
  5. Outer reset loop (line 110): GOTO 10 resets all variables and repeats the entire process indefinitely.

Variables

VariablePurpose
NCount of numbers entered
TRunning total of entered values
AComputed arithmetic mean (T/N)
ZMost recently entered number / sentinel

Key BASIC Idioms

  • Sentinel value: Entering 0 terminates 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 10 at line 110 re-executes the LET assignments to reset state, which is concise but relies on the program re-running those lines each cycle.
  • Inline echo: Line 40 prints Z immediately 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 0 without providing any numbers, line 70 will attempt to evaluate T/N with N=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 A is initialized at line 20 but is only ever assigned at line 70 immediately before use; the initialization is unnecessary.
  • Lines 120–130: The SAVE and RUN at lines 120–130 are unreachable during normal execution since the program loops indefinitely via line 110.

Content

Appears On

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

Related Products

Related Articles

Related Content

Image Gallery

Average

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.

People

No people associated with this content.

Scroll to Top