Standard Algorithm & Logic Patterns: Counting And Accumulation Algorithms (Copy)
Counting And Accumulation Algorithms
What Counting And Accumulation Mean (AS Level Focus)
- These are foundational algorithm patterns
- Used whenever a problem asks:
- “How many…”
- “How much in total…”
- “Find the sum / total / count”
- Almost every real-world and exam problem is built on:
- Counters
- Accumulators
Core Examiner Rule (Absolutely Critical)
- Counting and accumulation must be done correctly and separately
- Mixing them up is a high-frequency mark loss
- Examiners look for:
- Correct initialisation
- Correct update logic
- Correct placement inside loops
Definitions (Exam-Precise)
Counter
- A variable that:
- Tracks how many times something occurs
- Usually increases by 1
- Typical initial value:
0
Accumulator
- A variable that:
- Stores a running total
- Increases by adding a value
- Typical initial value:
0
Counting Algorithm: Core Pattern
When To Use
- Questions with phrases like:
- “How many students…”
- “Count the number of…”
- “How many values satisfy…”
Standard Counting Template
count ← 0
FOR i ← 1 TO n
INPUT value
IF condition THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Accumulation Algorithm: Core Pattern
When To Use
- Questions with phrases like:
- “Find the total…”
- “Calculate the sum…”
- “Total distance / marks / sales”
Standard Accumulation Template
total ← 0
FOR i ← 1 TO n
INPUT value
total ← total + value
NEXT i
OUTPUT total
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
Combining Counting And Accumulation (Very Common)
Typical Use Case
- Average calculation
- Conditional totals
Pattern: Average Of Selected Values
total ← 0
count ← 0
FOR i ← 1 TO n
INPUT value
IF value > 50 THEN
total ← total + value
count ← count + 1
ENDIF
NEXT i
IF count > 0 THEN
average ← total / count
OUTPUT average
ENDIF
Examiner Note
- Division by
count, notn count > 0prevents division by zero
Counting Without Arrays (One-Pass Logic)
When This Is Preferred
- Data used once
- No need to store values
Example: Count Values Greater Than 10
count ← 0
FOR i ← 1 TO 8
INPUT num
IF num > 10 THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Accumulation With Arrays
When Arrays Are Needed
- Values reused later
- Multiple passes required
Example: Sum Of Array Values
DECLARE nums : ARRAY[1..5] OF INTEGER
total ← 0
FOR i ← 1 TO 5
INPUT nums[i]
NEXT i
FOR i ← 1 TO 5
total ← total + nums[i]
NEXT i
OUTPUT total
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 Accumulation (High-Frequency Exam Pattern)
Example: Total Of Even Numbers
total ← 0
FOR i ← 1 TO 10
INPUT num
IF num MOD 2 = 0 THEN
total ← total + num
ENDIF
NEXT i
OUTPUT total
Conditional Counting (High-Frequency Exam Pattern)
Example: Count Failed Students
failCount ← 0
FOR i ← 1 TO 25
INPUT mark
IF mark < 50 THEN
failCount ← failCount + 1
ENDIF
NEXT i
OUTPUT failCount
Counting And Accumulation With Strings
Example: Count Vowels In A String
count ← 0
INPUT text
FOR i ← 1 TO LENGTH(text)
IF text[i] = "A" OR text[i] = "E" OR text[i] = "I" OR
text[i] = "O" OR text[i] = "U" THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Accumulation With Repeated Input Until Condition
Example: Sum Until Sentinel Value
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
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
Nested Counting And Accumulation (Advanced But Testable)
Example: Total Marks For Multiple Classes
grandTotal ← 0
FOR class ← 1 TO 3
classTotal ← 0
FOR student ← 1 TO 10
INPUT mark
classTotal ← classTotal + mark
NEXT student
grandTotal ← grandTotal + classTotal
NEXT class
OUTPUT grandTotal
Common Counting And Accumulation Errors (Exam Traps)
Error 1: Not Initialising
total ← total + num
totalmust start at0
Error 2: Updating Outside Loop
FOR i ← 1 TO 10
INPUT num
NEXT i
count ← count + 1
- Only increments once
Error 3: Wrong Variable Incremented
count ← count + num
- Counter should increase by
1, notnum
Error 4: Dividing By Wrong Value
average ← total / n
- Wrong if only some values counted
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
Counting vs Accumulation (Quick Comparison)
| Feature | Counting | Accumulation |
|---|---|---|
| Purpose | How many | How much |
| Update | +1 | +value |
| Initial value | 0 | 0 |
| Common variable name | count | total |
| Used with IF | Very common | Very common |
Examiner Language You Should Use
- “A counter is used to track the number of occurrences”
- “An accumulator stores the running total”
- “The variable is initialised before the loop”
- “The update occurs inside the loop”
Exam-Ready Combined Template (Safe For Most Questions)
count ← 0
total ← 0
FOR i ← 1 TO n
INPUT value
IF condition THEN
count ← count + 1
total ← total + value
ENDIF
NEXT i
IF count > 0 THEN
OUTPUT total
OUTPUT count
ENDIF
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
- Counters count occurrences
- Accumulators add values
- Always initialise before looping
- Update inside the loop
- Divide by what you counted, not total inputs
