--- title: "Addition 1" id: 55178 type: "computer_media" slug: "addition-1" url: "http://localhost/computer_media/addition-1/" markdown_url: "http://localhost/computer_media/addition-1.md" published_at: "2024-06-20T11:49:56+00:00" modified_at: "2026-03-30T21:40:55+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2019/02/20230809-041332.jpg" excerpt: "A currency-style addition program that accumulates entered numbers into a running total, using a string-formatting subroutine that the author himself flagged as broken." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "Algis Gedris" slug: "algis-gedris" taxonomy: "indiv" url: "http://localhost/indiv/algis-gedris/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 55220 title: "Long Island Sinclair Timex (LIST) User Group Library Tape #7" type: "computer_media" url: "http://localhost/computer_media/long-island-sinclair-timex-list-user-group-library-tape-7/" media_type: "Program" programmers: - name: "Algis Gedris" slug: "algis-gedris" taxonomy: "indiv" url: "http://localhost/indiv/algis-gedris/" download_url: "https://archive.org/download/timex-sinclair-software-archive/Addition%201%20%28198x%29%28Gedris%2C%20Algis%29%28TS2068%29%28US%29%28Program%29.zip" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/06/addition-1.png" media_type_tags: "Mathematics" --- # Addition 1 This program implements a simple addition accumulator that prompts the user to enter numbers one at a time and displays a running total when the user enters zero. A subroutine starting at line 5000 formats each number as a currency-style string with two decimal places by manipulating an 8-character string representation produced by STR$. The formatting routine right-justifies the number in an 8-character field, locates the decimal point, and attempts to ensure it falls at position 6 (i.e., two digits after a fixed-width integer portion). The author’s own REM note at line 1 flags the program as not operating correctly, likely due to edge cases in the string-padding and decimal-alignment logic in the subroutine. *** ## Program Analysis ### Program Structure The program is divided into two logical sections: a main input loop (lines 10–80) and a number-formatting subroutine (lines 5000–5080). Execution begins at line 10 with a title display, initializes an accumulator `M` at line 30, then loops at line 40 accepting numeric input. When the user enters `0`, line 70 prints a decorative underline and the total, then halts. Otherwise the formatted value is added to `M` and the loop repeats. ### The Formatting Subroutine (lines 5000–5080) The subroutine at line 5000 attempts to format a number `p` into a fixed 8-character currency string stored in `m$`, with the decimal point always at position 6 (leaving two decimal places). The steps are: 1. Convert `p` to a string with `STR$ p` and store in an 8-character DIM array `m$`. 2. Lines 5010: Right-justify by repeatedly shifting trailing spaces to the front. 3. Lines 5020–5040: Scan left-to-right for the decimal point, leaving `j` at its position (or 9 if none found). 4. Line 5050: If no decimal found (`j=9`), append `".00"` and trim to 8 chars from position 4. 5. Line 5060: If decimal is at position 7, append `"0"` and trim from position 2 (shift left by one). 6. Line 5070: If the decimal is still not at position 6 after adjustments, set error flag `x=1`. ### Key BASIC Idioms - `DIM m$(8)` allocates a fixed 8-character string, initialized to spaces — relied upon by the right-justify loop at line 5010. - `STR$ p` converts the numeric input to its string representation before manipulation. - `LET M=M+VAL m$` at line 50 converts the formatted string back to a number for accumulation, which works correctly only when `m$` contains a valid numeric substring. - The double-quote idiom `""0""` in the PRINT at line 20 embeds a literal quote character in the output string. ### Bugs and Anomalies The author’s own REM at line 1 acknowledges the program does not operate properly. Several issues are apparent: - **Line 45 conditional display:** Line 45 prints `m$` only when `x=0`, but the accumulation at line 50 (`LET M=M+VAL m$`) runs unconditionally regardless of the error flag, meaning malformed values still affect the total. - **Incomplete decimal normalization:** The subroutine only handles the cases where `j=9` (no decimal) or `j=7`. Numbers with the decimal at positions 1–5 or 8 are not corrected; they simply set `x=1` and return with a misaligned string. - **String truncation on append:** Lines 5050 and 5060 use slice notation to trim `m$` after appending characters, but since `m$` is a fixed DIM of 8, appending `".00"` or `"0"` via concatenation produces a longer intermediate string that is then re-sliced — this logic is fragile and implementation-dependent. - **Loop variable `j` after FOR/NEXT:** If the FOR loop at lines 5020–5040 completes without a GO TO, `j` exits as 9 (one past the limit of 8), which is the intended sentinel for “no decimal found.” This is a valid idiom but relies on the post-loop value of the control variable. - **Line 9997 STOP:** This line is unreachable dead code — execution never falls through to it from the subroutine or the main loop. ### Variable Summary | Variable | Purpose | | --- | --- | | `M` | Running numeric total | | `p` | Raw numeric input from user | | `m$` | 8-character formatted string of current number | | `x` | Error flag: 0 = formatted OK, 1 = formatting failed | | `j` | Position of decimal point in `m$` (9 = not found) | ## Source Code ``` 1 REM TO BE MODIFIED. NOT OPERATING PROPERLY. ALGIS E. GEDRIS 10 PRINT TAB 10;"ADDITION 1"'' 20 PRINT TAB 2;"To obtain total press ""0"""'' 30 LET M=0 40 INPUT "ENTER NUMBERS TO BE ADDED: ";p: GO SUB 5000 45 IF x=0 THEN PRINT TAB 10;m$ 50 LET M=M+VAL m$ 70 IF p=0 THEN PRINT TAB 12;"______"'"Total=";TAB 13;M: STOP 80 GO TO 40 5000 LET x=0: DIM m$(8): LET m$=STR$ p 5010 IF m$(8)=CHR$ 32 THEN LET m$=CHR$ 32+m$( TO 7): GO TO 5010 5020 FOR j=1 TO 8 5030 IF m$(j)="." THEN GO TO 5050 5040 NEXT j 5050 IF j=9 THEN LET m$=m$(4 TO )+".00" 5060 IF j=7 THEN LET m$=m$(2 TO )+"0" 5070 IF m$(6)<>"." THEN LET x=1 5080 RETURN 9997 STOP 9998 SAVE "ADDITION" LINE 1 ```