This set covers the skills in repetition and arrays: tracing count-controlled and condition-controlled loops, repairing off-by-one faults, and using arrays with a loop. Attempt each question on paper and write the trace table before you open the answer.
Questions 7 to 11 use the array Temp[1:6], which holds the readings 28, 31, 27, 33, 30, 33. Log your wrong answers in the mistake log.
Warm-up: count-controlled loops
Q1. What is output?
FOR i ← 1 TO 4
OUTPUT i * i
NEXT i
Show answer
i takes 1, 2, 3, 4. The outputs are 1, 4, 9, 16.
Q2. How many times does the loop run, and what is Sum at the end?
Sum ← 0
FOR i ← 2 TO 10 STEP 2
Sum ← Sum + i
NEXT i
Show answer
i takes 2, 4, 6, 8, 10, so the loop runs 5 times. Sum is 2, 6, 12, 20, 30. The final value is 30.
Condition-controlled loops
Q3. What are N and Count at the end?
N ← 100
Count ← 0
WHILE N > 10 DO
N ← N DIV 3
Count ← Count + 1
ENDWHILE
Show answer
| Test | N after | Count after |
|---|---|---|
| 100 > 10 true | 33 | 1 |
| 33 > 10 true | 11 | 2 |
| 11 > 10 true | 3 | 3 |
| 3 > 10 false | ends |
N is 3 and Count is 3.
Q4. What is the value of X when the loop ends, and how many passes were made?
X ← 1
REPEAT
X ← X * 2
UNTIL X > 50
Show answer
X becomes 2, 4, 8, 16, 32 (each is not > 50, so repeat), then 64 (64 > 50, stop). That is 6 passes and X is 64.
Repair and spot the fault
Q5. The task is to output 1 to 8. This outputs 1 to 7. Repair it.
Count ← 1
WHILE Count < 8 DO
OUTPUT Count
Count ← Count + 1
ENDWHILE
Show answer
When Count is 8, 8 < 8 is false and 8 is never output. Change the condition to WHILE Count <= 8. Then Count 1 to 8 pass the test and 8 values are output.
Q6. This loop never ends. Explain why, repair it, then state the final N and the number of passes for your repair.
N ← 10
WHILE N <> 0 DO
N ← N - 3
ENDWHILE
Show answer
N takes 7, 4, 1, -2, -5 and so on. It skips over 0, so N <> 0 is always true. Repair: WHILE N > 0.
Trace: 10 > 0 true (N = 7), 7 > 0 true (N = 4), 4 > 0 true (N = 1), 1 > 0 true (N = -2), -2 > 0 false. That is 4 passes and N is -2.
Arrays and loops
Q7. Complete the code to count how many readings are above 30, then give the output.
Hot ← 0
FOR i ← 1 TO 6
IF Temp[i] > 30
THEN
Hot ← Hot + 1
ENDIF
NEXT i
OUTPUT Hot
Show answer
The readings above 30 are 31, 33 and 33. 30 is not above 30. Hot is 3.
Q8. Trace this and give the output.
Max ← Temp[1]
MaxPos ← 1
FOR i ← 2 TO 6
IF Temp[i] > Max
THEN
Max ← Temp[i]
MaxPos ← i
ENDIF
NEXT i
OUTPUT MaxPos, Max
Show answer
| i | Temp[i] | Test | Max | MaxPos |
|---|---|---|---|---|
| start | 28 | 1 | ||
| 2 | 31 | 31 > 28 true | 31 | 2 |
| 3 | 27 | false | 31 | 2 |
| 4 | 33 | 33 > 31 true | 33 | 4 |
| 5 | 30 | false | 33 | 4 |
| 6 | 33 | 33 > 33 false | 33 | 4 |
The output is 4, 33.
Q9. Write pseudocode to output the total and the average of all six readings. Then give both values, with the average to 1 decimal place.
Show answer
Total ← 0
FOR i ← 1 TO 6
Total ← Total + Temp[i]
NEXT i
Average ← Total / 6
OUTPUT Total, Average
Total after each pass: 28, 59, 86, 119, 149, 182. The total is 182. The average is 182 / 6 = 30.33…, so 30.3 to 1 decimal place.
Q10. What is Temp[Temp[1] - 25]?
Show answer
Temp[1] is 28. 28 - 25 = 3. Temp[3] is 27.
Q11. A stock array Stock[1:4] starts as 5, 0, 3, 0. Give its contents after this code.
FOR i ← 1 TO 4
IF Stock[i] = 0
THEN
Stock[i] ← 10
ENDIF
NEXT i
Show answer
Positions 2 and 4 hold 0 and are replaced. The array becomes 5, 10, 3, 10.
If you got these wrong
- Wrong number of passes (Q1, Q2, Q4, Q5): revisit tracing a count-controlled loop and tracing a condition-controlled loop.
- Loop never ends or ends one pass early or late (Q5, Q6): go to repairing an off-by-one error.
- Wrong maximum or position (Q8): read finding a maximum in a supplied list.
- Index and value mixed up (Q9, Q10, Q11): work through using an array index without confusing it with a value.
Step through any of these in the pseudocode trace trainer or the Python reasoning sandbox. If one error type keeps returning, our online one-to-one Computer Science tuition can focus on exactly that skill.