← QCE Digital Solutions Unit 2

Aggregating a stream of records

To aggregate is to combine many records into one summary value, such as a running total, a count of matching records, or an average worked out after the loop.

Understanding Aggregating a stream of records in QCE Digital Solutions Unit 2

What it does: The if limits the counting to matching records, and adding 1 each time combines them into a single count. An aggregate needs a starting value. A total starts at 0 so the first record is added to nothing. With no if, every record adds 1 to the count. The second option only counts records that match the condition. The total and count are only complete once the loop ends. Calculate the average after the loop for the final value. An empty stream leaves count at 0. Check that count > 0 before dividing.

Common mistakes: A running total must start at 0. Starting at 1 makes every final total one too high. It should be total ← 0. The file name is text, so it must be in quotes. It should be marks ← readFile("marks.csv").

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 186–228) and word search (Modules 144–228).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A loop adds each record's price to a variable called total. What does this build?
A running total that combines many records into one valueA copy of every record in the stream
Matches
for each record in records total <- 0 total <- total + record.mark
↓
The total is reset to 0 inside the loop, so every pass wipes out the earlier marks and only the last record is kept.
Applying
Meanings
Term
aggregate
Meaning
To aggregate is to combine many records into one summary value, such as a running total, a count of matching records, or an average worked out after the loop.
Practice
records = [("Ava", 88), ("Liam", 65), ("Mia", 95)]
Testing
Gaps
total ←
Check Answer
Spots
1sales ← readFile("sales.csv")
2total ← 1

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Constellation Wolf' badge — Space Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A loop adds each record's price to a variable called total. What does this build?
A. A copy of every record in the stream
B. A running total that combines many records into one value ✓
Aggregating turns many records into one summary value. A running total starts at 0 and adds each record's value as the loop goes.
2. Which line sets up a running total correctly BEFORE the loop starts?
A. total <- 0 ✓
B. total <- price
The total must start at 0 before the loop. If it starts at a record's price, the first value is counted twice or the total is wrong.
3. You want to count how many records have mark >= 50. Where does count <- count + 1 go?
A. Outside the loop after it finishes
B. Inside an if mark >= 50 inside the loop ✓
Counting matching records means adding 1 only when the condition is true. That needs an if inside the loop.
4. Which code counts ALL the records in the stream, not just the matching ones?
A. for each record: count <- count + 1 ✓
B. for each record: if mark > 50 then count <- count + 1
With no if, every record adds 1 to the count. The second option only counts records that match the condition.
5. When should the average be worked out for a stream of records?
A. After the loop: average <- total / count ✓
B. Inside the loop on every record: average <- total / count
The total and count are only complete once the loop ends. Calculate the average after the loop for the final value.

Match the Pairs

total <- total + record.mark → Adds each record's mark to a running total as the loop visits every record
average <- total / count → Works out the average after the loop has finished, using the total and the number of records
Count only the records that match a condition by adding 1 to a counter inside an if statement → if record.mark >= 50 then count <- count + 1
Set the running total to 0 before the loop so each record can be added to it → total <- 0
for each record in records total <- 0 total <- total + record.mark → The total is reset to 0 inside the loop, so every pass wipes out the earlier marks and only the last record is kept.
for each record in records total <- total + record.mark average <- total / count → The average is calculated inside the loop, before all records have been added and counted, so it is wrong until the last pass.

Key Term

aggregate
To aggregate is to combine many records into one summary value, such as a running total, a count of matching records, or an average worked out after the loop.

Fill in the Blanks

1. total ← 0 for each sale in sales total ← total + sale.amount
Hint: Type the starting value a running total must have before any records are added.
2. total ← 0 for each sale in sales total ← total + sale.amount
Hint: Type the arithmetic operator that adds each record's amount onto the running total.
3. count ← 0 for each s in students if s.mark >= 50 then count ← count + 1
Hint: Type the number added to the counter each time a record matches the condition.
4. end for average ← total / count print average
Hint: Type the variable holding how many records were seen, which the total is divided by after the loop.
5. total ← 0 for each sale in sales total ← total + sale.amount
Hint: Type the loop keywords that visit every record in the stream one at a time.

Spot the Error

Question 1
A sales ← readFile("sales.csv")
B total ← 1
C for each s in sales
D total ← total + s.amount
E print total
Line B — A running total must start at 0. Starting at 1 makes every final total one too high. It should be total ← 0.
Question 2
A marks ← readFile(marks.csv)
B total ← 0
C for each m in marks
D total ← total + m
E print total
Line A — The file name is text, so it must be in quotes. It should be marks ← readFile("marks.csv").
Question 3
A count ← 0
B for each s in students
C if s.mark >= 50 then
D count ← 1
E print count
Line D — This line sets count to 1 each time instead of adding to it, so the count never goes above 1. It should be count ← count + 1.
Question 4
A total ← 0
B for each m in avg
C total ← total + m
D avg ← total / length(marks)
E print avg
Line B — The loop must go through the list of records, marks. The variable avg has not been created yet. It should be for each m in marks.
Question 5
A total ← ""
B for each o in orders
C total ← total + o.price
D avg ← total / length(orders)
E print avg
Line A — The accumulator must start as the number 0, not text. Adding a number to text will not work. It should be total ← 0.

Student with a class code? Join your class →