Score and the Answer Key
Read the questions from DATA, keep score, and check answers against an array — DIM makes a row of numbered boxes, the quiz's answer key. Your first array.
One hard-coded question does not scale — each would need its own IF to check. The fix is the
idea Volume 2 is built on: an array. Store every right answer in a numbered row, and one
line checks them all.
5 DIM k(4)
10 BORDER 0: PAPER 0: INK 7: CLS
50 RESTORE: FOR i = 1 TO 4: READ k(i): NEXT i
55 RESTORE 610
80 LET score = 0: LET n = 0
110 FOR q = 1 TO 4
120 LET n = n + 1
130 CLS
190 READ q$,a$,b$,c$,d$
200 PRINT "Question ";n;" of 4"
210 PRINT q$
220 PRINT "1. ";a$
230 PRINT "2. ";b$
240 PRINT "3. ";c$
250 PRINT "4. ";d$
260 INPUT "Answer (1-4): ";g
270 IF g = k(n) THEN PRINT "Correct!": LET score = score + 1
280 IF g <> k(n) THEN PRINT "The answer was ";k(n)
300 PAUSE 80
310 NEXT q
360 PRINT "You scored ";score;" out of 4"
550 DATA 1,2,3,4
610 DATA "How many legs does a spider have?","Eight","Six","Ten","Twelve"
620 DATA "What is the fastest land animal?","Lion","Cheetah","Horse","Wolf"
640 DATA "Which planet is closest to the Sun?","Venus","Earth","Mercury","Mars"
650 DATA "How many planets orbit the Sun?","Seven","Nine","Ten","Eight"
DIM: a row of numbered boxes
Line 5 — DIM k(4) — makes an array: four numbered boxes, k(1) to k(4), each holding a
number. A plain variable is one box; an array is a labelled row of them, and you reach any one
by its index. Line 50 fills the key from DATA (FOR i = 1 TO 4: READ k(i)), so k holds the
correct option for each question. This is indexed storage — and once you can keep a list of
values addressable by position, a whole class of programs opens up: scores, boards, inventories,
maps.
One check for every question
The round is now a loop. Line 110's FOR q = 1 TO 4 plays four questions; line 190 reads each
question and its options from DATA; and the check is a single line that works for every
question: IF g = k(n) THEN ... score = score + 1. The answer key array means the logic never
mentions a specific answer — it just compares the guess to k(n), the right answer for question
n. Add a question to the DATA, add its answer to the key, and the loop handles it. The score
counter you know from Volume 1; the array is what makes it scale.
Next: group the questions into categories.