Keeping Count

Throughout the course, you’ll be investigating a number of algorithms. You’ll look at how you can describe algorithms, compare them, and ultimately program them. In the next few steps you’re going to practise by looking at a counting algorithm.

The goal of this algorithm will be to count the number of matching items in a given list, whether it be a list of names, scores, a drinks order, or any other kind of list.

So suppose you are given this drinks list:

If your counting algorithm were asked to count Tea, it would return the value 4. As programmers, your job is to think about how to make a computer carry this out.

Step by step

The above example might seem trivial, as most human beings could very quickly and accurately determine how many teas, coffees, and hot chocolates there are. In fact, people may do this without consciously counting the items. A computer, however, cannot do this — it cannot look at the whole picture at once and make judgements about it. Instead, it will need to look at each item in turn.

This is important to remember when thinking about how to instruct computers to solve problems. They lack many human skills, including interpretation and estimation. When looking at the above list, or a list of any length, the computer will have to check each and every value in the list.

If you were to express the process carried out by the computer in words, you might say:

Look at each item in the drinks list
  Is the item Tea?

Keeping count

Once your algorithm is looking at each value in the list, you need a way of tallying the number of matching items. For this, of course, you will use a variable. You need to set the count to zero at the start of the program, and then increment the variable for each matching item in the list.

Your algorithm would therefore look something like this:

Set count to 0

Look at each item in the drinks list
  If the item is “Tea”
    Add 1 to count

Display count

This algorithm should work for a list of any length, and should be sufficiently detailed for a programmer to adapt and code in their chosen language.

An alternative view

There is no set way of writing algorithms and no formal language that they have to use, as they usually only serve as planning tools for programs that are yet to be implemented. There are other ways to express algorithms that can be a little more like code or even displayed symbolically.

Pseudocode

Pseudocode is one way of expressing algorithms that is quite close to its final representation in code. It is often used to communicate programs in a way that is neutral between programming languages.

In England, GCSE exam boards have different approaches to using pseudocode, so it is worth checking your exam board’s specification before you start to teach it. To overcome this, the Teach Computing team have created this version of pseudocode to use with the Teach Computing Curriculum lesson resources, which cover all the GCSE specifications.

Using this standard, your algorithm above might look like this. (The grammar uses the symbols <- to show a value being assigned to a variable, as these symbols resemble an arrow.)

count <- 0

FOR item = 0 to LEN(drinks)
  IF item is equal to “Tea” THEN
    count <- count + 1
ENDFOR

OUTPUT count

Flowcharts

Another, more visual, way to represent this algorithm would be using flowchart symbols to show the program flow. Flowcharts are a tool which many students may find more appealing, although as the algorithm grows in complexity the flowchart grows in size.

Below you can see the same counting algorithm expressed using a flowchart.