Post 8: Introduction to Arrays

Namaste once more, Diwakar here.

At the end of the last post, I mentioned a problem every student eventually runs into: imagine storing the marks of 40 students. Are you really going to write mark1, mark2, mark3… all the way to mark40? I did exactly this once, in a school project, before I'd learned what I'm about to show you — and renaming variables by hand across a 40-line program is not an experience I'd wish on anyone.

The fix is a single idea called an array, and it's the subject of Post 8.

1. What is an array?

Definition: an array is a single variable name that can hold many values at once, each one accessible by a number called an index (or subscript).

Instead of 40 separate variable names, you get one name — say, marks — and 40 numbered slots underneath it: marks(1), marks(2), all the way to marks(40).

2. The row-of-lockers analogy

Analogy: picture a long row of numbered school lockers — locker 1, locker 2, locker 3, and so on. The row has one name ("Class 10 lockers"), but each individual locker holds its own item, and you find any item instantly just by knowing its locker number. An array works exactly the same way: one name for the whole row, one index number per slot.

3. Declaring an array with DIM

syntax.bas
DIM marks(10) AS INTEGER

DIM stands for dimension — it tells QBasic "set aside this many lockers, under this name, for this type of value." Once declared, you reach any slot using its index in brackets: marks(3) = 85 stores 85 in locker number 3.

4. The OPTION BASE trap

This one catches almost every student at least once: by default, DIM marks(10) actually creates 11 lockers, numbered 0 to 10 — not 1 to 10 as you'd expect. If you want your lockers to start counting from 1, like real life, add OPTION BASE 1 as the very first line of your program, before any DIM statement.

base.bas
OPTION BASE 1
DIM marks(10) AS INTEGER
' Now marks(1) through marks(10) exist - exactly 10 lockers

5. Fill and scan an array

Let's watch an array being built and searched, step by step — this is where loops (from the last two posts) and arrays finally meet.

Try it: fill an array, then find the highest mark

First fill the lockers using a simulated FOR loop, then scan them to find the maximum — just like a computer would.

Size:
Click "Fill with FOR loop" to begin.

6. Example 1 (Easy): Store and print 5 marks

basic_array.bas
OPTION BASE 1
DIM marks(5) AS INTEGER

marks(1) = 78
marks(2) = 85
marks(3) = 92
marks(4) = 67
marks(5) = 74

PRINT marks(1), marks(2), marks(3), marks(4), marks(5)

This works, but typing each slot by hand defeats half the purpose of arrays — which is exactly why we pair arrays with loops.

7. Example 2 (Medium): Fill an array using a loop

loop_fill.bas
OPTION BASE 1
DIM marks(5) AS INTEGER

FOR i = 1 TO 5
    PRINT "Enter marks for student"; i; ": ";
    INPUT marks(i)
NEXT i

PRINT "Here are all the marks you entered:"
FOR i = 1 TO 5
    PRINT marks(i)
NEXT i

Now the loop counter i is doing double duty — it's both counting the rounds and pointing at which locker to fill. This is the single most important pattern in this whole post.

8. Example 3 (Harder): Find the highest value

find_max.bas
OPTION BASE 1
DIM marks(5) AS INTEGER

FOR i = 1 TO 5
    INPUT "Enter marks: ", marks(i)
NEXT i

highest = marks(1)
FOR i = 2 TO 5
    IF marks(i) > highest THEN
        highest = marks(i)
    END IF
NEXT i

PRINT "The highest mark is"; highest

The trick here: start by assuming the first value is the highest, then walk through the rest, replacing your answer only when you find something bigger. This "assume, then check everyone else" pattern is exactly what you just watched happen in the locker demo above.

9. Example 4 (Hero level): Average and above-average count

average_and_count.bas
OPTION BASE 1
DIM marks(5) AS INTEGER
total = 0

FOR i = 1 TO 5
    INPUT "Enter marks: ", marks(i)
    total = total + marks(i)
NEXT i

average = total / 5
PRINT "Average marks ="; average

above = 0
FOR i = 1 TO 5
    IF marks(i) > average THEN
        above = above + 1
    END IF
NEXT i

PRINT "Number of students above average:"; above

Notice this needs two separate loops — the first one fills the array and builds the total, but we can't know the average until all marks are in, so counting "who's above average" has to wait for a second pass. This two-pass idea is extremely common once you start working with real data.

10. Common mistakes

  1. Forgetting DIM before using an array — QBasic needs to reserve the lockers before you can put anything in them.
  2. Index out of range — trying to access marks(11) when you only declared DIM marks(10) will crash your program.
  3. Mixing up 0-based and 1-based indexing — remember the OPTION BASE trap from section 4.
  4. Trying to find the average before the loop finishes — you need the complete total first, which means the average calculation must happen after the filling loop, not inside it.

11. Practice tasks

  1. Store 5 subjects' marks in an array and print the total.
  2. Write a program that finds the smallest value in an array of 6 numbers (hint: it's the mirror image of Example 3).
  3. Count how many numbers in an array of 10 are even, using MOD from Post 5.
  4. Challenge: store 3 numbers in an array and arrange them in ascending order, by comparing and swapping pairs using IF statements. (This is the very first idea behind real sorting algorithms — we'll go much deeper into this later in the series.)

Arrays are the point where QBasic starts feeling like it can handle real problems — a whole classroom of marks, a whole week of temperatures, a whole list of anything — all under one name, all reachable through a loop. Combined with IF and FOR/WHILE from the last three posts, you genuinely have enough now to write small, useful programs on your own.

Next time, we'll look at string functions — LEN, MID$, LEFT$, and RIGHT$ — for working with names and text instead of just numbers. Until then, try the locker demo above a few times with different sizes, and then actually type Example 4 into QBasic yourself with your own five marks.

— Diwakar

Comments