Arrays, Strings And Data Handling: Updating And Modifying Array Elements (Copy)
Updating And Modifying Array Elements
What “Updating And Modifying Array Elements” Means
- Updating = changing the value stored at an existing index
- Modifying = recalculating values based on rules or conditions
- Very common in:
- Data cleaning
- Adjusting marks
- Applying rules (discounts, bonuses, penalties)
- Normalising values
- Almost always done using loops
Core Examiner Rule (Non-Negotiable)
- An array element can only be updated if:
- It is accessed using a valid index
- The new value matches the array’s data type
- Updates must occur inside traversal loops
Basic Update Syntax (Single Element)
Direct Update
arr[3] ← 50
- Changes only one element
- Rare in exams unless index is explicitly given
Updating All Elements Using A Loop (MOST COMMON)
Example 1: Multiply All Values By 2
FOR i ← 1 TO n
arr[i] ← arr[i] * 2
NEXT i
- Used when:
- Every value must be changed
- Examiner focus:
- Correct loop bounds
- Correct index usage
Example 2: Add Bonus Marks To All Students
FOR i ← 1 TO n
arr[i] ← arr[i] + 5
NEXT i
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
Conditional Updates (MOST TESTED)
When Conditional Updates Are Used
- Only some elements should change
- Decision based on:
- Value range
- Status
- Category
Example 3: Replace Negative Values With Zero
FOR i ← 1 TO n
IF arr[i] < 0 THEN
arr[i] ← 0
ENDIF
NEXT i
- Data cleaning pattern
- Very examiner-friendly
Example 4: Increase Only Passing Marks
FOR i ← 1 TO n
IF arr[i] >= 50 THEN
arr[i] ← arr[i] + 3
ENDIF
NEXT i
Example 5: Cap Maximum Value
- Scenario
- Marks must not exceed 100
FOR i ← 1 TO n
IF arr[i] > 100 THEN
arr[i] ← 100
ENDIF
NEXT i
Updating Based On Index (Position-Based Logic)
Example 6: Update Alternate Elements
FOR i ← 1 TO n STEP 2
arr[i] ← arr[i] * 2
NEXT i
- Updates elements at:
- Index 1, 3, 5, …
Example 7: Update Elements At Even Indices
FOR i ← 2 TO n STEP 2
arr[i] ← arr[i] - 1
NEXT i
Updating Using WHILE Loops
When WHILE Is Appropriate
- Update should stop early
- Update depends on condition becoming false
Example 8: Reduce Values Until Zero
index ← 1
WHILE index <= n AND arr[index] > 0 DO
arr[index] ← arr[index] - 1
index ← index + 1
ENDWHILE
- Pitfall
- Forgetting to increment
index
- Forgetting to increment
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
Updating Arrays Using REPEAT UNTIL
When REPEAT UNTIL Is Used
- Update must happen at least once
- Validation-style logic
Example 9: Force Minimum Value
index ← 1
REPEAT
IF arr[index] < 10 THEN
arr[index] ← 10
ENDIF
index ← index + 1
UNTIL index > n
Updating One Array Using Another Array
Example 10: Copy And Modify
- Scenario
- Create adjusted marks
FOR i ← 1 TO n
adjusted[i] ← marks[i] + 2
NEXT i
- Original array remains unchanged
Example 11: Conditional Copy
FOR i ← 1 TO n
IF marks[i] >= 50 THEN
passMarks[i] ← marks[i]
ELSE
passMarks[i] ← 0
ENDIF
NEXT i
Updating Two-Dimensional Arrays (Tables)
Core Rule
- Nested loops are mandatory
- Update happens at
[row, col]
Example 12: Increase Every Value In A Table
FOR row ← 1 TO r
FOR col ← 1 TO c
table[row, col] ← table[row, col] + 1
NEXT col
NEXT row
Example 13: Conditional Table Update
- Replace negative values with zero
FOR row ← 1 TO r
FOR col ← 1 TO c
IF table[row, col] < 0 THEN
table[row, col] ← 0
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
High-Frequency Exam Patterns For Updates
| Task | Update Type |
|---|---|
| Bonus marks | Add constant |
| Penalty | Subtract constant |
| Data cleaning | Replace invalid values |
| Normalisation | Cap min/max |
| Index-based update | STEP loop |
| Table update | Nested loops |
Common Logic Errors When Updating Arrays
Error 1: Updating Outside Loop
arr[i] ← arr[i] + 1
- But
iis not inside a loop - Causes undefined behaviour
Error 2: Wrong Index Variable
FOR i ← 1 TO n
arr[j] ← arr[j] + 1
NEXT i
jnot changing- Only one element updated repeatedly
Error 3: Missing ELSE Path
IF arr[i] > 0 THEN
arr[i] ← arr[i] - 1
ENDIF
- May be fine or logically incomplete
- Depends on question requirement
Error 4: Modifying Loop Control Variable Instead Of Array
FOR i ← 1 TO n
i ← i + 1
NEXT i
- Dangerous
- Can skip elements or cause infinite loops
Dry-Run Technique For Update Questions
Step-By-Step Method
- Write indices horizontally
- Write original values
- Apply update rule per iteration
- Write updated values after each loop pass
- Confirm no index skipped or repeated
Safe Update Templates (Copy-Paste Ready)
Template 1: Update All Elements
FOR i ← 1 TO n
arr[i] ← <new value using arr[i]>
NEXT i
Template 2: Conditional Update
FOR i ← 1 TO n
IF <condition using arr[i]> THEN
arr[i] ← <updated value>
ENDIF
NEXT i
Template 3: Table Update
FOR row ← 1 TO r
FOR col ← 1 TO c
table[row, col] ← <new value>
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
Examiner Checklist For Full Marks
- Correct loop chosen
- Loop bounds correct
- Correct index used
- Correct update placement
- No out-of-range access
- Data type preserved
One-Line Rules To Memorise
- Updates happen inside loops
- Every index must be valid
- Conditional updates need IF
- Tables need nested loops
- If you change
i, you’re wrong
