Week 28 · Unit 12: Be a problem solver · 3 lessons
2
Algorithms with inputs
Computing 3: Unit 12, pp. 178–193
GoalPick out the input and output statements in an algorithm, and trace it with numbers.
New words
input statement · a step that asks for dataoutput statement · a step that shows the resulttrace · to follow an algorithm step by step with real numbers
Explanation
Some algorithms need data. A step that asks for data is an input statement, such as “Enter a number”. A step that shows the result is an output statement, such as “Show the answer”. The steps between them work out the answer. An algorithm usually begins with Start and ends with Stop. To check an algorithm, we trace it. We choose some numbers, follow each step and write down the output. If the output is not what we expected, the algorithm has an error.
Worked examples
Input
Process
Output
8
8 + 8
16
15
15 + 15
30
This algorithm doubles a number. We trace it with 8. Input: 8. Process: 8 + 8 = 16. Output: 16. With 15 the output is 30.
Step
Instruction
Kind
1
Start
2
Enter the price
input
3
Enter the money given
input
4
Let change = money given − price
process
5
Show “Thank you!”
output
6
Show the change
output
7
Stop
This algorithm works out the change at a shop. Steps 2 and 3 ask for data, so they are input statements. Steps 5 and 6 show something, so they are both output statements: an algorithm can have more than one output. Trace it with a price of 7 and a payment of 10: it shows “Thank you!” and then 10 − 7 = 3.
Price
Money given
Output of the wrong step 4
Right answer
7
10
−3
3
Tracing finds mistakes. In step 4, someone has written: let change = price − money given. With 7 and 10 the output is 7 − 10 = −3, which is not right for change. We swap the two numbers in step 4.
Class activity
Number machine (in pairs): on a card, write a secret rule in three steps, such as “1 Enter a number. 2 Add 3. 3 Show the answer.” Do not show it. Your partner says a number, and you say the output. After three tries, can your partner guess the rule? Swap roles. Then write down the input and output statements of your algorithm.
Practice
1
Which step is the input statement and which is the output statement?
Step
Instruction
1
Start
2
Enter the number of guests
3
Let cups = guests + guests
4
Show the number of cups
5
Stop
Step 2 is the input statement. Step 4 is the output statement.
2
Use the algorithm from question 1. Nine guests are coming. What is the output?
9 + 9 = 18 cups.
3
Write an algorithm that asks for a child’s age and shows how old the child will be in 5 years.
1 Start. 2 Enter the age. 3 Let new age = age + 5. 4 Show the new age. 5 Stop.
4
Trace this algorithm with the number 4. What is the output?