Skip to 0 minutes and 3 seconds Imagine you wanted to count the number of items in a list– for example, the number of cups of tea in this drinks list. A counting algorithm should return the value 4. So as programmers, our job is to think about how to make a computer carry this out. This might seem trivial, as most human beings could very quickly and accurately determine how many tea, coffee, and hot chocolates there are in this list. In fact, many people would do this without consciously counting them at all. A computer, however, cannot do this. It cannot look at the whole picture in one go and make judgments about it. It would instead need to look at each and every item in turn.

Skip to 0 minutes and 43 seconds This is important to remember when thinking about how to instruct computers to solve problems. They lack many human skills, including interpretation and estimation. Once our algorithm is looking at each value in the list, we’ll need a way of tallying the number of matching items. For this, of course, we can use a variable. At the start of the program, we just set that variable to 0 and increment it each time we find a matching item in the list. Our algorithm would therefore look something like this. We would set the count to 0, look at each item in the drinks list in turn. And if the item is tea, we would add 1 to the count.

Skip to 1 minute and 22 seconds Once we’ve looked at all items, we would display the value of our count variable. This algorithm should work for a list of any length and should be sufficiently detailed for a programmer to turn into code in their chosen language. There is no set way of writing in algorithms and no formal language as, often, they’re only really used as planning tools for programmers. This means that there are other ways to express algorithms that can be a little more code-like or even be displayed symbolically. 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 language-neutral way. There is no universal way of writing pseudocode.

Skip to 2 minutes and 5 seconds But the standards set out by the English exam body, AQA– our tea counting algorithm might look something like this. Another more visual way to represent this algorithm would be using flowchart symbols to show the program flow. Flow charts are a tool which many students may find more appealing. Although as the algorithm grows in complexity, the flow chart grows in size. Here, you can see the same drinks algorithm expressed using a flowchart. Notice that we use rectangles for processes. We use diamonds for decisions. And we use parallelograms for inputs and outputs.

Skip to 2 minutes and 41 seconds In this section, we’ve examined how algorithms can be expressed in plain English, pseudocode, or flow charts. Whichever representation you feel most comfortable with– have a go at completing the algorithm challenges at the bottom of the page. Remember to share your solutions in the comments. Good luck.

# Keeping Count

Throughout the course, we’ll be investigating a number of algorithms. We’ll look at how we can describe algorithms, compare them, and ultimately program them. In the next few steps we’re going to look together at a simple 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 we are given this *drinks* list:

- Tea
- Coffee
- Tea
- Tea
- Coffee
- Hot Chocolate
- Tea

If our counting algorithm were asked to *count Tea*, it would return the value 4. As programmers, our 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 we were to express the process carried out by the computer in words, we might say:

Look at each item in the

drinkslist

Is the item Tea?

## Keeping count

Once our algorithm is looking at each value in the list, we need a way of tallying the number of matching items. For this, of course, we will use a variable. We 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.

Our algorithm would therefore look something like this:

Set count to 0

Look at each item in the

drinkslist

If the item is “Tea”

Add 1 to countDisplay 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. Whilst there is no universal way of writing pseudocode, some organisations have sought to develop their own standard for consistency. This example grammar , set out by the English exam body AQA, is regularly used to communicate algorithms with learners. Using this standard, our algorithm above might looks 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 = “Tea” THEN

count <- count + 1

ENDFOROUTPUT(count)

### Flowcharts

Another, more visual, way to represent this algorithm would be using flowchart symbols to show the program flow. Flowcharts are 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.

## Your turn

In this section, we’ve examined how algorithms can be expressed in three different forms (plain English, pseudocode, and flowcharts). Use the comments to answer the following questions, using whichever representation you feel most comfortable with:

- What would an algorithm for finding the largest number in a list look like?
- What about the most popular item in a list?
- How and when would you use each form with your learners?