Skip to content
Game 8 Unit 3 of 16 1 hr learning time

Array Initialisation

Fill arrays with patterns — zeros, diagonals, and borders.

19% of Minefield

Before placing mines, you need to start with a clean grid. DIM sets every cell to zero, but what if you want a specific pattern? This unit explores different ways to fill an array.

The Program

10 CLS
20 PRINT "Array Initialisation"
30 PRINT
40 DIM m(8, 8)
50 REM fill entire grid with zeros
60 FOR r = 1 TO 8
70 FOR c = 1 TO 8
80 LET m(r, c) = 0
90 NEXT c
100 NEXT r
110 REM set a diagonal to 1
120 FOR i = 1 TO 8
130 LET m(i, i) = 1
140 NEXT i
150 REM set a border to 5
160 FOR i = 1 TO 8
170 LET m(1, i) = 5
180 LET m(8, i) = 5
190 LET m(i, 1) = 5
200 LET m(i, 8) = 5
210 NEXT i
220 REM display
230 FOR r = 1 TO 8
240 FOR c = 1 TO 8
250 PRINT m(r, c);
260 NEXT c
270 PRINT
280 NEXT r

How It Works

Lines 60-100 fill the entire grid with zeros using nested loops. DIM already does this, but the pattern is useful when you need to reset a grid mid-game.

Lines 120-140 set the diagonal — m(1,1), m(2,2), m(3,3) and so on. When the row equals the column, you’re on the diagonal. A single loop handles it because both indices are the same.

Lines 160-210 set the borders. The first and last rows (m(1,i) and m(8,i)) and the first and last columns (m(i,1) and m(i,8)) are set to 5. One loop does all four edges.

Three Patterns, Three Techniques

PatternLoop typeKey idea
Fill allNested FORVisit every cell
DiagonalSingle FORRow equals column
BorderSingle FORFirst/last row and column

The diagonal and border patterns show that you don’t always need nested loops. When you know the relationship between row and column, a single loop is enough.

Try This

Cross pattern. Set the middle row (m(4,c) for c = 1 to 8) and middle column (m(r,4) for r = 1 to 8) to 3. Display the result — you should see a cross.

Random fill. Instead of setting specific patterns, fill each cell with INT(RND*10). This creates a random grid — similar to what you’ll do with mines in the next unit.

What You’ve Learnt

  • Full fill — nested loops to visit every cell
  • Diagonal fill — single loop where row equals column
  • Border fill — setting edges with first/last indices
  • Reset pattern — filling with zeros to clear the grid