AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Inputs and outputs

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.

Startenter a numberanswer = number + numbershow the answerEnd
Worked examples
InputProcessOutput
88 + 816
1515 + 1530
This algorithm doubles a number. We trace it with 8. Input: 8. Process: 8 + 8 = 16. Output: 16. With 15 the output is 30.
StepInstructionKind
1Start
2Enter the priceinput
3Enter the money giveninput
4Let change = money given − priceprocess
5Show “Thank you!”output
6Show the changeoutput
7Stop
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.
PriceMoney givenOutput of the wrong step 4Right answer
710−33
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?
StepInstruction
1Start
2Enter the number of guests
3Let cups = guests + guests
4Show the number of cups
5Stop
2
Use the algorithm from question 1. Nine guests are coming. What is the output?
3
Write an algorithm that asks for a child’s age and shows how old the child will be in 5 years.
4
Trace this algorithm with the number 4. What is the output?
Startenter a numberanswer = number + number + numbershow the answerEnd