← Year 10 Space

Write the algorithm

Turn the pinned-down sprinkler job into numbered steps with a decision and a loop, exact enough for a machine, then run it by hand on two ovals.

Hand the sprinkler plan to a friend who has never seen the job. Could they follow it step by step and get your answer, with nobody to ask?

Last time you pinned the sprinkler job down: an oval that's L long and W wide, a spray that reaches r, and every point within r of a sprinkler. Now the plan has to be written so exactly that a machine could follow it. You found that one sprinkler covers a square a bit under 1.41 × r wide, so the plan uses 1.4 × r to stay on the safe side, and the oval is cut into cells that size or smaller with a sprinkler in the middle of each. The tricky part is deciding how many cells fit, so have a guess at that before the steps.

How this lesson works: Predict, Try, Investigate, Modify, Practise, Make
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

How many cells?

You have a guess. Now here are the steps that give the answer.

One sprinkler covers a square about 11.2 m wide (made up). The junior oval is 40 m by 24 m. How many sprinklers does it need to be fully covered?

TryTry it on the board and compare the result with your prediction.

The cell size

Steps 3 to 5 on the junior oval (L = 40, W = 24, r = 8): s = 1.4 × 8 = 11.2, then cols = 40 ÷ 11.2 rounded up. After that, w = 40 ÷ cols. How wide is one cell, in metres?

InvestigateWork out why it happens, one step at a time.

Steps, decisions and loops

A flowchart of the sprinkler algorithm. Read the length, width and reach. A decision asks whether any of them is zero or less: if yes, say not an oval and stop. If no, set s to 1.4 times r, count the columns and rows rounding up, work out the cell size, place one sprinkler in each cell, then say the count and positions.
The sprinkler algorithm

Say which steps compute, which decide, and which repeat. Here are the steps in full.

  1. Read the oval's length L, its width W and the spray's reach r, all in metres.
  2. If L, W or r is zero or less, say "not an oval" and stop.
  3. Set s = 1.4 × r.
  4. Set cols = L ÷ s, rounded up, and rows = W ÷ s, rounded up.
  5. Set w = L ÷ cols and h = W ÷ rows.
  6. For each row j from 1 to rows, and each column i from 1 to cols, put a sprinkler at x = (i − 0.5) × w and y = (j − 0.5) × h.
  7. Say how many sprinklers there are (cols × rows), and where each one is.

Here is the whole algorithm run on the junior oval.

Worked exampleBefore each step, say what you think comes next.
  1. s=1.4×8=11.2s = 1.4 \times 8 = 11.2Step 3.

Now run it yourself on paper, on the senior oval, with the example beside you. Write each step's result, then pick the reason for rounding up.

Check where Zoe's first sprinkler lands, and where its cell's middle is.

Build up to it

On the junior oval each cell is 10 m wide. How far along the 40 m side is the middle of the first cell, in metres?

Think about what you'd have to write without a loop.

Here is the algorithm, step by step. What does each step do?

Step 2: If L, W or r is zero or less, say 'not an oval' and stop
Step 3: Set s = 1.4 × r
Step 4: Set cols = L ÷ s and rows = W ÷ s, each rounded up
Step 5: Set w = L ÷ cols and h = W ÷ rows
Step 6: For each row and each column, put a sprinkler in the middle of the cell
Step 7: Say how many sprinklers, and where each one is

Run the algorithm by hand on the senior oval: L = 60 m, W = 30 m, r = 8 m (made up). Write each step's result, then how many sprinklers it places.

Work it out on paper first, one step per line. Then type your answer.

Why is 5.36 rounded up to 6, not down to 5?

Zoe tries her algorithm on the junior oval (w = 10, h = 8) and finds the left end of the oval is out of reach of every sprinkler. Click the line where it goes wrong.

What is the loop in step 6 for?

ModifyChange the problem on purpose and see what happens.

Change the oval

Run it on an oval exactly 11.2 m long and 11.2 m wide (r = 8). How many sprinklers does it place?

Run it on an oval 11.5 m long and 4 m wide (r = 8). How many sprinklers does it place?

