Flowcharts and algorithms written in natural language
Machine translation
Learn
An algorithm in natural language is a numbered list of steps written in sentences. Each step is clear and does only one thing. It takes in data, which is the input, and gives an answer, which is the output.
Double n plus 3 1. Read n. 2. Work out double n. 3. Add 3 to the answer. 4. Write the answer.
Input: n. Output: the answer A flowchart draws the algorithm with boxes and arrows. It starts at START and ends at END. Each arrow shows which step comes next. A question with two exits, yes and no, is a decision. In your book it is drawn with a diamond. Here each box is a line with its arrows.
START → 1 1. Read n → 2 2. Is n even? yes → 3 no → 4 3. Write “even” → END 4. Write “odd” → END END
A flowchart with a decision: even or odd To follow an algorithm, start with a piece of data and do each box. In a decision, answer yes or no and follow that arrow.
Step Box What happens 1 Read n n = 7 2 Is n even? no 4 Write “odd” “odd” comes out The route with n = 7: steps 1, 2 and 4 If an arrow goes back to an earlier step, it makes a loop: the steps repeat. The loop has to end. For that, something must change on each turn.
START → 1 1. Set n to 20 → 2 2. Is n > 5? yes → 3 no → 4 3. Subtract 6 from n → 2 4. Write n → END END
A loop: n goes down by 6 each turn until n > 5 is false A good algorithm works for lots of data. That is why it uses a letter, such as n, and not a fixed number.
Worked examples
Example 1
Tony follows this flowchart with price = 70. What does he write?
START → 1 1. Read price → 2 2. Is price > 50? yes → 3 no → 4 3. Subtract 10 from price → 4 4. Write price → END END
- Step 1: he reads 70 and stores it in price.
- Step 2: 70 > 50 is true. Follow the “yes” arrow, to step 3.
- Step 3: subtract 10 and price is worth 60. Step 4: write 60.
Write 60.
Watch out! Here many people skip step 3 and write 70.
Example 2
Julie follows the loop flowchart (n starts at 20). What does she write?
| Turn | n | ¿n > 5 | new n |
|---|---|---|---|
| 1 | 20 | yes | 14 |
| 2 | 14 | yes | 8 |
| 3 | 8 | yes | 2 |
| 4 | 2 | no | — |
- Turn 1: 20 > 5, so n becomes 14. Turn 2: 14 > 5, n becomes 8.
- Turn 3: 8 > 5, n becomes 2.
- Now 2 > 5 is false: follow the “no” arrow and write 2.
Write 2.
Watch out! Here many people stop at 8, because they forget that the arrow from step 3 goes back to step 2.
Example 3
Ellie works out 7 × 2 + 3 and wants an algorithm that works for any number. How does she write it?
1. Read n. 2. Work out n × 2. 3. Add 3 to the answer. 4. Write the answer.
- Think about which piece of data goes in: a number, n.
- Write the steps with n and not with 7.
- Finish with the output: write the answer.
Read n, work out n × 2, add 3 and write the answer.
Watch out! Here many people write “work out 7 × 2 + 3”, and the algorithm only works for 7.
A challenge to think about
Write an algorithm in natural language to decide whether someone can go on a ride: they must be more than 120 cm tall. Then draw its flowchart with boxes and arrows. Test it with three heights, one of them exactly 120 cm.
Practise
Rounds of 10 questions. Choose a level:
Your grade in this topic:No grade yetDo a round at the Basic level to get a grade.