HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Standard Algorithms: Totals, Searching, Sorting and Validation
  1. 1Totalling, counting, maximum, minimum and average
  2. 2Linear search and bubble sort
  3. 3Validation and verification
  1. Computer Science
  2. /Standard Algorithms: Totals, Searching, Sorting and Validation
  3. /Totalling, counting, maximum, minimum and average

Course 7.3·Lesson 1 of 3

Totalling, counting, maximum, minimum and average

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.

about 25 min1 simulation1 practice question
Lesson 1 of 3Show all lessonsHide
Standard Algorithms: Totals, Searching, Sorting and Validation
  1. 1Totalling, counting, maximum, minimum and average
  2. 2Linear search and bubble sort
  3. 3

Try the code and the simulations beside the lesson.

Four jobs a computer does all the time

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.

Totalling

A total starts at 000. Each value is added to it.

pseudocode
Total ← 0
FOR Count ← 1 TO 5
    INPUT Price
    Total ← Total + Price
NEXT Count
OUTPUT Total

Counting

A count also starts at 000. It goes up by 111, either for every value or only for the values that meet a condition.

pseudocode
Passed ← 0
FOR Count ← 1 TO 30
    INPUT Mark
    IF Mark >= 50
      THEN
        Passed ← Passed + 1
    ENDIF
NEXT Count
OUTPUT Passed

Notice the difference. Totalling adds the value: Total ← Total + Price. Counting adds one: Passed ← Passed + 1.

Maximum and minimum

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.

  • Start Highest lower than any possible value, and Lowest higher than any possible value. For temperatures on Earth, −100-100−100 and 100100100 are safe.
  • Or, often safer, input the first value and use it as the starting value of both.

The average

The average is the total divided by the number of values. Work it out after the loop, once.

All together

This algorithm inputs the midday temperature for each day of a week, in degrees Celsius. It uses all five methods.

pseudocode
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: ", Lowest

With the inputs 181818, 222222, 252525, 191919, 212121, 161616 and 262626, the total is 147147147. So the average is 147÷7=21147 \div 7 = 21147÷7=21. Four days are above 202020: 222222, 252525, 212121 and 262626. The highest is 262626 and the lowest is 161616.

Practise with new numbers

Try it in Python

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.

Read it first. Then run it. Do the results match the dry run above?

1. Change every temperature to a negative number, such as a week in a freezer. Do the highest and lowest still come out right?

2. Count the days below 181818 as well. Which lines do you add?

Common mistakes

  • Setting the total inside the loop. Total ← 0 inside the loop wipes the total on every pass.
  • Adding 111 instead of the value, or the value instead of 111. Totalling adds the value; counting adds one.
  • A bad starting maximum. Highest ← 0 fails when every value is negative. Use a very low value or the first input.
  • Dividing inside the loop. Work out the average once, after the loop.
  • Dividing by the wrong number. Divide by how many values were input, not by a fixed number that may be wrong.

Remember these

  • Totalling: Total ← 0 before the loop, Total ← Total + Value inside.
  • Counting: Count ← 0 before the loop, Count ← Count + 1 inside (often inside an IF).
  • Maximum and minimum: start with the first value, or with an extreme value; replace when a new value is larger or smaller.
  • Average: Total / Count after the loop.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Which line totals the prices: Total ← Total + 1 or Total ← Total + Price?
  1. The inputs are 555, 999, 222 and 888. What are the total and the average?
  1. Why might Lowest ← 0 give a wrong minimum for the marks of a test out of 100100100?
  1. Write the line that counts the marks of 909090 or more inside an IF statement.
  1. A total of 848484 comes from 777 values. What is the average?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (2026, 2027 and 2028), unit 7, part 4: standard methods of solution.

Enroll free in Standard Algorithms: Totals, Searching, Sorting and Validation

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A school weather station records the rain each day, in millimetres…Easy · High School (9–10) · by StemPy Lab
A freezer records four temperatures in degrees Celsius: -3, -8, -1 and -6…Medium · High School (9–10) · by StemPy Lab
This is the kind of long scenario question that ends Paper 2…Hard · High School (9–10) · by StemPy Lab
Next: lesson 2Linear search and bubble sort
← Standard Algorithms: Totals, Searching, Sorting and Validation
← PreviousLinear search and bubble sort →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Validation and verification

Python Labs (1)

The standard methods by handPython Lab · by StemPy Lab