Aggregating a stream of records

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Aggregating a stream of records

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which line sets up a running total correctly BEFORE the loop starts?
A total <- 0
B total <- price
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

aggregate

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
total ← _ for each sale in sales total ← total + sale.amount
2.
total ← 0 for each sale in sales total ← total _ sale.amount
3.
count ← 0 for each s in students if s.mark >= 50 then count ← count + _
4.
end for average ← total / _____ print average
5.
total ← 0 ________ sale in sales total ← total + sale.amount

Spot the Error

Shade in the letter of the line that contains an 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
Question 2
A marks ← readFile(marks.csv)
B total ← 0
C for each m in marks
D total ← total + m
E print total
Question 3
A count ← 0
B for each s in students
C if s.mark >= 50 then
D count ← 1
E print count
Question 4
A total ← 0
B for each m in avg
C total ← total + m
D avg ← total / length(marks)
E print avg
Question 5
A total ← ""
B for each o in orders
C total ← total + o.price
D avg ← total / length(orders)
E print avg

Answer Key

Aggregating a stream of records
Section A: Quick Questions
Q1
B — A running total that combines many records into one value
Q2
A — total <- 0
Q3
B — Inside an if mark >= 50 inside the loop
Q4
A — for each record: count <- count + 1
Q5
A — After the loop: average <- total / count
Section B: Match the Pairs
Q1
total <- total + record.mark → Adds each record's mark to a running total as the loop visits every record
Q2
average <- total / count → Works out the average after the loop has finished, using the total and the number of records
Q3
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
Q4
Set the running total to 0 before the loop so each record can be added to it → total <- 0
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
total ← 0 for each sale in sales total ← total + sale.amount
Q2
total ← 0 for each sale in sales total ← total + sale.amount
Q3
count ← 0 for each s in students if s.mark >= 50 then count ← count + 1
Q4
end for average ← total / count print average
Q5
total ← 0 for each sale in sales total ← total + sale.amount
Section E: Spot the Error
Q1
B — A running total must start at 0. Starting at 1 makes every final total one too high. It should be total ← 0.
Q2
A — The file name is text, so it must be in quotes. It should be marks ← readFile("marks.csv").
Q3
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.
Q4
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.
Q5
A — The accumulator must start as the number 0, not text. Adding a number to text will not work. It should be total ← 0.