HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Arrays and Files in Pseudocode
  1. 1One-dimensional arrays
  2. 2Two-dimensional arrays and nested loops
  3. 3Reading and writing files
  1. Computer Science
  2. /Arrays and Files in Pseudocode
  3. /Two-dimensional arrays and nested loops

Course 8.3·Lesson 2 of 3

Two-dimensional arrays and nested loops

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.

about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
Arrays and Files in Pseudocode
  1. 1One-dimensional arrays
  2. 2Two-dimensional arrays and nested loops
  3. 3Reading and writing files

Try the code and the simulations beside the lesson.

Rows and columns

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.

Declaring and using a 2D array

pseudocode
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 3×4=123 \times 4 = 123×4=12 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.

Nested loops over a 2D array

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.

pseudocode
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 ", Total

With 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.

Practise with new numbers

Try it in Python

The program keeps the sales table as a list of lists and adds it up with nested loops.

Read it first. Then run it. Do the totals match the pseudocode?

1. Change the program to print the total of each column instead. Which loop goes outside?

2. Print sales[2][0]. Which element is it in pseudocode?

Common mistakes

  • The indexes in the wrong order. The row comes first, then the column.
  • Two pairs of brackets in pseudocode. Write Sales[2, 3], not Sales[2][3]. Python uses sales[1][2].
  • Setting a row total to zero in the wrong place. Before both loops it never restarts; inside the inner loop it is wiped at every element.
  • Loop bounds that do not match the array. The row loop uses the row bounds, and the column loop the column bounds.

Remember these

  • DECLARE Name : ARRAY[1:Rows, 1:Columns] OF TYPE.
  • An element is Name[Row, Column].
  • Nested loops visit every element: outer for the rows, inner for the columns.
  • The number of elements is rows times columns.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. In the sales array, which element holds 9?
  1. Write a statement that puts an O in the bottom left square of Board.
  1. How many times does the inner loop body run in the row-total part of the sales program?
  1. What is the total of column 2 of the sales array?
  1. Why is a 2D array better than three separate 1D arrays for the sales?

Enroll free in Arrays and Files in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A small theatre has 5 rows of 8 seats. The program marks booked seats…Easy · High School (9–10) · by StemPy Lab
Three teams play three games each. Points[Team, Game] holds the points of a team in a game…Medium · High School (9–10) · by StemPy Lab
The nine squares of a board are input row by row: X, O, O, then X, X, X, then O, a dot for an empty…Hard · High School (9–10) · by StemPy Lab
Next: lesson 3Reading and writing files
← Arrays and Files in Pseudocode
← One-dimensional arraysReading and writing files →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Python Labs (1)

Arrays with loopsPython Lab · by StemPy Lab