Arrays, Records & Data Handling: Two-Dimensional Arrays: Row–Column Logic (Copy)
Two-Dimensional Arrays: Row–Column Logic (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of Two-Dimensional Arrays In Cambridge Pseudocode
- Two-dimensional arrays are used to:
- Store data in rows and columns
- Represent tables, grids, matrices, or grouped records
- Cambridge tests 2D arrays to assess:
- Understanding of row–column structure
- Correct nested loop logic
- Safe indexing in both dimensions
- Two-dimensional arrays appear frequently in:
- Pre-release material
- Section B algorithm modifications
- Trace table and dry-run questions
What Cambridge Means By A Two-Dimensional Array
- A two-dimensional array is:
- An array of arrays
- Accessed using two indices
- Structure:
- First index → row
- Second index → column
- All elements:
- Must be of the same data type
Core Two-Dimensional Array Declaration Structure
- DECLARE arrayName[rowLower:rowUpper, columnLower:columnUpper] : dataType
Declaration Rules
- DECLARE keyword is mandatory
- Both row and column ranges:
- Must be explicitly stated
- Index bounds:
- Are inclusive
- Data type:
- Applies to every cell
Example Declarations
- DECLARE marks[1:30, 1:5] : INTEGER
- DECLARE seating[1:10, 1:6] : STRING
- DECLARE temperature[0:6, 0:23] : REAL
- DECLARE attendance[1:12, 1:31] : BOOLEAN
Understanding Row–Column Meaning
- First index:
- Identifies the row
- Second index:
- Identifies the column
Example:
- marks[3,2]
- Row 3
- Column 2
Real-World Interpretation
| Array | Row Meaning | Column Meaning |
|---|---|---|
| marks | Student | Subject |
| seating | Row of seats | Seat number |
| timetable | Day | Period |
| sales | Product | Month |
- Cambridge expects:
- Logical interpretation of indices
- Consistency throughout the algorithm
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Accessing Elements In Two-Dimensional Arrays
Standard Access Syntax
- arrayName[rowIndex, columnIndex]
Examples
- marks[1,1]
- seating[row, column]
- temperature[day, hour]
Assigning Values
- marks[5,3] ← 78
- seating[2,4] ← “Ali”
Reading Values
- total ← total + marks[i,j]
- OUTPUT temperature[day, hour]
Nested Loop Structure For Row–Column Processing (Most Tested Area)
Standard Traversal Pattern
- FOR row ← rowLower TO rowUpper
- FOR column ← columnLower TO columnUpper
- PROCESS arrayName[row, column]
- ENDFOR
- FOR column ← columnLower TO columnUpper
- ENDFOR
Key Rule
- Outer loop:
- Controls rows
- Inner loop:
- Controls columns
- Reversing this logic:
- Usually causes incorrect processing
Example: Summing All Values
- total ← 0
- FOR r ← 1 TO 30
- FOR c ← 1 TO 5
- total ← total + marks[r,c]
- ENDFOR
- FOR c ← 1 TO 5
- ENDFOR
Examiner Expectations
- Correct nesting order
- Distinct loop variables
- Loop bounds match array declaration
Common Row–Column Logic Errors
Error 1: Swapping Indices
- marks[column,row]
- Changes meaning completely
Error 2: Using Single Loop For 2D Array
- FOR i ← 1 TO 30
- PROCESS marks[i]
- ENDFOR
- Invalid for two-dimensional arrays
Error 3: Reusing Same Loop Variable
- FOR i ← 1 TO 10
- FOR i ← 1 TO 5
- ENDFOR
- Inner loop overwrites outer loop variable
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Inputting Data Into Two-Dimensional Arrays
Standard Input Pattern
- FOR row ← rowLower TO rowUpper
- FOR column ← columnLower TO columnUpper
- INPUT arrayName[row, column]
- ENDFOR
- FOR column ← columnLower TO columnUpper
- ENDFOR
Example
- FOR s ← 1 TO 30
- FOR sub ← 1 TO 5
- INPUT marks[s, sub]
- ENDFOR
- FOR sub ← 1 TO 5
- ENDFOR
Examiner Focus
- INPUT must be inside inner loop
- Each iteration inputs exactly one value
- No skipped cells
Outputting Data From Two-Dimensional Arrays
Standard Output Pattern
- FOR row ← rowLower TO rowUpper
- FOR column ← columnLower TO columnUpper
- OUTPUT arrayName[row, column]
- ENDFOR
- FOR column ← columnLower TO columnUpper
- ENDFOR
Formatting Output (Optional)
- OUTPUT arrayName[row, column], ” “
- Formatting is:
- Not graded heavily
- Logic is the priority
Processing Rows Or Columns Separately
Processing One Row At A Time
- FOR column ← columnLower TO columnUpper
- total ← total + marks[rowNumber, column]
- ENDFOR
Processing One Column At A Time
- FOR row ← rowLower TO rowUpper
- total ← total + marks[row, columnNumber]
- ENDFOR
Examiner Expectation
- Correct index fixed
- Other index iterated
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Two-Dimensional Arrays And Selection Logic
Conditional Processing Example
- FOR r ← 1 TO 30
- FOR c ← 1 TO 5
- IF marks[r,c] >= 50 THEN
- passCount ← passCount + 1
- ENDIF
- IF marks[r,c] >= 50 THEN
- ENDFOR
- FOR c ← 1 TO 5
- ENDFOR
Examiner Focus
- IF does not affect loop structure
- Both loops still run fully
- Selection filters processing
Two-Dimensional Arrays And Accumulators
Correct Accumulator Pattern
- sum ← 0
- FOR r ← 1 TO rows
- FOR c ← 1 TO columns
- sum ← sum + data[r,c]
- ENDFOR
- FOR c ← 1 TO columns
- ENDFOR
Common Accumulator Errors
- Initialising accumulator inside loops
- Resetting accumulator per row unintentionally
- Using wrong index order
Trace Tables With Two-Dimensional Arrays
How Cambridge Tests Tracing
- Trace tables may show:
- Row index
- Column index
- Current array value
- Updated variables
Trace Table Rule
- Inner loop completes fully:
- Before outer loop increments
Common Trace Errors
- Advancing row too early
- Skipping column iterations
- Misreading index order
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Two-Dimensional Arrays In Pre-Release Material
- Pre-release tasks commonly use:
- 2D arrays to store grouped data
- You must:
- Respect declared dimensions
- Preserve row–column meaning
- Avoid hardcoding limits
Two-Dimensional Arrays In Section B Modifications
- Section B often asks to:
- Add validation per cell
- Process subset of rows or columns
- Add conditional logic inside nested loops
Safe Modification Strategy
- Keep nested loop structure
- Insert IF inside inner loop
- Do not change array dimensions
Example Modification
Original:
- FOR r ← 1 TO 30
- FOR c ← 1 TO 5
- total ← total + marks[r,c]
- ENDFOR
- FOR c ← 1 TO 5
- ENDFOR
Modified:
- FOR r ← 1 TO 30
- FOR c ← 1 TO 5
- IF marks[r,c] >= 0 AND marks[r,c] <= 100 THEN
- total ← total + marks[r,c]
- ENDIF
- IF marks[r,c] >= 0 AND marks[r,c] <= 100 THEN
- ENDFOR
- FOR c ← 1 TO 5
- ENDFOR
Illegal Two-Dimensional Array Practices (Guaranteed Mark Loss)
- Using one index instead of two
- Swapping row and column meaning mid-algorithm
- Exceeding declared bounds
- Using same loop variable twice
- Ignoring nested loop structure
Best-Practice Strategy For 2D Arrays In Paper 2
- Clearly define row and column meaning
- Always use nested loops
- Use different variables for row and column
- Match loop bounds to declaration
- Keep selection inside inner loop
- Trace one row manually to confirm logic
Final Quality Checklist
- Correct declaration with two dimensions
- Clear row–column interpretation
- Proper nested loops
- Safe indexing
- No boundary violations
- Logic traceable step-by-step
Final Lock-In Rules
- First index = row, second = column
- Nested loops are mandatory
- Inner loop completes before outer loop increments
- Most errors are index-order errors
- Clean row–column logic = strong Paper 2 performance
