Algebra I

Products: Algebra I
Date: 198x
Type: Cassette
Platform(s): ZX81, TS 1000
Tags: Education

This program solves quadratic equations of the form AX² + BX + C = 0 using the quadratic formula, handling both real and complex roots. It computes the discriminant S = B²−4AC; if negative, it calculates the real and imaginary parts separately and displays the complex conjugate pair in the form a ± bI. The program uses SLOW mode during input and output for readability, switching to FAST mode during computation. A DIM A$(1) declaration at line 16 allocates a single-character string for the yes/no prompt response. Input validation ensures coefficient A is non-zero, preventing division by zero in the root calculations.


Program Analysis

Program Structure

The program is organized into a straightforward linear flow with two branch points. Lines 10–46 handle setup and coefficient input, lines 47–109 perform computation and output results, and lines 110–160 manage the repeat-or-quit loop. The overall structure is:

  1. Initialize display and prompt for coefficients A, B, C (lines 10–46)
  2. Compute discriminant and roots (lines 47–109)
  3. Ask user to repeat or quit (lines 110–160)
  4. Save line at 170–180 (never reached during normal execution)

Mathematical Approach

The discriminant is calculated at line 50 as S = B*B - 4*A*C. Line 60 computes R = SQR(ABS S), taking the absolute value before the square root so that the same variable R holds the magnitude regardless of the sign of S. The branch at line 70 then determines whether roots are real (S ≥ 0) or complex (S < 0).

For real roots, line 85 prints both values directly using the quadratic formula: (-B-R)/(2*A) and (R-B)/(2*A). For complex roots, line 102 reassigns S to hold the imaginary coefficient R/(2*A), and lines 103–104 print the conjugate pair in the form a + bI / a - bI.

Key BASIC Idioms

  • DIM A$(1) at line 16 — allocates a one-character string variable for the Y/N response, required before string input can be accepted.
  • SLOW / FAST toggling (lines 15, 47, 115) — switches between display-synchronised and full-speed execution modes at appropriate points.
  • IF A=0 THEN GOTO 33 at line 36 — simple validation loop that re-prompts until a non-zero coefficient A is entered, preventing a division-by-zero error.
  • IF A$="Y" THEN RUN at line 135 — restarts the entire program from the beginning rather than using a GOTO, which cleanly reinitializes all variables.

Notable Techniques

The reuse of variable S at line 102 is a minor space-saving idiom: after the discriminant has served its purpose in the branch test, the variable is repurposed to store the imaginary part of the complex roots. This avoids introducing an additional variable at the cost of some readability.

Lines 30–31 simulate a superscript “2” for the equation display by printing it on the line above the formula text, achieving a rudimentary typeset appearance within the constraints of the character-based display.

Bugs and Anomalies

  • The PRINT "ENTER COEFFICIENT A"; at line 33 is paired with INPUT A at line 34, then PRINT " ";A at line 35. The echo print after INPUT is somewhat redundant since the value is already visible on-screen from the INPUT prompt, but causes no error.
  • Lines 150–160 form a fallthrough handler for invalid yes/no input, printing a message and looping back to line 120. However, line 150 says “ENTER YES OR NO” while the program only accepts single characters “Y” or “N” — a minor mismatch between the prompt text and actual validation logic.
  • Lines 170–180 (SAVE and GOTO 10) are only reachable if execution somehow falls through line 160’s GOTO 120, which cannot happen under normal conditions. They serve as a save-and-restart block intended to be run manually or jumped to directly.

Variable Summary

VariablePurpose
ACoefficient of x² term; also reused as yes/no string variable name prefix
BCoefficient of x term
CConstant term
SDiscriminant (B²−4AC); later repurposed as imaginary coefficient
RSquare root of |S|
A$Single-character user response (“Y” or “N”)

Content

Appears On

Related Products

Find the greatest common denominator, solve the roots of a quadratic equation and linear equations in up to 10 unknowns....

Related Articles

Related Content

Image Gallery

Source Code

  10 CLS
  15 SLOW
  16 DIM A$(1)
  20 PRINT "ROOTS OF QUADRATIC EQUATIONS"
  25 PRINT 
  30 PRINT "        2"
  31 PRINT "WHERE AX  +BX +C = 0"
  32 PRINT 
  33 PRINT "ENTER COEFFICIENT A";
  34 INPUT A
  35 PRINT "  ";A
  36 IF A=0 THEN GOTO 33
  38 PRINT "ENTER COEFFICIENT B";
  39 INPUT B
  40 PRINT "  ";B
  41 PRINT "ENTER COEFFICIENT C";
  45 INPUT C
  46 PRINT "  ";C
  47 FAST
  50 LET S=B*B-4*A*C
  60 LET R=SQR (ABS S)
  70 IF S<0 THEN GOTO 100
  80 PRINT "THE ROOTS ARE REAL AND ARE:"
  85 PRINT (-B-R)/(2*A);" AND ";(R-B)/(2*A)
  90 GOTO 110
 100 PRINT "THE ROOTS ARE COMPLEX NUMBERS:"
 102 LET S=R/(2*A)
 103 PRINT "    ";-B/(2*A);" + ";S;" I"
 104 PRINT "AND ";-B/(2*A);" - ";S;" I"
 110 PRINT 
 115 SLOW
 120 PRINT "DO YOU WANT TO DO ANOTHER ONE?"
 130 INPUT A$
 135 IF A$="Y" THEN RUN 
 140 IF A$="N" THEN STOP
 150 PRINT "ENTER YES OR NO"
 160 GOTO 120
 170 SAVE "QUADRATI[C]"
 180 GOTO 10

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top