Algebra 1 – Roots of Quadratic Equations

Products: Algebra I
Date: 198x
Type: Cassette
Platform(s): TS 1000

This program solves quadratic equations of the form AX² + BX + C = 0, computing both real and complex roots using the quadratic formula. It calculates the discriminant (B²−4AC) stored in variable S, then uses SQR(ABS S) to safely extract the square root regardless of sign, branching at line 70 to handle the complex-root case. For real roots, both solutions are printed directly; for complex roots, the real part (−B/2A) and imaginary part (R/2A) are displayed separately with “I” notation. The program uses SLOW mode for user interaction and FAST mode during calculation, and validates that coefficient A is non-zero before proceeding.


Program Analysis

Program Structure

The program flows through four distinct phases: input collection (lines 10–46), discriminant calculation (lines 47–60), root display (lines 70–110), and repeat-or-quit logic (lines 110–160). Lines 170–180 are a save/restart stub that is never reached during normal execution.

  1. Lines 10–46: Display the equation form, collect coefficients A, B, and C with inline echo-printing, and validate that A ≠ 0.
  2. Lines 47–60: Switch to FAST mode, compute discriminant S = B*B - 4*A*C, and compute R = SQR(ABS S).
  3. Lines 70–110: Branch on the sign of S; print real roots or complex conjugate pair accordingly.
  4. Lines 110–160: Prompt for another calculation; accept “Y” to restart via RUN or “N” to STOP, with a fallback message for other input.

Mathematical Approach

The discriminant S = B² − 4AC is computed at line 50. Taking SQR(ABS S) at line 60 avoids a runtime error when S is negative (which would otherwise cause a square-root-of-negative error), storing the magnitude of the root in R. The sign of S is then tested at line 70 to decide which formula to apply.

For real roots (S ≥ 0), the two solutions are printed inline at line 85 as (-B-R)/(2*A) and (R-B)/(2*A). For complex roots (S < 0), line 102 repurposes S to hold R/(2*A) — the imaginary coefficient — and lines 103–104 print the conjugate pair in the form a + b I / a - b I.

Key BASIC Idioms

  • SLOW/FAST switching: SLOW is set at line 15 for readable I/O, FAST at line 47 for the arithmetic, then SLOW again at line 115 before the repeat prompt — a standard pattern to avoid flicker during calculation.
  • Input echo: After each INPUT, the entered value is re-printed with leading spaces (e.g., line 35 PRINT " ";A), compensating for the lack of inline input display in some display modes.
  • Single-character string input: DIM A$(1) at line 16 declares a one-character string for the yes/no response, avoiding wasted memory.
  • Variable reuse: S is first used as the discriminant, then overwritten at line 102 with the imaginary coefficient — economical but potentially confusing if the program were extended.

Notable Techniques

The RUN statement at line 135 (without a line number) restarts the entire program from the beginning, which reinitializes all variables. This is simpler than a GO TO 10 loop but has the side effect of clearing all variables, including any that might be reused — in this program that is harmless since fresh coefficients are always requested.

Bugs and Anomalies

  • The boundary case S = 0 (a repeated real root) is handled correctly by the real-root branch, and both printed values will be identical, which is mathematically accurate.
  • The yes/no validation loop at lines 150–160 sends control back to line 120 to re-prompt, but does not prevent the user from entering multi-character strings — only the first character matters given DIM A$(1).
  • Lines 170–180 (SAVE followed by GOTO 10) are unreachable under normal execution, as no code path leads there. They appear to be a maintenance artifact left in the listing.
  • The superscript “2” in the equation display is faked by printing " 2" on a separate line above "WHERE AX +BX +C = 0" (lines 30–31), a common workaround for the lack of superscript characters.

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