Course 8.3Lesson 2 of 3
A timetable, a seating plan and a game board all have rows and columns. This lesson declares two-dimensional arrays in Cambridge pseudocode and visits every element with nested loops, as the syllabus example of row totals does.
Try the code and the simulations beside the lesson.
A cinema seat has a row and a number in the row. A game of noughts and crosses has three rows of three squares. One index is not enough to say which element you mean. A two-dimensional array has two indexes: the first for the row, the second for the column.
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
DECLARE Row : INTEGER
DECLARE Column : INTEGER
FOR Row ← 1 TO 3
FOR Column ← 1 TO 3
Board[Row, Column] ← '.'
NEXT Column
NEXT Row
Board[2, 2] ← 'X'
Board[1, 3] ← 'O'ARRAY[1:3, 1:3] gives the bounds of the rows and then of the columns. An element is named with both indexes in one pair of square brackets: Board[2, 2] ← 'X' puts an X in the middle square, and Board[1, 3] is the top right square. The nested loops at the start fill every square with a dot.
![A grid with 3 rows and 4 columns, titled Sales : ARRAY[1:3, 1:4] OF INTEGER. Row 1 holds 5, 8, 2, 6. Row 2 holds 3, 0, 7, 4. Row 3 holds 9, 1, 5, 2. The cell in row 2, column 3, holding 7, is shaded green, with the label Sales[2, 3] = 7.](https://tuygolavdykfydmasiqc.supabase.co/storage/v1/object/public/discussion-images/57b80fd5-0d05-40b0-bb7c-fdadd190c00d/figures/ch08-programming-in-pseudocode-and-python/ics-g3-grid.png)
The picture shows a 2D array of sales: 3 rows and 4 columns, so elements. Sales[2, 3] is in row 2, column 3: it holds 7.
In Python a 2D array is a list of lists. Sales[2, 3] becomes sales[1][2]: one less in each index, and two pairs of brackets.
To visit every element, put one loop inside another. The outer loop takes each row; the inner loop takes each column of that row. This program inputs the twelve values row by row, then outputs each row total and the grand total, like the example in the syllabus.
DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
DECLARE Total : INTEGER
// the 12 values are input row by row
FOR Row ← 1 TO 3
FOR Column ← 1 TO 4
INPUT Sales[Row, Column]
NEXT Column
NEXT Row
Total ← 0
FOR Row ← 1 TO 3
RowTotal ← 0
FOR Column ← 1 TO 4
RowTotal ← RowTotal + Sales[Row, Column]
NEXT Column
OUTPUT "Total for row ", Row, " is ", RowTotal
Total ← Total + RowTotal
NEXT Row
OUTPUT "The grand total is ", TotalWith the twelve inputs 5, 8, 2, 6, then 3, 0, 7, 4, then 9, 1, 5 and 2, the outputs are Total for row 1 is 21, Total for row 2 is 14, Total for row 3 is 17 and The grand total is 52.
Look at where RowTotal ← 0 sits. It is inside the outer loop but before the inner loop, so each row starts again from zero. Total ← 0 is before both loops, because it collects every row.
A column total swaps the loops: the outer loop takes each column, and the inner loop runs down the rows.
The program keeps the sales table as a list of lists and adds it up with nested loops.
Sales[2, 3], not Sales[2][3]. Python uses sales[1][2].DECLARE Name : ARRAY[1:Rows, 1:Columns] OF TYPE.Name[Row, Column].Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Board.Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.