Arrays, Records & Data Handling: Counting, Totalling And Finding Maximum/Minimum Values (Copy)
Counting, Totalling And Finding Maximum/Minimum Values (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why These Algorithms Are Core In Paper 2
- Counting, totalling, maximum and minimum algorithms are:
- The most frequently tested array-processing patterns
- Cambridge uses these to test:
- Correct traversal
- Proper initialisation
- Accurate conditional logic
- These patterns appear in:
- Pre-release material
- Section B algorithm modifications
- Trace tables
- Pseudocode completion questions
Core Principle Shared By All These Tasks
- All four operations require:
- Traversing an array using a loop
- Updating one or more variables conditionally
- The loop structure is almost always identical
- Only the update logic changes
Counting Values In An Array
Purpose Of Counting
- Counting is used to:
- Count how many values meet a condition
- Examples:
- Number of students who passed
- Number of valid inputs
- Number of records matching a criterion
Standard Counting Pattern (Cambridge-Safe)
- count ← 0
- FOR i ← lowerBound TO upperBound
- IF condition(arrayName[i]) THEN
- count ← count + 1
- ENDIF
- IF condition(arrayName[i]) THEN
- ENDFOR
Example: Counting Pass Marks
- passCount ← 0
- FOR i ← 1 TO 30
- IF marks[i] >= 50 THEN
- passCount ← passCount + 1
- ENDIF
- IF marks[i] >= 50 THEN
- ENDFOR
Examiner Expectations
- Counter initialised before loop
- Increment by exactly 1
- IF inside loop
- Loop runs full range
Common Counting Errors
- Initialising count inside loop
- Forgetting to increment
- Incrementing by wrong value
- Using IF without a loop
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
Totalling (Summing) Values In An Array
Purpose Of Totalling
- Totalling is used to:
- Add values together
- Examples:
- Total marks
- Total sales
- Sum of temperatures
Standard Totalling Pattern
- total ← 0
- FOR i ← lowerBound TO upperBound
- total ← total + arrayName[i]
- ENDFOR
Example: Total Marks
- totalMarks ← 0
- FOR i ← 1 TO 30
- totalMarks ← totalMarks + marks[i]
- ENDFOR
Examiner Expectations
- Accumulator initialised before loop
- Update occurs every iteration (unless filtered)
- Correct variable used in addition
Totalling With Condition (Filtered Total)
- total ← 0
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- total ← total + marks[i]
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Common Totalling Errors
- Initialising total inside loop
- Adding wrong variable
- Forgetting condition when required
- Resetting total accidentally
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
Finding Maximum Value In An Array
Purpose Of Maximum Algorithm
- Used to find:
- Highest mark
- Largest value
- Maximum temperature
Standard Maximum Pattern (Cambridge-Approved)
- max ← arrayName[lowerBound]
- FOR i ← lowerBound + 1 TO upperBound
- IF arrayName[i] > max THEN
- max ← arrayName[i]
- ENDIF
- IF arrayName[i] > max THEN
- ENDFOR
Example: Highest Mark
- highest ← marks[1]
- FOR i ← 2 TO 30
- IF marks[i] > highest THEN
- highest ← marks[i]
- ENDIF
- IF marks[i] > highest THEN
- ENDFOR
Examiner Expectations
- max initialised to first array element
- Loop starts from second element
- Comparison uses correct operator (>)
Common Maximum Errors
- Initialising max to 0 incorrectly
- Starting loop from wrong index
- Using < instead of >
- Resetting max inside loop
Finding Minimum Value In An Array
Purpose Of Minimum Algorithm
- Used to find:
- Lowest mark
- Smallest value
- Minimum temperature
Standard Minimum Pattern
- min ← arrayName[lowerBound]
- FOR i ← lowerBound + 1 TO upperBound
- IF arrayName[i] < min THEN
- min ← arrayName[i]
- ENDIF
- IF arrayName[i] < min THEN
- ENDFOR
Example: Lowest Mark
- lowest ← marks[1]
- FOR i ← 2 TO 30
- IF marks[i] < lowest THEN
- lowest ← marks[i]
- ENDIF
- IF marks[i] < lowest THEN
- ENDFOR
Examiner Expectations
- Initialisation from first element
- Correct comparison operator (<)
- Full traversal of array
Common Minimum Errors
- Initialising min to a large constant
- Using > instead of <
- Skipping first element
- Confusing min with max 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
Combining Counting And Totalling
Example: Counting And Totalling Valid Values
- count ← 0
- total ← 0
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- count ← count + 1
- total ← total + marks[i]
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Examiner Focus
- Multiple variables updated safely
- Correct initialisation of each
- IF groups related updates
Combining Maximum / Minimum With Conditions
Example: Highest Passing Mark
- highest ← -1
- FOR i ← 1 TO 30
- IF marks[i] >= 50 AND marks[i] > highest THEN
- highest ← marks[i]
- ENDIF
- IF marks[i] >= 50 AND marks[i] > highest THEN
- ENDFOR
Examiner Expectation
- Condition combines:
- Validity
- Comparison
- Correct initial value chosen deliberately
Counting, Totalling And Trace Tables
How These Appear In Trace Questions
- Trace tables usually include:
- Index
- Current array value
- count / total / max / min
Trace Behaviour
- count:
- Changes only when condition TRUE
- total:
- Accumulates steadily
- max/min:
- Changes occasionally
Common Trace Errors
- Updating values every iteration incorrectly
- Forgetting initial values
- Confusing old and new max/min 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 O Level And IGCSE Computer Science Full Scale Course
Counting, Totalling And Pre-Release Material
- Pre-release tasks often require:
- Multiple passes through arrays
- Conditional totals
- Highest or lowest value tracking
- You must:
- Follow task order
- Use correct variable names
- Respect data constraints
Counting, Totalling And Section B Modifications
- Section B commonly asks to:
- Add extra conditions
- Ignore invalid values
- Track both count and total
- Safe strategy:
- Add IF inside existing loop
- Do not change loop bounds
Example Modification
Original:
- total ← 0
- FOR i ← 1 TO 30
- total ← total + marks[i]
- ENDFOR
Modified:
- count ← 0
- total ← 0
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- count ← count + 1
- total ← total + marks[i]
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Illegal Practices (Guaranteed Mark Loss)
- Initialising counters or totals inside loops
- Accessing out-of-range indices
- Using incorrect comparison operators
- Forgetting to initialise max/min properly
- Mixing logic of max and min
Best-Practice Strategy For Paper 2
- Identify which pattern is required:
- Count
- Total
- Maximum
- Minimum
- Initialise variables clearly
- Traverse array once where possible
- Combine logic carefully inside IF
- Trace one full run mentally
Final Quality Checklist
- Correct initial values
- Correct loop bounds
- Correct comparison operators
- Updates placed inside loop
- Logic matches task description
Final Lock-In Rules
- Counting increments by 1
- Totalling accumulates values
- Max/min start from first element
- IF filters processing, not looping
- Clean accumulator logic = guaranteed Paper 2 marks
