Teachers' Portal  /  IGCSE Computer Science 0478  /  Algorithm design and problem-solving  /  Writing and amending algorithms
Lesson pack · IGCSE Computer Science 0478 · 7 (7-9) · IGCSE

Writing and amending algorithms

Say it precisely. The longest questions ask you to write an algorithm for a scenario, or to change one you are given.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Algorithm design and problem-solving
Syllabus reference
Cambridge IGCSE Computer Science 0478, 7 (7-9)
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 9 of 9 in Algorithm design and problem-solving; follows Finding and correcting errors in algorithms
Central model
Fig. 1: a flowchart that totals numbers until -1 is entered
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Precision matters: Examiners can only award marks for statements a computer could follow.
  • Writing a solution: Run the standard methods programs in the lab, then see how the same patterns combine into one full answer to a scenario.
  • From flowchart to code, and amending: Fig. 1 is a flowchart; the first program below is the same algorithm in pseudocode.
Key points to leave on the board
  • Initialise: totals and counts to 0; max low, min high; declare constants.
  • Input: prompt, then INPUT; validate with a loop if the question asks.
  • Process: choose FOR (known number), WHILE or REPEAT (until a condition).
  • Output: results with clear messages, after the loop when they are final.
The core visual model

One picture every idea returns to

An algorithm must be precise: write Score > Target, not "score is bigger than the target". Build a solution from the standard parts: initialise variables, prompt and input, validate, process inside a suitable loop (totalling, counting, max/min), then output clear results. Use meaningful identifiers. To amend an algorithm, keep what works and add or change only the lines needed.

STARTTotal ← 0INPUT Number Number = -1?Total ← Total + Number OUTPUTTotalSTOP YesNo
Fig. 1: a flowchart that totals numbers until -1 is entered
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Say it precisely.. The longest questions ask you to write an algorithm for a scenario, or to change one you are given. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Precision mattersExaminers can only award marks for statements a computer could follow.Whole class, then pairs
13 to 21 minTeach and check: Writing a solutionRun the standard methods programs in the lab, then see how the same patterns combine into one full answer to a scenario. Project tab 2 of the lab (Standard methods to build with) and let a student drive it while the class predicts. Model the worked example A ticket scenario on the board, one line at a time.Pairs
21 to 30 minTeach and check: From flowchart to code, and amendingFig. 1 is a flowchart; the first program below is the same algorithm in pseudocode. Your turn: With the inputs 8, 3, 7, -1, what does the amended algorithm output?Corners
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 20 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in From flowchart to code, and amending, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Corners

Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Describing the algorithm in English sentences earns little or nothing.Write statements: INPUT, OUTPUT, ←, IF, loops.
Put the validation loop inside the customer loop, around the INPUT.Validating once before the main loop checks only the first customer.
When amending, keep the original identifiers and structure.Adding a whole new loop, or renaming variables, risks breaking the parts that already worked.
Worksheet

Writing and amending algorithms: worksheet

6 questions, 20 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · write[5 marks]

Write pseudocode to input the times, in seconds, of 8 runners in a race, then output the fastest (smallest) time and the average time.

Q2 · write[4 marks]

Write pseudocode that asks the user to guess a secret number stored in Secret. The user keeps guessing until correct. Output "Too high" or "Too low" after each wrong guess, and the number of guesses at the end.

Q3 · write[3 marks]

Draw a flowchart for an algorithm that inputs a temperature and outputs "Hot" if it is above 30, otherwise "Not hot".

Q4 · write[3 marks]

Write the algorithm in Fig. 1 of this lesson in pseudocode.

Q5 · write[3 marks]

Amend the ticket algorithm in this lesson so that it also outputs the largest number of tickets bought by any one customer. State where each new line goes.

Q6 · explain[2 marks]

A student writes "keep adding the numbers until the user has finished". Explain why this would not gain full marks and rewrite it as pseudocode statements.

Answer key and marking notes
Q1 · 5 marks
  • Initialise Total ← 0 and Fastest to a large value (or the first time).
  • FOR Runner ← 1 TO 8 ... NEXT Runner, with a prompt and INPUT Time inside.
  • Total ← Total + Time inside the loop.
  • IF Time < Fastest THEN Fastest ← Time inside the loop.
  • After the loop: Average ← Total / 8; OUTPUT Fastest and Average.
Q2 · 4 marks
  • Guesses ← 0 before the loop; REPEAT ... UNTIL Guess = Secret.
  • Inside: prompt, INPUT Guess, Guesses ← Guesses + 1.
  • IF Guess > Secret THEN OUTPUT "Too high"; IF Guess < Secret THEN OUTPUT "Too low".
  • After the loop: OUTPUT Guesses.
Q3 · 3 marks
  • START and STOP terminators, joined by flow lines.
  • INPUT Temperature in an input/output symbol, then a decision Temperature > 30? with Yes and No exits.
  • Yes leads to OUTPUT "Hot", No leads to OUTPUT "Not hot" (input/output symbols), both then to STOP.
Q4 · 3 marks
  • Total ← 0 then INPUT Number.
  • WHILE Number <> -1 DO, with Total ← Total + Number and INPUT Number inside, ENDWHILE.
  • OUTPUT Total after the loop.
