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

2D Arrays

Create a grid in memory with DIM and two dimensions.

6% of Minefield

In Quiz Master, you used DIM to create a list — a one-dimensional array. A grid needs two dimensions: rows and columns. DIM m(5,5) creates a 5-by-5 grid with 25 cells, all starting at zero.

The Program

10 CLS
20 PRINT "2D Arrays"
30 PRINT
40 DIM m(5, 5)
50 LET m(1, 1) = 7
60 LET m(2, 3) = 4
70 LET m(3, 5) = 9
80 LET m(5, 5) = 1
90 PRINT "m(1,1) = "; m(1, 1)
100 PRINT "m(2,3) = "; m(2, 3)
110 PRINT "m(3,5) = "; m(3, 5)
120 PRINT "m(4,4) = "; m(4, 4)
130 PRINT "m(5,5) = "; m(5, 5)
140 PRINT
150 PRINT "Full grid:"
160 FOR r = 1 TO 5
170 FOR c = 1 TO 5
180 PRINT m(r, c); " ";
190 NEXT c
200 PRINT
210 NEXT r

2D array output showing stored values and full grid

How It Works

Line 40 creates the array. DIM m(5,5) allocates 25 cells arranged in 5 rows and 5 columns. Every cell starts at 0.

Lines 50-80 store values at specific positions. m(1,1) is row 1, column 1. m(2,3) is row 2, column 3. The first number is always the row, the second is the column.

Lines 90-130 read individual cells back. m(4,4) was never set, so it returns 0 — the default.

Lines 160-210 use nested FOR loops to print every cell. The outer loop steps through rows, the inner loop steps through columns. This pattern — row loop wrapping column loop — is how you visit every cell in a 2D array.

Row, Column — Always in That Order

Think of it like a spreadsheet. The row number comes first (which horizontal line), then the column number (which position along that line). m(3,5) means “row 3, column 5”.

This convention matters. Swapping them won’t crash the program — m(5,3) is valid — but it will read the wrong cell.

Why 2D Arrays?

A grid game needs grid data. You could use 64 separate variables for an 8x8 board, but that’s impractical. A 2D array lets you access any cell with m(row, column) and loop through the entire grid with nested FOR loops.

Try This

Bigger grid. Change DIM m(5,5) to DIM m(8,8) and fill it with random values: LET m(r,c) = INT(RND*10). Print the full grid.

Checkerboard. Set alternating cells to 1: IF (r+c)/2 = INT((r+c)/2) THEN LET m(r,c) = 1. Print the grid to see the pattern.

What You’ve Learnt

  • DIM with two dimensionsDIM m(5,5) creates a 2D array
  • Addressing cellsm(row, column) reads or writes a specific cell
  • Default values — all cells start at 0
  • Nested loops — outer loop for rows, inner loop for columns