Guide 4 of 7

1D & 2D Arrays in Pseudocode: Declaration & Traversal

Stop declaring 50 separate variables. Learn how to store, manage, and traverse large datasets using one-dimensional lists and two-dimensional grids.

Exam Strategy: 1-Based Indexing

The single most common mistake students make with arrays in Cambridge computer science exams is getting the index numbers wrong. In almost all modern programming languages, arrays start at index 0. However, in standard Cambridge Pseudocode, arrays traditionally start at index 1. You must state the upper and lower bounds clearly when declaring an array.

Python vs. Pseudocode Arrays

Python doesn't technically have built-in arrays; it uses dynamic "Lists" that can shrink, grow, and hold mixed data types. Pseudocode arrays are static (fixed size) and homogeneous (hold only one data type).

Concept 🐍 Python (Wrong for Exams) 📘 Cambridge Pseudocode (Correct)
Declaration scores = [] DECLARE Scores : ARRAY[1:5] OF INTEGER
First Element scores[0] = 10 Scores[1] ← 10
Adding Data scores.append(50) Must assign to a specific index:
Scores[2] ← 50

1. 1D Arrays (Lists)

A 1-Dimensional array is like a single row of lockers. When you declare it in standard pseudocode, you must specify the lower bound, the upper bound, and the data type.

1D Array Syntax
// Declare an array that holds 5 integers, indexed 1 to 5
DECLARE Scores : ARRAY[1:5] OF INTEGER

// Assigning data to specific indexes
Scores[1] ← 95
Scores[2] ← 82
Scores[5] ← 100

// Retrieving data
OUTPUT "The first score is: ", Scores[1]

2. Traversing a 1D Array

The true power of arrays comes from combining them with FOR loops. Instead of writing out an operation for every single index, you can use the loop's counter variable to scan through the entire array automatically.

Array Traversal
DECLARE Names : ARRAY[1:3] OF STRING
DECLARE i : INTEGER

Names[1] ← "Alice"
Names[2] ← "Bob"
Names[3] ← "Charlie"

// The loop variable 'i' acts as the index!
FOR i ← 1 TO 3
    OUTPUT "Student ", i, " is ", Names[i]
NEXT i

3. 2D Arrays (Grids)

If a 1D array is a row, a 2D array is a grid (like a spreadsheet or a chessboard). You declare it by providing the bounds for the Rows first, followed by the Columns.

2D Array Syntax
// A grid with 3 rows and 3 columns
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR

// Place an 'X' in Row 1, Column 1
Board[1, 1] ← 'X'

// Place an 'O' in Row 2, Column 3
Board[2, 3] ← 'O'

4. Traversing & Populating a 2D Array (Column-First)

To traverse or populate a 2D array, you must use nested loops (a loop placed inside another loop). When processing tabular data, it is best practice to begin with a column counter in the outer loop and a row counter in the inner loop, moving down through each column first before advancing to the next.

2D Array Traversal (Column-First)
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Col : INTEGER
DECLARE Row : INTEGER

// Outer loop: advances across columns 1 to 4
FOR Col ← 1 TO 4
    // Inner loop: moves down all rows 1 to 3 for the active column
    FOR Row ← 1 TO 3
        OUTPUT Grid[Row, Col]
    NEXT Row
NEXT Col

🤔 Why Should We Begin With a Column Counter?

Starting with the column counter as the outer loop and the row counter as the inner loop is the preferred approach for several key reasons:

  • Category-Wise & Attribute-Wise Data Entry: In most real-world datasets, rows represent individual entities (such as students, experimental trials, or store items), while columns represent specific attributes (such as Test 1, Test 2, or Price). When collecting data, it is far more common to record all results for one category or test across all entities before moving on to the next test.
  • Logical Prompting Flow: When populating an array with user inputs, moving column-by-column lets you display clear, cohesive prompts (e.g. "Enter Test 1 marks for all students") rather than switching contexts back and forth across different categories.
  • Immediate Column-Level Calculations: Moving down an entire column lets you calculate totals, find the maximum or minimum value, or compute an average for that entire category on the fly before advancing to the next column, without needing separate arrays or multiple accumulators running at once.
  • Structured Algorithmic Flow: Moving through the columns first creates a clean, vertical scan through the data table, making it easier to trace, dry-run, and verify against problem specifications.

⚙️ How It Actually Works While Populating

When populating a 2D array column-by-column, the outer loop locks onto the current column, while the inner loop cycles through every row from top to bottom. Here is how the indices change step-by-step for a 3-row by 2-column array:

Step Outer Loop (Col) Inner Loop (Row) Target Element What Happens
1 Col = 1 Row = 1 Scores[1, 1] Input value saved to Row 1 of Column 1
2 Col = 1 Row = 2 Scores[2, 1] Input value saved to Row 2 of Column 1
3 Col = 1 Row = 3 Scores[3, 1] Input value saved to Row 3 of Column 1
(Column 1 is now completely filled!)
4 Col = 2 Row = 1 Scores[1, 2] Outer loop steps to Col 2; Inner loop restarts at Row 1
5 Col = 2 Row = 2 Scores[2, 2] Input value saved to Row 2 of Column 2
6 Col = 2 Row = 3 Scores[3, 2] Input value saved to Row 3 of Column 2
(Column 2 is now completely filled!)

Important Rule on Indexing: Even though the Col loop is on the outside, the array index order is still written as Scores[Row, Col] because the array was declared as ARRAY[1:Rows, 1:Cols]. The outer loop dictates which column is currently active, and the inner loop moves down which row inside that column is being filled.

Populating and Displaying a 2D Array Column-by-Column
// Declare a 2D array for 3 students across 2 subjects
DECLARE StudentMarks : ARRAY[1:3, 1:2] OF INTEGER
DECLARE Col : INTEGER
DECLARE Row : INTEGER

// --- 1. POPULATE THE ARRAY COLUMN BY COLUMN ---
FOR Col ← 1 TO 2
    OUTPUT "--- Entering marks for Subject ", Col, " ---"
    FOR Row ← 1 TO 3
        OUTPUT "Enter mark for Student ", Row, ":"
        INPUT StudentMarks[Row, Col]
    NEXT Row
NEXT Col

// --- 2. TRAVERSE AND DISPLAY THE POPULATED DATA ---
FOR Col ← 1 TO 2
    OUTPUT "--- Results for Subject ", Col, " ---"
    FOR Row ← 1 TO 3
        OUTPUT "Student ", Row, ": ", StudentMarks[Row, Col]
    NEXT Row
NEXT Col

💡 Pro Tip: Outputting the Entire 2D Array as a Table

In PseudoStudio, you can display your entire 2D grid in one step by typing OUTPUT StudentMarks! The live terminal will automatically format it as an aligned ASCII table with row levels (R1, R2...) and column levels (C1, C2...).

Interactive Exam Practice

Scenario: Declare an empty 1D array capable of holding 5 STRING values. Write an algorithm that uses a loop to ask the user to input 5 names, and saves each name sequentially into the array.

Hover or tap the black box to reveal the examiner's solution. Click Run Code to test it instantly!

Tap to Reveal Answer
DECLARE ClassList : ARRAY[1:5] OF STRING
DECLARE Index : INTEGER

FOR Index ← 1 TO 5
    OUTPUT "Enter a student name:"
    INPUT ClassList[Index]
NEXT Index

OUTPUT "Array completely populated!"

📝 Ready to Practice Arrays?

Test your arrays skills with interactive exam-style questions, dry-run trace tables, and automated grading.

⚡ AI Practice: Arrays 💻 50+ Questions 📊 Trace Table Practice