Arrays, Records & Data Handling: Common Off-By-One Errors In Array Indexes (Copy)
Common Off-By-One Errors In Array Indexes (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Off-By-One Errors Are So Costly In Paper 2
- Off-by-one errors occur when:
- An index starts one position too early
- Or ends one position too late
- These errors:
- Break traversal logic
- Cause out-of-range access
- Instantly lose marks even if the rest of the algorithm is correct
- Cambridge examiners treat off-by-one mistakes as:
- Logical errors, not minor slips
Core Cause Of Off-By-One Errors
- Mismatch between:
- Array declaration bounds
- Loop start and end values
- Incorrect assumptions about:
- Whether arrays start at 0 or 1
- Whether loop end values are inclusive
Cambridge Golden Rule For Arrays
- Array bounds are:
- Explicit
- Inclusive
- FOR loops are:
- Inclusive at both start and end
- Any index used:
- Must stay strictly within declared bounds
Most Common Off-By-One Error Categories
- Incorrect loop start value
- Incorrect loop end value
- Wrong initialisation of max/min loops
- Confusing element count with last index
- Incorrect reverse traversal bounds
- Incorrect partial traversal ranges
Error Category 1: Starting Loop At Wrong Index
Typical Declaration
- DECLARE marks[1:30] : INTEGER
Incorrect Traversal
- FOR i ← 0 TO 29
- PROCESS marks[i]
- ENDFOR
Why This Fails
- marks[0] does not exist
- marks[30] is skipped
- Two logical errors in one loop
Correct Traversal
- FOR i ← 1 TO 30
- PROCESS marks[i]
- ENDFOR
Examiner Expectation
- Loop start must match:
- Lower bound exactly
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
Error Category 2: Ending Loop One Step Too Far
Declaration
- DECLARE values[0:9] : INTEGER
Incorrect Loop End
- FOR i ← 0 TO 10
- PROCESS values[i]
- ENDFOR
Why This Fails
- values[10] does not exist
- Out-of-range access
Correct Loop End
- FOR i ← 0 TO 9
- PROCESS values[i]
- ENDFOR
Examiner Focus
- Loop end must:
- Match upper bound exactly
- Do not assume:
- Upper bound equals number of elements
Error Category 3: Confusing Number Of Elements With Last Index
Common Student Assumption
- “There are 30 elements, so last index is 29”
Why This Is Wrong
- Index range depends on declaration
- Example:
- DECLARE marks[1:30]
- Number of elements:
- 30
- Last index:
- 30 (not 29)
Correct Thinking Model
| Declaration | Elements | First Index | Last Index |
|---|---|---|---|
| [1:30] | 30 | 1 | 30 |
| [0:29] | 30 | 0 | 29 |
| [5:34] | 30 | 5 | 34 |
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
Error Category 4: Off-By-One In Maximum And Minimum Algorithms
Incorrect Maximum Pattern
- max ← 0
- FOR i ← 1 TO 30
- IF marks[i] > max THEN
- max ← marks[i]
- ENDIF
- IF marks[i] > max THEN
- ENDFOR
Why This Fails
- If all values are negative:
- max remains incorrect
- Logic depends on assumed data range
Correct Maximum Pattern
- max ← marks[1]
- FOR i ← 2 TO 30
- IF marks[i] > max THEN
- max ← marks[i]
- ENDIF
- IF marks[i] > max THEN
- ENDFOR
Off-By-One Risk
- Starting loop from index 1 instead of 2:
- Causes redundant comparison
- Starting from index 3:
- Skips a value
Same Logic Applies To Minimum
- min ← array[lowerBound]
- Loop starts from:
- lowerBound + 1
Error Category 5: Off-By-One In Reverse Traversal
Declaration
- DECLARE data[1:10] : INTEGER
Incorrect Reverse Loop
- FOR i ← 10 TO 0 STEP -1
- PROCESS data[i]
- ENDFOR
Why This Fails
- data[0] does not exist
- Loop runs one step too far
Correct Reverse Loop
- FOR i ← 10 TO 1 STEP -1
- PROCESS data[i]
- ENDFOR
Examiner Expectation
- Reverse traversal:
- Must still respect declared bounds
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
Error Category 6: Off-By-One In Partial Traversal
Task Example
- “Process the first 10 values of the array”
- DECLARE values[1:30]
Incorrect Interpretation
- FOR i ← 1 TO 9
- PROCESS values[i]
- ENDFOR
Why This Fails
- Only 9 values processed
- One value skipped
Correct Interpretation
- FOR i ← 1 TO 10
- PROCESS values[i]
- ENDFOR
Examiner Trap
- Students miscount inclusive ranges
- Cambridge FOR loops include both ends
Error Category 7: Off-By-One In WHILE Loop Conditions
Incorrect WHILE Traversal
- index ← 1
- WHILE index < 30 DO
- PROCESS marks[index]
- index ← index + 1
- ENDWHILE
Why This Fails
- marks[30] never processed
Correct WHILE Traversal
- index ← 1
- WHILE index <= 30 DO
- PROCESS marks[index]
- index ← index + 1
- ENDWHILE
Examiner Focus
- <= is not optional
- < vs <= decides correctness
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
Error Category 8: Off-By-One In Two-Dimensional Arrays
Declaration
- DECLARE grid[1:5, 1:4] : INTEGER
Incorrect Nested Loops
- FOR r ← 1 TO 4
- FOR c ← 1 TO 3
- PROCESS grid[r,c]
- ENDFOR
- FOR c ← 1 TO 3
- ENDFOR
Why This Fails
- Last row skipped
- Last column skipped
Correct Nested Loops
- FOR r ← 1 TO 5
- FOR c ← 1 TO 4
- PROCESS grid[r,c]
- ENDFOR
- FOR c ← 1 TO 4
- ENDFOR
Examiner Expectation
- Both dimensions:
- Must be fully traversed
- Off-by-one in either dimension:
- Loses marks
Off-By-One Errors In Trace Tables
Common Trace Mistakes
- Missing first iteration
- Stopping one iteration early
- Tracing an index that should not exist
Trace Safety Rule
- Always write:
- First index value
- Last index value
- Then count iterations
Off-By-One Errors In Pre-Release Material
- Pre-release algorithms often specify:
- Exact array sizes
- Maximum limits
- Ignoring these limits:
- Causes boundary violations
- Hardcoding wrong bounds:
- Is a frequent Section B mistake
Off-By-One Errors In Section B Modifications
- Section B commonly asks:
- “Modify the algorithm to process the first N values”
- Safe strategy:
- Identify declared bounds
- Adjust loop end carefully
- Do not change array declaration
Examiner Checklist To Avoid Off-By-One Errors
- Identify array lower bound
- Identify array upper bound
- Match loop start to lower bound
- Match loop end to upper bound
- For max/min:
- Initialise from first element
- Start loop from second element
- For WHILE:
- Use <=, not <
- For reverse loops:
- Check STEP direction
- Check final index
Illegal Practices (Guaranteed Mark Loss)
- Accessing index below lower bound
- Accessing index above upper bound
- Assuming arrays always start at 0 or 1
- Guessing bounds instead of reading declaration
- Using hardcoded values against declared ranges
Best-Practice Strategy For Paper 2
- Read the array declaration first
- Write loop bounds directly from declaration
- Treat FOR loop ends as inclusive
- For WHILE loops, double-check condition
- Manually test first and last iteration
Final Quality Checklist
- Loop start matches lower bound
- Loop end matches upper bound
- No index below or above range
- Max/min logic starts correctly
- Reverse loops terminate correctly
Final Lock-In Rules
- Off-by-one errors are boundary errors
- Boundaries are always explicit in Cambridge pseudocode
- FOR loops are inclusive
- < vs <= is never interchangeable
- One wrong index = lost marks
