Lesson contents

Grids and matrices

A grid locates an item with two coordinates. Pick a row, then a column within that row.

Simple example

The top row contains 4 and 7. The bottom row contains 2 and 9. Store the rows one after the other. Skip the two cells of the top row, then move one position across to print 9.

Start4,7 2,9
Result9
lesson.jl
function main()
    cells = [4, 7, 2, 9]
    columns = 2
    row = 1
    column = 1
    println(cells[1 + ((row * columns) + column)])
end

main()
Output
9
Advertisements

Everyday examples

Read a cinema seat map

In two cinema rows the ticket numbers are 11, 12 and 21, 22. Which ticket is in the second seat of the back row?

Start11,12 21,22
Result22

Select the back row first. Its second seat contains ticket 22.

Water one garden bed

The moisture readings in two garden rows are 3, 6 and 5, 8. Which reading belongs to the front-left bed?

Start3,6 5,8
Result3

The front row is listed first and the left bed is its first column. Read 3.

Find a timetable entry

Two days each have morning and afternoon classes. Their room numbers are 5, 8 and 6, 9. Where is the second-day morning class?

Start5,8 6,9
Result6

Use the second day's row and the morning column. Room 6 is the intersection.

Cost and limits

Check both coordinates. With a rectangular array, a valid cell is directly accessible. A jagged list can have different row lengths.