Arrays, Strings And Data Handling: Two-Dimensional Arrays: Table-Based Problems (Copy)
Two-Dimensional Arrays: Table-Based Problems
What A Two-Dimensional Array Is
- A two-dimensional array (2D array) stores data in:
- Rows
- Columns
- Used to represent:
- Tables
- Grids
- Matrices
- Timetables
- Scoreboards
- Each element accessed using two indices:
- Row index
- Column index
Core Structure Of A Two-Dimensional Array
| Feature | Description |
|---|---|
| Data type | Same type for all elements |
| Dimensions | Rows × Columns |
| Access | array[row][column] |
| Storage | Logical table form |
| Processing | Usually nested loops |
Declaring A Two-Dimensional Array
General Declaration Format
DECLARE arrayName : ARRAY[rowStart..rowEnd, colStart..colEnd] OF dataType
Example: Integer Table (5 Rows × 3 Columns)
DECLARE marks : ARRAY[1..5, 1..3] OF INTEGER
- Meaning
- 5 students
- 3 subjects per student
Example: String Table
DECLARE names : ARRAY[1..4, 1..2] OF STRING
- Example use
- Column 1 → First name
- Column 2 → Last name
Visualising A Two-Dimensional Array (Exam Skill)
Example Table: marks
| Row (Student) | Col 1 | Col 2 | Col 3 |
|---|---|---|---|
| 1 | 65 | 72 | 80 |
| 2 | 55 | 60 | 70 |
| 3 | 90 | 88 | 92 |
| 4 | 40 | 50 | 45 |
| 5 | 78 | 85 | 80 |
marks[3,2] = 88marks[5,1] = 78
Assigning Values To A 2D Array
Manual Assignment (Small Tables)
marks[1,1] ← 65
marks[1,2] ← 72
marks[1,3] ← 80
Assignment Using Nested Loops (MOST COMMON)
FOR row ← 1 TO 5
FOR col ← 1 TO 3
INPUT marks[row, col]
NEXT col
NEXT row
- Examiner focus
- Correct nesting order
- Correct loop limits
Accessing And Outputting A 2D Array
Output Entire Table
FOR row ← 1 TO 5
FOR col ← 1 TO 3
OUTPUT marks[row, col]
NEXT col
NEXT row
Output Row By Row (Readable Format)
FOR row ← 1 TO 5
FOR col ← 1 TO 3
OUTPUT marks[row, col], " "
NEXT col
OUTPUT NEWLINE
NEXT row
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 AS Level Computer Science Full Scale Course
Table-Based Problems: Common Exam Scenarios
Scenario 1: Total Marks Per Row (Row Processing)
FOR row ← 1 TO 5
total ← 0
FOR col ← 1 TO 3
total ← total + marks[row, col]
NEXT col
OUTPUT total
NEXT row
- Key idea
- Reset accumulator inside outer loop
Scenario 2: Total Marks Per Column (Column Processing)
FOR col ← 1 TO 3
total ← 0
FOR row ← 1 TO 5
total ← total + marks[row, col]
NEXT row
OUTPUT total
NEXT col
- Common mistake
- Swapping row/column order incorrectly
Scenario 3: Finding Maximum Value In Entire Table
max ← marks[1,1]
FOR row ← 1 TO 5
FOR col ← 1 TO 3
IF marks[row, col] > max THEN
max ← marks[row, col]
ENDIF
NEXT col
NEXT row
- Correct initialisation avoids logic errors
Scenario 4: Counting Values That Meet A Condition
- Example: Count marks ≥ 50
count ← 0
FOR row ← 1 TO 5
FOR col ← 1 TO 3
IF marks[row, col] >= 50 THEN
count ← count + 1
ENDIF
NEXT col
NEXT row
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 AS Level Computer Science Full Scale Course
Table-Based Problem Patterns (Exam-Focused)
| Task | Outer Loop | Inner Loop |
|---|---|---|
| Row totals | Rows | Columns |
| Column totals | Columns | Rows |
| Full table scan | Rows | Columns |
| Search | Rows | Columns |
| Modify values | Rows | Columns |
Searching In A Two-Dimensional Array
Linear Search In Table
found ← FALSE
row ← 1
WHILE row <= 5 AND found = FALSE DO
col ← 1
WHILE col <= 3 AND found = FALSE DO
IF marks[row, col] = target THEN
found ← TRUE
ELSE
col ← col + 1
ENDIF
ENDWHILE
row ← row + 1
ENDWHILE
- Examiner focus
- Bounds protection
- Flag logic
Updating Values In A Table
Example: Increase All Marks By 5
FOR row ← 1 TO 5
FOR col ← 1 TO 3
marks[row, col] ← marks[row, col] + 5
NEXT col
NEXT row
Example: Replace Negative Values With Zero
FOR row ← 1 TO 5
FOR col ← 1 TO 3
IF marks[row, col] < 0 THEN
marks[row, col] ← 0
ENDIF
NEXT col
NEXT row
Common Logic Errors In Table-Based Problems
Error 1: Not Resetting Accumulator
total ← 0
FOR row ← 1 TO 5
FOR col ← 1 TO 3
total ← total + marks[row, col]
NEXT col
OUTPUT total
NEXT row
- Error
- Totals accumulate incorrectly
- Fix
- Reset
totalinside outer loop
- Reset
Error 2: Loop Bounds Mismatch
- Declared
[1..5,1..3] - Looping:
FOR row ← 1 TO 6
- Causes out-of-range access
Error 3: Swapped Indices
marks[col, row]
- Logical error
- Leads to incorrect values
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 AS Level Computer Science Full Scale Course
Dry-Run Technique For 2D Arrays
Step-by-Step Method
- Draw table on paper
- Label rows and columns
- Fill values as inputs occur
- Track changes per loop iteration
- Verify which loop controls rows/columns
Safe 2D Array Template (Copy-Paste)
DECLARE table : ARRAY[1..r, 1..c] OF INTEGER
FOR row ← 1 TO r
FOR col ← 1 TO c
INPUT table[row, col]
NEXT col
NEXT row
Examiner Checklist For Full Marks
- Correct 2D declaration syntax
- Correct use of two indices
- Proper nested loop structure
- Correct accumulator placement
- No out-of-range access
- Clear row vs column logic
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 AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Tables need nested loops
- Outer loop = major grouping
- Inner loop = detailed processing
- Reset totals correctly
- Row/column order matters
