Counting elements that meet a condition

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

Counting elements that meet a condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Count ← 0 be written when counting how many marks are 50 or more?
A Before the loop starts
B Inside the loop, before the IF
2. Which line adds 1 to the counter when a mark passes the test?
A Count ← Count + 1
B Count ← Marks[Index]
3. Which code counts the marks that are 50 or more?
A IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF
B IF Marks >= 50 THEN Count ← Count + 1 ENDIF
4. A loop checks 10 marks and 6 of them are 50 or more. What is Count at the end?
A 10
B 6
5. What is the difference between a counter and a total?
A A counter adds up the values; a total adds 1 for each match
B A counter adds 1 for each match; a total adds up the values

Match the Pairs

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

Left columnRight column
1Count ← 0 AIF Names[Index] = Target THEN Count ← Count + 1 ENDIF
2IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF BTotal ← Total + Marks[Index]
3To count how many names in an array equal a given name, compare each element with that name and add 1 when they match CTests the current element and adds 1 to Count only when the mark is 50 or more
4A counter adds 1 each time the test is true, but a total adds the element values themselves DCount adds the mark itself instead of 1, so it totals the passing marks rather than counting them. It should be Count ← Count + 1.
5FOR Index ← 1 TO 10 Count ← 0 IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index ESets the counter to zero before the loop starts, so counting begins from nothing
6IF Marks[Index] >= 50 THEN Count ← Count + Marks[Index] ENDIF FCount ← 0 is inside the loop, so the counter is reset to zero on every repeat and the final count is wrong. Set it to 0 once before the loop.

Definitions

IF Marks[Index] >= 50 THEN Count ← Count + 1

Fill in the Blanks

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

1.
Count ← _ FOR Index ← 1 TO 10 IF Marks[Index] >= 50 THEN
2.
FOR Index ← 1 TO 10 IF Marks[Index] __ 50 THEN Count ← Count + 1
3.
Count ← Count + 1 _____ NEXT Index
4.
IF Names[Index] _ SearchName THEN Count ← Count + 1 ENDIF
5.
IF Marks[Index] >= 50 THEN Count ← Count + _ ENDIF

Spot the Error

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

Question 1
A Count ← 0
B FOR Index ← 1 TO 10
C IF Marks[Index] >= 50
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 2
A Count ← 0
B FOR Index ← 1 TO 20
C IF Names[Index] = Sam THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 3
A Count ← 0
B FOR Index ← 1 UNTIL 10
C IF Marks[Index] >= 50 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 4
A Count ← 1
B FOR Index ← 1 TO 8
C IF Temp[Index] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 5
A Count ← 0
B FOR Index ← 1 TO
C IF Age[Index] >= 18 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index

Answer Key

Counting elements that meet a condition
Section A: Quick Questions
Q1
A — Before the loop starts
Q2
A — Count ← Count + 1
Q3
A — IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF
Q4
B — 6
Q5
B — A counter adds 1 for each match; a total adds up the values
Section B: Match the Pairs
Q1
Count ← 0 → Sets the counter to zero before the loop starts, so counting begins from nothing
Q2
IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF → Tests the current element and adds 1 to Count only when the mark is 50 or more
Q3
To count how many names in an array equal a given name, compare each element with that name and add 1 when they match → IF Names[Index] = Target THEN Count ← Count + 1 ENDIF
Q4
A counter adds 1 each time the test is true, but a total adds the element values themselves → Total ← Total + Marks[Index]
Q5
FOR Index ← 1 TO 10 Count ← 0 IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index → Count ← 0 is inside the loop, so the counter is reset to zero on every repeat and the final count is wrong. Set it to 0 once before the loop.
Q6
IF Marks[Index] >= 50 THEN Count ← Count + Marks[Index] ENDIF → Count adds the mark itself instead of 1, so it totals the passing marks rather than counting them. It should be Count ← Count + 1.
Section C: Definitions
Q1
IF Marks[Index] >= 50 THEN Count ← Count + 1 — An IF inside the loop tests each element and adds 1 to Count when true. Set Count to 0 before the loop. A counter counts matches; a total adds up values.
Section D: Fill in the Blanks
Q1
Count ← 0 FOR Index ← 1 TO 10 IF Marks[Index] >= 50 THEN
Q2
FOR Index ← 1 TO 10 IF Marks[Index] >= 50 THEN Count ← Count + 1
Q3
Count ← Count + 1 ENDIF NEXT Index
Q4
IF Names[Index] = SearchName THEN Count ← Count + 1 ENDIF
Q5
IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF
Section E: Spot the Error
Q1
C — The IF line is missing THEN. It must be IF Marks[Index] >= 50 THEN so the counter is only increased when the test is true.
Q2
C — Sam is text, so it needs quote marks. Without them it looks like a variable. It should be IF Names[Index] = "Sam" THEN.
Q3
B — A count-controlled loop uses TO, not UNTIL. The line should be FOR Index ← 1 TO 10.
Q4
A — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be Count ← 0.
Q5
B — The FOR loop has no upper bound, so it does not say where to stop. It should be FOR Index ← 1 TO 5 so every element is tested.