Q5 · 3 marks
  • Before the FOR loop: Largest ← 0.
  • Inside the FOR loop, after validation: IF Tickets > Largest THEN Largest ← Tickets.
  • After the loop: OUTPUT "Largest order: ", Largest.
Q6 · 2 marks
  • It is not precise: there is no input statement, rogue value or assignment a computer could follow.
  • e.g. INPUT Number; WHILE Number <> -1 DO; Total ← Total + Number; INPUT Number; ENDWHILE.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

A program must keep asking for a password until the user types the stored one, however many attempts that takes. Which loop is the best choice?

  • AREPEAT ... UNTIL Password = Stored
  • BFOR Attempt ← 1 TO 3 ... NEXT Attempt
  • CIF Password = Stored THEN ... ENDIF
  • DFOR Attempt ← 1 TO 100 ... NEXT Attempt
Q21 mark

Minutes holds how many minutes late a student arrived (0 if on time). Which pseudocode adds one to the count of late arrivals only when the student is late?

  • AIF Minutes > 0 THEN Late ← Late + 1 ENDIF
  • BIF Minutes > 0 THEN Late ← Late + Minutes ENDIF
  • CIF Minutes >= 0 THEN Late ← Late + 1 ENDIF
  • DIF the student is late THEN add one ENDIF
Q31 mark

The amended algorithm from the lesson is run with the inputs 9, 4, 11, -1. What is output?

Total ← 0
Count ← 0
INPUT Number
WHILE Number <> -1 DO
    Total ← Total + Number
    Count ← Count + 1
    INPUT Number
ENDWHILE
OUTPUT Total
IF Count > 0
  THEN
    OUTPUT "Average: ", Total / Count
ENDIF

  • A24, then Average: 8
  • B23, then Average: 5.75
  • C24, then Average: 6
  • D11, then Average: 11
Q41 mark

Which is the best identifier for a variable that stores the number of seats sold for a concert?

  • ASeatsSold
  • Bx
  • CSeats Sold
  • D2Seats
Q51 mark

An algorithm inputs positive numbers until -1 is entered and outputs their total. It must be amended to also output the largest number entered. Which amendment is correct?

  • ALargest ← 0 before the loop; IF Number > Largest THEN Largest ← Number inside it; OUTPUT Largest after it
  • BLargest ← 0 inside the loop; IF Number > Largest THEN Largest ← Number inside it; OUTPUT Largest after it
  • CLargest ← 0 before the loop; IF Number < Largest THEN Largest ← Number inside it; OUTPUT Largest after it
  • DLargest ← 0 before the loop; IF Number > Largest THEN Number ← Largest inside it; OUTPUT Largest after it
Q61 mark

The lesson's ticket algorithm is changed to FOR Customer ← 1 TO 4. Each customer's input is validated with a REPEAT loop (1 to 4 tickets), then TotalTickets and FourCount are updated; tickets cost $5.50. The values typed, in order, are 2, 5, 4, 0, 3, 4. What are the money taken and the number of customers who bought 4 tickets?

  • A$71.50 and 2
  • B$99.00 and 2
  • C$60.50 and 1
  • D$13.00 and 2
Quiz key
Q1: A

The number of attempts is not known in advance, so a condition-controlled loop is needed; REPEAT ... UNTIL also asks at least once. A FOR loop runs a fixed number of times, so it stops too early (3) or keeps asking after a correct answer (100). An IF statement checks once and does not repeat at all.

Q2: A

Minutes > 0 is true only for a late student, and Late ← Late + 1 counts them. Adding Minutes totals the minutes late instead of counting students. Minutes >= 0 is true for everyone, including students who were on time. An English phrase such as "the student is late" is not precise pseudocode and earns no credit.

Q3: A

9, 4 and 11 are added and counted: Total 24, Count 3, average 8. The rogue value -1 ends the loop before it is processed, so adding and counting it (23 / 4 = 5.75) is wrong, and so is dividing by 4 (6). 11 would be output only if Total and Count were reset on every pass, which they are not: they are set to 0 once, before the loop.

Q4: A

SeatsSold is meaningful and is a single word made of letters. x is legal but says nothing about the data, so it is a poor choice. An identifier cannot contain a space, and it cannot start with a digit, so Seats Sold and 2Seats are not valid identifiers.

Q5: A

Initialising once before the loop, replacing Largest whenever a bigger number arrives and outputting after the loop is the maximum standard method; 0 is a safe start because every number is positive. Resetting Largest inside the loop leaves only the last number. Using < never replaces 0 with a positive number. Number ← Largest assigns the wrong way round, so Largest stays 0.

Q6: A

Customer 1 enters 2. Customer 2 enters 5 (rejected) then 4. Customer 3 enters 0 (rejected) then 3. Customer 4 enters 4. TotalTickets = 2 + 4 + 3 + 4 = 13, so the takings are 13 × 5.50 = $71.50, and two customers bought 4. $99.00 also adds the rejected 5 and 0. $60.50 and 1 ignores the validation and treats the first four values as the four customers. $13.00 is the number of tickets, not the money taken.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonFinding and correcting errors in algorithms