Total, count, maximum, minimum and average in one loop

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

Total, count, maximum, minimum and average in one loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Total, Count, Max and Min be set up in a combined algorithm?
A Inside the loop, at the top of each pass
B Before the loop starts
2. Which is the correct place to calculate the average?
A After the loop: Average ← Total / Count
B Inside the loop: Average ← Total / Count on every pass
3. Which pair of statements belongs INSIDE the loop for each item?
A Total ← 0 and Count ← 0
B Total ← Total + Mark and Count ← Count + 1
4. Which is a sensible way to start Max and Min for marks from 0 to 100 stored in an array?
A Max ← Marks[1] and Min ← Marks[1]
B Max ← 100 and Min ← 0
5. After a single pass over the marks 40, 75, 60, what are Total and Count?
A Total = 175, Count = 3
B Total = 75, Count = 3

Match the Pairs

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

Left columnRight column
1Total ← 0 Count ← 0 Max ← Mark[1] Min ← Mark[1] ATotal is set to 0 inside the loop, so it is reset on every pass and ends up holding only the last item. It must be initialised before the loop.
2Average ← Total / Count BInitialise every variable before the loop starts, with Max and Min set to the first item
3Inside the one loop, the total and the counter are both updated for every item CThe average is calculated inside the loop, before all the items have been added, so it is worked out too early. It should be calculated once after NEXT i.
4Inside the same loop, maximum and minimum are updated using IF statements that compare each item DTotal ← Total + Mark[i] Count ← Count + 1
5FOR i ← 1 TO 5 Total ← 0 Total ← Total + Mark[i] NEXT i ECalculate the average after the loop has finished, using the final total and count
6FOR i ← 1 TO 5 Total ← Total + Mark[i] Average ← Total / 5 NEXT i OUTPUT Average FIF Mark[i] > Max THEN Max ← Mark[i] IF Mark[i] < Min THEN Min ← Mark[i]

Definitions

single pass

Fill in the Blanks

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

1.
Total ← 0 Count ← _ Max ← Marks[1]
2.
FOR i ← 1 TO 10 Total ← Total + _____[i] Count ← Count + 1
3.
IF Marks[i] _ Max THEN Max ← Marks[i] ENDIF
4.
IF Marks[i] < Min ____ Min ← Marks[i] ENDIF
5.
NEXT i Average ← Total _ Count OUTPUT Average

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Total ← 0
B Count ← 1
C Max ← Mark[1]
D Min ← Mark[1]
E FOR i ← 1 TO 10
F Total ← Total + Mark[i]
Question 2
A FOR i ← 1 TO 5
B Total ← Total + Score[i]
C Count ← Count + Score[i]
D IF Score[i] > Max THEN
E Max ← Score[i]
F ENDIF
Question 3
A FOR i ← 1 TO 8
B Total ← Total + Mark[i]
C IF Mark[i] < Min THEN
D Max ← Mark[i]
E ENDIF
F NEXT i
Question 4
A Min ← 0
B FOR i ← 2 TO 10
C IF Mark[i] < Min THEN
D Min ← Mark[i]
E ENDIF
F NEXT i
Question 5
A FOR i ← 1 TO 10
B Total ← Total + Mark[i]
C Count ← Count + 1
D NEXT j
E Average ← Total / Count
F OUTPUT Average

Answer Key

Total, count, maximum, minimum and average in one loop
Section A: Quick Questions
Q1
B — Before the loop starts
Q2
A — After the loop: Average ← Total / Count
Q3
B — Total ← Total + Mark and Count ← Count + 1
Q4
A — Max ← Marks[1] and Min ← Marks[1]
Q5
A — Total = 175, Count = 3
Section B: Match the Pairs
Q1
Total ← 0 Count ← 0 Max ← Mark[1] Min ← Mark[1] → Initialise every variable before the loop starts, with Max and Min set to the first item
Q2
Average ← Total / Count → Calculate the average after the loop has finished, using the final total and count
Q3
Inside the one loop, the total and the counter are both updated for every item → Total ← Total + Mark[i] Count ← Count + 1
Q4
Inside the same loop, maximum and minimum are updated using IF statements that compare each item → IF Mark[i] > Max THEN Max ← Mark[i] IF Mark[i] < Min THEN Min ← Mark[i]
Q5
FOR i ← 1 TO 5 Total ← 0 Total ← Total + Mark[i] NEXT i → Total is set to 0 inside the loop, so it is reset on every pass and ends up holding only the last item. It must be initialised before the loop.
Q6
FOR i ← 1 TO 5 Total ← Total + Mark[i] Average ← Total / 5 NEXT i OUTPUT Average → The average is calculated inside the loop, before all the items have been added, so it is worked out too early. It should be calculated once after NEXT i.
Section C: Definitions
Q1
single pass — One loop visits each item once, updating the total, count, maximum and minimum together. The average is worked out after the loop ends, as Total / Count.
Section D: Fill in the Blanks
Q1
Total ← 0 Count ← 0 Max ← Marks[1]
Q2
FOR i ← 1 TO 10 Total ← Total + Marks[i] Count ← Count + 1
Q3
IF Marks[i] > Max THEN Max ← Marks[i] ENDIF
Q4
IF Marks[i] < Min THEN Min ← Marks[i] ENDIF
Q5
NEXT i Average ← Total / Count OUTPUT Average
Section E: Spot the Error
Q1
B — Count must be initialised to 0 before the loop. Starting at 1 makes the final count one too high, so it should be Count ← 0.
Q2
C — A counter goes up by 1 for each item. Adding Score[i] turns it into a second total. It should be Count ← Count + 1.
Q3
D — The IF tests for a new minimum, so the new smallest value must be stored in Min. It should be Min ← Mark[i], not Max.
Q4
A — Min must start as the first item (or a very large number). If it starts at 0, no mark is ever smaller, so Min never changes. It should be Min ← Mark[1].
Q5
D — The loop variable is i, so the loop must end with NEXT i. The average is then worked out after the loop, as on the next line.