4 minute read

Creating repeating algorithms

Developing algorithms

Developing algorithms is the process of planning algorithms before creating them. This helps you to make sure all the instructions in the algorithm are presented in the correct order.

When you develop an algorithm, you must decide on a starting point. Sometimes the starting point is obvious. At other times you can choose any point to start from as they all have the same importance.

Let’s look at an indefinite (forever) loop in nature.

A female butterfly lays her eggs on a plant. After a few days, a caterpillar comes out of each egg. The caterpillar starts eating the plant and grows bigger. Then it makes a chrysalis and goes to sleep inside. While the caterpillar sleeps, the chrysalis changes into a pupa. After a few weeks, the pupa changes again and becomes a butterfly. The butterfly comes out of the pupa and flies away. The adult butterfly then lays its eggs on a plant and the cycle starts again.

Developing repeating algorithms

When you develop a repeating algorithm, follow this checklist:

• First decide on the starting point. Sometimes this is obvious, sometimes you can choose any point in the cycle.

• Write a clear, concise instruction for the starting point. For example, the butterfly lays eggs

• Think about the next step. For example, a caterpillar comes out of the egg. You don’t want to miss out any instructions, otherwise your algorithm won’t work!

• Write the instruction for each step in the most logical order until the instructions are back at the starting point.

• Write REPEAT FOREVER at the top of the algorithm to show that all the steps in that indefinite loop keep repeating.

Unplugged activity 1

You will need: paper and a pen

Work in pairs. Write a complete indefinite loop algorithm for the life cycle of a butterfly.

In Unplugged activity 1, you used the steps of the butterfly life cycle to create an algorithm with an indefinite loop. Usually when we write an algorithm, we do not know the instructions and have to decide on them ourselves.

This time you are going to develop an algorithm for a different situation. You must choose the instructions to include.

Unplugged

Activity 2

You will need: paper and a pen

Imagine a bus journey that starts and finishes at the same place. The bus driver makes the same journey many times each day. There are lots of stops along the way where the driver picks up passengers and drops them off.

Create a repeating algorithm for a bus journey that starts and finishes at the same place. Choose five destinations for the bus to stop along the journey.

Tips to help you:

• You can develop your algorithm using either a written algorithm or a diagram.

• You can pick any destinations you would like the bus to visit on the journey.

• Try to include at least five different locations on the bus journey.

How are we doing?

Work in pairs. Check your partner’s algorithm. Record your answers in a table like this:

Checkpoint

Is each destination clear?

Does the journey start and finish at the same location?

Does the algorithm clearly show an indefinite loop?

Predicting the outcome of repeating algorithms

It is important to be able to predict what algorithms will do. Predicting means to say what you think will happen.

This is helpful when you create your own algorithms. It means that you can identify possible errors before you create the program.

Questions

Practise your predicting skills and then test each situation to see if you are correct.

1 How many steps will it take your teacher to cross the classroom?

2 How many people are left-handed in your class?

3 If your teacher drops a small piece of paper, where will it land?

Predicting an output

Some things are easier to predict than others.

It can be difficult to predict what will happen when there are random elements, such as dropping a piece of paper. But you will know some things about what will happen – for example, the paper will fall down and not up.

Predicting an output in an algorithm means saying what you think will happen after each instruction in an algorithm is carried out.

Some algorithms will always have the same output each time they are run. In other algorithms that include more random elements, the output can be

Unplugged activity 3

You will need: paper and a pen

Look at the algorithm below. You can choose any number to start with, but the final number will always be the same. Predict what the final number is each time.

Use a pen and paper and run the algorithm a few times to check if your prediction is correct.

1 REPEAT FOREVER.

2 Think of a number between 1 and 10 and write it on your paper.

3 Add 10 to that number.

4 Subtract 3.

5 Subtract the first number you thought of.

6 Multiply by 2.

7 Say the number out loud.

Cool trick! I can use this algorithm to make my friends think I can read their minds!

Now let’s look at a different algorithm. You need to work out the answer when the algorithm is run over and over again. This time it will not always be the same answer.

Unplugged activity 4

You will need: paper and a pen

Follow this algorithm. This is an indefinite loop and could keep running forever, but we only need to run it a few times to see the pattern.

1 Think of a number between 1 and 10 and write it down.

2 REPEAT FOREVER.

3 Multiply the last number you wrote by 2 and write it down.

We can repeat this pattern forever because it is an indefinite loop. The answers will continue to get bigger and bigger.

When trying to predict the outcome of an algorithm, it can help to run it a few times and record your answers, as we have just done. It is not possible and not necessary to keep repeating an indefinite loop forever. After repeating it a few times, you will be able to understand the pattern and should be able to decide if it is working correctly.