Arrays, Records & Data Handling: Initialising Arrays And Default Value Errors (Copy)
Initialising Arrays And Default Value Errors (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Array Initialisation Is A High-Risk Area In Paper 2
- Array initialisation refers to:
- Giving array elements a known starting value before they are used
- Cambridge tests this to assess:
- Understanding of memory state
- Avoidance of undefined values
- Logical correctness before processing
- Many Paper 2 answers fail because:
- Students assume arrays are automatically initialised
- Cambridge does not make that assumption
Cambridge’s Core Rule On Array Initialisation
- Arrays are NOT automatically initialised
- Every array element:
- Contains an unknown value unless explicitly set
- Using an array element before assigning it:
- Is a logical error
- Leads to incorrect results
- Causes mark loss
What Cambridge Means By “Initialising An Array”
- Initialising an array means:
- Assigning a starting value to each element
- This is required when:
- Array values will be updated
- Array values will be accumulated
- Array values will be compared (max/min)
- Array values will be conditionally modified
When Array Initialisation Is Mandatory
You MUST initialise an array when:
- You plan to:
- Increment elements
- Add values to elements
- Compare against elements
- Update based on conditions
- The array is used:
- Before INPUT fills it completely
- The pre-release material:
- Does not explicitly state values are already stored
When Array Initialisation Is NOT Required
- If every element:
- Is immediately filled using INPUT
- Before any processing occurs
Example (no initialisation needed):
- FOR i ← 1 TO 30
- INPUT marks[i]
- ENDFOR
- marks[i] is defined by INPUT before use
Standard Array Initialisation Pattern (Cambridge-Safe)
One-Dimensional Array Initialisation
- FOR i ← lowerBound TO upperBound
- arrayName[i] ← defaultValue
- ENDFOR
Example: Initialising To Zero
- FOR i ← 1 TO 30
- totalMarks[i] ← 0
- ENDFOR
Examiner Expectations
- Loop bounds:
- Match array declaration
- Default value:
- Matches data type
- Assignment occurs:
- Before array is used
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
Initialising Arrays Of Different Data Types
INTEGER Arrays
- FOR i ← 1 TO n
- count[i] ← 0
- ENDFOR
REAL Arrays
- FOR i ← 1 TO n
- average[i] ← 0.0
- ENDFOR
BOOLEAN Arrays
- FOR i ← 1 TO n
- valid[i] ← FALSE
- ENDFOR
STRING Arrays
- FOR i ← 1 TO n
- name[i] ← “”
- ENDFOR
Examiner Focus
- Default value must:
- Match the array’s data type
- Using wrong default:
- Is a logic error
Initialising Two-Dimensional Arrays
Standard 2D Initialisation Pattern
- FOR r ← rowLower TO rowUpper
- FOR c ← columnLower TO columnUpper
- arrayName[r,c] ← defaultValue
- ENDFOR
- FOR c ← columnLower TO columnUpper
- ENDFOR
Example
- FOR r ← 1 TO 10
- FOR c ← 1 TO 5
- grid[r,c] ← 0
- ENDFOR
- FOR c ← 1 TO 5
- ENDFOR
Examiner Expectations
- Nested loops mandatory
- Both dimensions fully initialised
- Correct row–column 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
Default Value Errors (Very Common Exam Mistakes)
Error 1: Assuming Arrays Start At Zero
Incorrect assumption:
- “All array values are 0 by default”
Why this fails:
- Cambridge pseudocode:
- Makes no such guarantee
- Uninitialised values:
- Are undefined
Error 2: Using Array Elements Before Assignment
Incorrect logic:
- FOR i ← 1 TO 30
- total ← total + marks[i]
- ENDFOR
When:
- marks[] was never initialised or input
Why this fails:
- marks[i] contains unknown values
Correct Approach
Either:
- INPUT values first, OR
- Initialise values first
Error 3: Initialising With Wrong Default Value
Example
- DECLARE minScore[1:30] : INTEGER
Incorrect:
- minScore[i] ← 0
When:
- Scores can be negative
Why this fails:
- Logic depends on incorrect assumptions
Correct Approach
- Initialise from:
- First valid input
- Or first array element after INPUT
Example:
- min ← scores[1]
Error 4: Partial Initialisation
Incorrect
- FOR i ← 1 TO 10
- values[i] ← 0
- ENDFOR
When:
- values declared [1:30]
Why this fails:
- Remaining elements uninitialised
- Leads to inconsistent behaviour
Correct
- FOR i ← 1 TO 30
- values[i] ← 0
- ENDFOR
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
Initialisation And Accumulators (Key Distinction)
- Accumulators:
- Are variables, not arrays
- They must be initialised separately
Example:
- total ← 0
- FOR i ← 1 TO 30
- total ← total + values[i]
- ENDFOR
Common mistake:
- Confusing accumulator initialisation with array initialisation
Initialising Arrays For Conditional Updates
Example: Flag Arrays
- FOR i ← 1 TO 30
- used[i] ← FALSE
- ENDFOR
Later:
- IF used[i] = FALSE THEN
- used[i] ← TRUE
- ENDIF
Examiner Focus
- Boolean arrays must:
- Be initialised explicitly
- TRUE / FALSE:
- Must be used consistently
Initialising Arrays For Counting
Example: Frequency Count
- FOR i ← 1 TO 10
- frequency[i] ← 0
- ENDFOR
Then:
- frequency[value] ← frequency[value] + 1
Without initialisation:
- Increment causes undefined behaviour
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
Initialisation And Trace Tables
- Trace tables assume:
- Variables have defined values
- If array elements are not initialised:
- Trace answers become invalid
- Examiners will:
- Penalise logic, not tracing skill
Trace Safety Rule
Before tracing:
- Identify where array values come from:
- INPUT
- Initialisation
- If neither exists:
- Logic is incorrect
Initialisation In Pre-Release Material
- Pre-release tasks often:
- Declare arrays without filling them
- You must:
- Initialise arrays if you plan to update them
- Never additionally initialise:
- If pre-release explicitly states array already contains data
Initialisation In Section B Modifications
- Section B frequently asks:
- “Modify the algorithm so that…”
- If modification introduces:
- Counting
- Incrementing
- Updating
- Then array initialisation may become necessary
Safe Modification Strategy
- Add initialisation loop
- Place it:
- Before processing
- Do not:
- Remove existing INPUT logic
Example Section B Fix
Original:
- FOR i ← 1 TO 30
- frequency[value[i]] ← frequency[value[i]] + 1
- ENDFOR
Corrected:
- FOR i ← 1 TO 10
- frequency[i] ← 0
- ENDFOR
- FOR i ← 1 TO 30
- frequency[value[i]] ← frequency[value[i]] + 1
- ENDFOR
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
Examiner Checklist To Avoid Default Value Errors
- Has every array element been assigned?
- Is INPUT guaranteed before use?
- Does default value match data type?
- Is full range initialised?
- Are assumptions avoided?
- Is logic traceable?
Best-Practice Strategy For Paper 2
- Never assume default values
- Initialise before update
- Initialise before increment
- Initialise before comparison
- Match initial values to logic purpose
- Trace one iteration mentally
Final Quality Checklist
- Arrays explicitly initialised when required
- No use of undefined elements
- Correct default values chosen
- Full bounds covered
- No hidden assumptions
Final Lock-In Rules
- Arrays are not auto-initialised
- Undefined values cause logical failure
- Initialisation loops prevent silent errors
- Default value choice matters
- Correct initialisation = safe Paper 2 logic
