Course 7.3Lesson 1 of 3
Some small algorithms appear in almost every program. You learn to total a set of values, count the ones that meet a condition, and find the maximum and minimum. Then you work out the average, in pseudocode and in Python, with the right starting values.
Try the code and the simulations beside the lesson.
A shop adds up the day's sales. A teacher counts how many pupils passed. A weather station finds the hottest and the coldest day. A coach works out the average time of a runner. These are the standard methods of solution in the syllabus: totalling, counting, finding the maximum, the minimum and the average.
Each one follows the same pattern. Set a starting value before the loop. Update it inside the loop, once for each value. Use it after the loop.
A total starts at . Each value is added to it.
Total ← 0
FOR Count ← 1 TO 5
INPUT Price
Total ← Total + Price
NEXT Count
OUTPUT TotalA count also starts at . It goes up by , either for every value or only for the values that meet a condition.
Passed ← 0
FOR Count ← 1 TO 30
INPUT Mark
IF Mark >= 50
THEN
Passed ← Passed + 1
ENDIF
NEXT Count
OUTPUT PassedNotice the difference. Totalling adds the value: Total ← Total + Price. Counting adds one: Passed ← Passed + 1.
To find the largest value, keep the largest one seen so far. Each new value is compared with it, and replaces it if it is larger. The starting value matters.
Highest lower than any possible value, and Lowest higher than any possible value. For temperatures on Earth, and are safe.The average is the total divided by the number of values. Work it out after the loop, once.
This algorithm inputs the midday temperature for each day of a week, in degrees Celsius. It uses all five methods.
Total ← 0
Warm ← 0
Highest ← -100
Lowest ← 100
FOR Day ← 1 TO 7
INPUT Temp
Total ← Total + Temp
IF Temp > 20
THEN
Warm ← Warm + 1
ENDIF
IF Temp > Highest
THEN
Highest ← Temp
ENDIF
IF Temp < Lowest
THEN
Lowest ← Temp
ENDIF
NEXT Day
Average ← Total / 7
OUTPUT "Average: ", Average
OUTPUT "Warm days: ", Warm
OUTPUT "Highest: ", Highest, " Lowest: ", LowestWith the inputs , , , , , and , the total is . So the average is . Four days are above : , , and . The highest is and the lowest is .
The grey blocks marked pseudocode are written in the exam style and do not run. The Python box is the same algorithm in Python, and that one runs. Python has sum(), max() and min(), but in the exam you must show the method. So the program does each one by hand.
Total ← 0 inside the loop wipes the total on every pass.Highest ← 0 fails when every value is negative. Use a very low value or the first input.Total ← 0 before the loop, Total ← Total + Value inside.Count ← 0 before the loop, Count ← Count + 1 inside (often inside an IF).Total / Count after the loop.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Total ← Total + 1 or Total ← Total + Price?Lowest ← 0 give a wrong minimum for the marks of a test out of ?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.