11
Practical lesson: presenting algorithms
Textbook: p. 34
GoalWrites an algorithm that fits an executor’s instruction set, presents it in words and as a flowchart, and calculates with a condition block.
New words
instruction set: the commands the executor is allowed to use · ko‘rsatmalar tizimicondition block: a rhombus with two exits, “yes” and “no” · shart tekshirish blokidiscount: a part of the price taken off · chegirmatrace table: a table of variable values after each step · kuzatuv jadvali
Explanation
In this practical lesson we design algorithms to fit the executor’s abilities. First write down the executor’s instruction set and then use only those commands. Writing down the result after every step (a trace table) is the easiest way to check an algorithm. Problems with a condition (for example “if the purchase is over 100 000 so‘m, take 5 % off”) need a rhombus block: the discount is computed on the “yes” branch and the price stays the same on the “no” branch. When the algorithm is ready, walk through it by hand with one or two numbers: put in the input value and pass through every block.
Worked examples
The executor’s set: {+3; ×2; –4}. To get 22 from 5: ×2 → 10; +3 → 13; ×2 → 26; –4 → 22. Trace table: 5, 10, 13, 26, 22.
The purchase is 120 000 so‘m. Flowchart: start → input price → price > 100 000? → “yes”: pay = price – price · 5 / 100; “no”: pay = price → output pay → end. Since 120 000 > 100 000, pay = 120 000 – 6 000 = 114 000 so‘m.
Class activity
“Executor test”: one group invents an executor and its instruction set; another group writes an algorithm that solves a task with that set.
Practice
1
An executor knows the commands {+3; ×2; –4}. Write an algorithm that makes 22 from 5.
×2, +3, ×2, –4: 5 → 10 → 13 → 26 → 22.
2
A 5 % discount applies to purchases over 100 000 so‘m. How much is paid for a purchase of 120 000 so‘m?
114000
3
Which executor fits the instruction set {take the ball; dribble the ball; pass to a teammate; throw into the basket}?
A basketball player (a person).
4
Why can’t you give the command “up” to an executor with {right; left}?
“Up” is not in its instruction set, so the executor does not understand the command and cannot carry it out.