On that 11.5 m by 4 m oval, one sprinkler in the middle is only about 6.1 m from a corner, well inside 8 m. What does that tell you about the algorithm?

The new sprinklers reach 5 m instead of 8 m. Which step of the algorithm has to be rewritten?

PractiseA few on your own, to check you have it.

Run it

Run the algorithm on an oval 50 m long and 20 m wide (r = 8, so s = 11.2). How many sprinklers does it place?

On that oval each cell is 10 m wide. How far along the 50 m side is the middle of the first cell, in metres?

Which of these is a boundary case for the algorithm when s = 11.2?

Review: a cell is 6 m wide and 8 m tall, with the sprinkler in the middle. How far is the sprinkler from a corner, in metres?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

The other oval

Write this on paper first, a step at a time.

Also used in

  • Packing a van. A loop places one box after another, a decision rejects a box that's too big, and the steps are exact enough for a program.
  • Laying paving or tiles. The same rounding up and the same cell by cell placing.
  • Testing and refining. The next job is to test an algorithm on awkward ovals, such as a curved one or one with a tree in the way, find where it fails, and improve it. That's for your class project.

The caretaker's third oval is 36 m long and 20 m wide, with an 8 m spray (made up). Run the algorithm by hand. Write each step's result, then how many sprinklers it places.

Work it out on paper first, one step per line. Then type your answer.

Now test the answer. Each cell on that oval is 9 m by 10 m with its sprinkler in the middle. How far is the sprinkler from a corner of its cell, to 1 decimal place? Is it within 8 m?

Now you can
Practise this skillNew questions, new numbers each timeGet 3 right in a row to mark it practised.Finished the year?Go over it: Year 10 maths recapEvery idea in one place, to check you've got it.You reached the endBack to Year 10 Space
For teachers

Students turn the pinned-down sprinkler job into numbered steps with a decision and a loop, run the steps by hand on two ovals, and find a bug where a sprinkler is placed on the edge of its cell instead of the middle (AC9M10SP03, node 2). About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Do the Predict and the senior oval yourself first. Students need Pin the problem down, Pythagoras and the Year 9 idea that an algorithm has computation and decision steps.

The one idea: an algorithm is exact steps that work for any input. Here the steps cut the oval into cells no wider than 1.4 × the reach (the biggest square is about 1.41 × the reach, so 1.4 is on the safe side), with a sprinkler in the middle of each cell. A decision guards the input and a loop places one sprinkler in every cell.

The steps always cover the oval, but they don't always use the fewest sprinklers: the 11.5 m by 4 m oval in Modify needs two when one would do. That is on purpose, to show the gap. Testing awkward ovals (a curved one, a tree in the middle), finding where the algorithm fails and refining it is node 3, the class project, and isn't covered here.

The junior and senior ovals, the reach and the sizes are all made up. A flowchart or pseudocode both work; the numbered steps and the flowchart in the lesson say the same thing.

Running the lesson

  1. Predict: take a show of hands. Most will say 6 or 9. Ask a student to say what happens to the leftover strip.
  2. Paper: the senior oval and the third oval are worked on paper first, a step at a time, with the steps beside you.
  3. Investigate: ask a student to point at the loop and say what would have to be written without it.
  4. Modify: before 11.5 m by 4 m, ask whether the algorithm will place one or two.
  5. Make: ask whether the answer meets the success criterion, and how they know.

The hard part (on purpose)

Rounding a count down, and putting a sprinkler on the edge of its cell. Ask: what is left over if you round down? Where is the middle of the first cell?

When students get stuck

Stretch and support

Stretch: Write the same algorithm in pseudocode with a loop that reads a list of ovals and prints each count, then try a staggered layout, where every second row is shifted half a cell, and see whether it needs fewer sprinklers.

Support: A printed copy of the seven steps and a blank table with columns for s, cols, rows, w, h and the count.

Where it leads

This covers AC9M10SP03 node 2.

Node 3 (testing, refining and justifying) is left to class: the project is to test this algorithm on the school's own oval, find where it fails, and refine it. Where it leads: the same habit of pinning down, then writing steps, then testing, for any spatial job.

What each step is for