AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Repeat and reuse

Week 5 · 1.3 Creating algorithms · 3 lessons
2

Limits you type

Computing 9: Topic 1.3, pp. 46–79
GoalBuild loops whose start, end or number of repeats come from the user, guard them with a selection, and use them for averages and percentages.
New words
guard · an IF placed before a loop so the loop only runs when the typed values make senseaverage · the total of the values divided by how many values there arepercentage · a part divided by the whole and multiplied by 100
Explanation

The values in FOR i ← a TO b do not have to be fixed numbers. The user can type them, and the loop then follows the input. The counter can also be used as data inside the body, for example to work out a price from a weight. A typed range can be nonsense, such as a last value smaller than the first one. A guard solves this: an IF before the loop checks the values, and the loop sits in the THEN part. For an average, add the typed values to a running total inside the loop and divide by the number of values after it. If that number could be 0, the division needs a guard too, because you cannot divide by 0. For a percentage, count the items that match, multiply the count by 100 and divide by the number of items.

Pseudocode
1INPUT first
2INPUT last
3IF last >= first THEN
4    FOR kg ← first TO last
5        OUTPUT kg, " kg: ", 4 + kg * 2
6    NEXT kg
7ELSE
8    OUTPUT "Check your numbers"
9ENDIF
Output
2 kg: 8
3 kg: 10
4 kg: 12
5 kg: 14
Worked examples
StartINPUT firstINPUT lastlast >= first?yesnokg ← firstkg <= last?yesOUTPUT kg, " kg: ", 4 + kg * 2kg ← kg + 1noOUTPUT "Check your numbers"End
The parcel price list above, drawn as a flowchart. The user types 2 and 5. The guard 5 >= 2 is Yes, so the loop starts with kg = 2. Each pass outputs the price 4 + kg * 2, which is 8, 10, 12 and 14, and adds 1 to kg. When kg is 6 the test kg <= 5 is No. If the user types 5 and 2 the guard is No and the message is output, with no price at all.
Pseudocode
1INPUT n
2total ← 0
3FOR i ← 1 TO n
4    INPUT temp
5    total ← total + temp
6NEXT i
7IF n > 0 THEN
8    OUTPUT "Average: ", total / n
9ELSE
10    OUTPUT "No readings"
11ENDIF
Output
Average: 20
An average of a typed number of readings. The user types 4 for n and then the temperatures 18, 21, 19 and 22. The running total grows to 18, 39, 58 and 80. Then 80 / 4 gives 20. The IF n > 0 protects the division, and with n = 0 the output is No readings.
Pseudocode
1INPUT n
2wins ← 0
3FOR game ← 1 TO n
4    INPUT result
5    IF result = "W" THEN
6        wins ← wins + 1
7    ENDIF
8NEXT game
9OUTPUT wins * 100 / n, " % won"
Output
60 % won
A percentage. A team plays n games and the user types W for a win or L for a loss after each one. For 5 games the user types W, L, W, W, L, so wins becomes 1, 1, 2, 3, 3. The output is 3 * 100 / 5, which is 60. (This algorithm assumes n is at least 1.)
Class activity

Price list maker (no computer). Invent a rule for a price, such as 3 + kg * 4, or the cost of a bus ticket by number of stops. Your partner chooses a first and a last value. Run the loop by hand and write the list of prices, keeping a table with the columns counter and output. Then your partner chooses a last value that is smaller than the first. Show with the guard that the loop does not run, and say which message is output. Swap roles twice.

Practice
1
The user types 2 for first and 4 for last. Draw a trace table with the columns n, output and total, and give everything that is output.
Pseudocode
1INPUT first
2INPUT last
3total ← 0
4FOR n ← first TO last
5    OUTPUT n * n
6    total ← total + n * n
7NEXT n
8OUTPUT "Sum: ", total
2
What goes wrong when the user types 0 for n? Rewrite the last line so that the algorithm is safe.
Pseudocode
1INPUT n
2total ← 0
3FOR i ← 1 TO n
4    INPUT price
5    total ← total + price
6NEXT i
7OUTPUT "Average: ", total / n
3
The user types the lengths 4, 7 and 5 and expects the total 16. What is output? Find the mistake and correct it.
Pseudocode
1total ← 0
2FOR i ← 1 TO 3
3    INPUT length
4    total ← total + i
5NEXT i
6OUTPUT total
4
Write pseudocode. The user types how many pupils there are, then each pupil’s height in cm. Output how many are 150 cm or taller and what percentage that is. Test it with 4 pupils: 148, 150, 155 and 162.