WA

7.9 Writing & Amending Algorithms

7.9 • Modifying or creating solutions using pseudocode / flowcharts

Learning Objectives

  • analyse a problem statement and identify inputs, processing and outputs
  • break a problem into clear algorithmic steps
  • create algorithms using Cambridge IGCSE pseudocode
  • represent algorithms using flowcharts
  • amend an existing algorithm when a requirement changes
  • select sequence, selection and iteration appropriately
  • trace an existing solution before changing it
  • test newly written and amended algorithms with suitable data
  • check that a change has not broken required existing behaviour
  • use clear identifiers and readable algorithm structure

Key Terms

TermSimple definition
AlgorithmA sequence of steps used to solve a problem.
RequirementWhat the problem says the solution must do.
InputData supplied to the algorithm.
ProcessingCalculations or decisions performed on data.
OutputInformation produced by the algorithm.
PseudocodeA structured text description of an algorithm.
FlowchartA visual description of an algorithm using symbols and arrows.
AmendChange an existing algorithm to meet a new or changed requirement.
SelectionChoosing between different paths using a condition.
IterationRepeating a statement or group of statements.
Dry-runManually following an algorithm using test data.
Logic errorThe solution behaves incorrectly because the logic is wrong.

1. Understand the Problem Before Writing

Before writing an algorithm, turn the question into a small set of precise requirements.

AskExample: student result system
What is the input?Student mark
What is the processing?Compare the mark with the pass threshold
What is the output?Pass or Fail
  • Identify the data entering the solution.
  • Identify calculations, comparisons and decisions.
  • Identify exactly what must be displayed or returned.
  • Look for words such as IF, OTHERWISE, FOR, EACH, REPEAT and UNTIL.
  • Break complex requirements into smaller steps before coding.
Real-life example:A school attendance program takes attendance data, processes it and displays a result. That same input → processing → output thinking helps when writing an algorithm.

Activity 1A: Identify IPO

Classroom / homework activity

A program accepts a mark and displays Pass when the mark is 50 or above.

PartAnswer
InputMark
ProcessCompare mark with 50
OutputPass or Fail

Activity 1B: Convert the requirement

Classroom / homework activity

Requirement: enter two numbers, add them and display the result.

  • Input the first number.
  • Input the second number.
  • Add the numbers.
  • Output the result.

Check Your Understanding

  • It clarifies what data is available.
  • It identifies what the algorithm must do.
  • It identifies the required result.
  • It reduces the chance of missing a requirement.
  • If can indicate a decision.
  • Otherwise can indicate an alternative path.
  • A condition normally controls which branch is followed.
  • In pseudocode this is commonly represented by IF.
  • For can indicate a known number of repetitions.
  • Each can indicate repeated processing.
  • Repeat and until can indicate condition-controlled repetition.
  • Recognising these words helps select a loop structure.
  • Smaller steps are easier to understand.
  • Missing actions are easier to identify.
  • Each step can be checked.
  • The steps can then be represented in pseudocode or a flowchart.
  • It defines what the solution must produce.
  • It helps identify the required processing.
  • It helps identify the final output statement.
  • It provides something concrete to test.

2. Create an Algorithm from a Requirement

Creating an algorithm means converting the requirements into a logical sequence of instructions.

StageWhat to decide
InputsWhat values are needed?
VariablesWhat values need to be stored?
ProcessingWhat calculations or checks are required?
Control flowIs sequence, selection or iteration needed?
OutputWhat result must be produced?
Cambridge IGCSE pseudocode
DECLARE Mark1 : REAL
DECLARE Mark2 : REAL
DECLARE Average : REAL

INPUT Mark1
INPUT Mark2

Average ← (Mark1 + Mark2) / 2

OUTPUT "Average = ", Average

Cambridge specifies declarations in the formDECLARE <identifier> : <data type>and usesfor assignment. Identifiers use mixed/Pascal case. fileciteturn19file0L72-L78 fileciteturn19file0L85-L100 fileciteturn19file0L105-L115

Activity 2A: Write a rectangle algorithm

Classroom / homework activity

Input Width and Height, calculate Area and output Area.

Model answer
DECLARE Width : REAL
DECLARE Height : REAL
DECLARE Area : REAL

INPUT Width
INPUT Height
Area ← Width * Height
OUTPUT Area

Activity 2B: Add selection

Classroom / homework activity

Amend the rectangle solution so Area greater than 100 outputs Large; otherwise output Small.

Model amendment
IF Area > 100
  THEN
    OUTPUT "Large"
  ELSE
    OUTPUT "Small"
ENDIF

Check Your Understanding

  • Identify the required input.
  • Identify the processing.
  • Identify the output.
  • Choose suitable control structures.
  • They describe the stored value.
  • They make the algorithm easier to read.
  • They reduce ambiguity.
  • They help another programmer understand the solution.
  • It is ←.
  • It assigns a value to a variable or data item.
  • It can assign the result of an expression.
  • For example, Total ← Total + Number.
  • It shows which statements are contained inside another statement.
  • It makes IF structures clearer.
  • It makes loops clearer.
  • It reduces ambiguity in nested structures.
  • The logic can be checked before syntax.
  • Missing steps are easier to find.
  • The plan can be translated into pseudocode.
  • The same logic can be represented by a flowchart.

3. Amend an Existing Algorithm

When a requirement changes, the existing algorithm may need to be amended rather than completely rewritten.

  • Read the complete existing algorithm first.
  • State exactly what has changed.
  • Locate the affected part.
  • Keep correct existing logic whenever possible.
  • Make the smallest suitable change.
  • Trace and test the entire amended solution.
Original
INPUT Mark
IF Mark >= 50
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
ENDIF
Amended
INPUT Mark
IF Mark >= 50
  THEN
    IF Mark >= 80
      THEN
        OUTPUT "Distinction"
      ELSE
        OUTPUT "Pass"
    ENDIF
  ELSE
    OUTPUT "Fail"
ENDIF
Real-life example:A school grading program already working for Pass/Fail may be amended to add a Distinction band. You do not need to redesign the entire grading system; add the new decision where it belongs.
FlowchartStartRead MarkMark≥ 50?NoPrint"Fail"YesMark≥ 80?NoPrint"Pass"YesPrint"Distinction"End

The completed flowchart shows the original Pass/Fail decision and the new Distinction decision added only to the Pass branch.

Interactive amendment checker

Activity 3A: Change the pass mark

Classroom / homework activity

Change the pass threshold from 50 to 60.

Model amendment
INPUT Mark
IF Mark >= 60
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
ENDIF

Activity 3B: Add another grade

Classroom / homework activity

Add Excellent when the mark is 90 or above.

Model amendment
INPUT Mark
IF Mark >= 60
  THEN
    IF Mark >= 90
      THEN
        OUTPUT "Excellent"
      ELSE
        OUTPUT "Pass"
    ENDIF
  ELSE
    OUTPUT "Fail"
ENDIF

Check Your Understanding

  • You need to understand the existing logic.
  • The change may depend on earlier steps.
  • You might remove correct logic accidentally.
  • You need to know which existing cases should continue working.
  • It reduces the risk of breaking working logic.
  • The amendment is easier to understand.
  • Testing becomes easier.
  • The solution remains closer to the original.
  • It keeps the original Pass/Fail decision.
  • It adds a second condition.
  • The second condition is inside the Pass branch.
  • Marks of 80 or more produce Distinction.
  • A change may introduce a new logic error.
  • Old cases may still be required.
  • New cases need to demonstrate the changed requirement.
  • Testing the whole solution checks for unintended effects.
  • A change causes another part of the solution to stop working correctly.
  • The new requirement may work while an old case fails.
  • The output may become incorrect for previous inputs.
  • Careful testing can reveal it.

4. Flowcharts — Create and Amend Visually

The flowchart below is the visual version of the same Pass/Fail decision.

SymbolMeaning
TerminatorSTART / END
RectangleProcess / calculation
ParallelogramINPUT / OUTPUT
DiamondDecision / condition
ArrowDirection of flow
START
INPUT Mark
Mark ≥ 50?
YES
OUTPUT "Pass"
NO
OUTPUT "Fail"
FlowchartStartReadMarkMark≥ 50?NoPrint"Fail"YesPrint"Pass"End

Activity 4A: Plan the flowchart

Classroom / homework activity

For a program that validates a mark from 0 to 100, identify the major flowchart stages.

StageAction
1START
2INPUT Mark
3Decision: valid?
4Output/process the selected path
5END or repeat if required

Proper Flowchart — Pass / Fail

STARTINPUT MarkMark ≥ 50?OUTPUT "Fail"OUTPUT "Pass"ENDNOYES

A decision diamond creates the two paths. Both paths eventually rejoin at END.

Activity 4B: Add a new branch

Classroom / homework activity

A Pass/Fail flowchart now needs Distinction at 80 or above.

Add a second decision on the existing Pass branch:Mark ≥ 80?. This is the flowchart equivalent of the nested IF.

Check Your Understanding

  • It represents a decision condition.
  • It creates different branches.
  • Each branch represents an outcome.
  • The selected branch determines the next step.
  • They show execution order.
  • They connect the stages.
  • They show where each branch goes.
  • They make the control flow understandable.
  • Identify the changed requirement.
  • Find the affected symbol or branch.
  • Add or replace only the necessary part.
  • Check all arrows after the change.
  • Both represent a condition.
  • Both can produce different paths.
  • A diamond is the visual form.
  • IF is a pseudocode form.
  • It gives a visual overview.
  • Branches and loops are easy to see.
  • Missing paths may be detected early.
  • The logic can then be translated into pseudocode.

5. Choose the Correct Control Structure

StructureUse it forExample
SequenceSteps that always happen in orderINPUT → calculate → OUTPUT
SelectionA decision changes the pathIF Mark >= 50 THEN ...
IterationThe same work must repeatFOR Counter ← 1 TO 10

An algorithm can contain all three structures. When amending it, identify which structure the new requirement actually changes.

Activity 5A: Choose a structure

Classroom / homework activity

Choose the best structure for: calculate once; choose Pass/Fail; process ten scores.

TaskStructure
Calculate onceSequence
Pass/FailSelection
Process ten scoresIteration

Activity 5B: Change the repetition

Classroom / homework activity

A program processes 5 scores. The requirement changes to 10 scores.

Change the FOR loop boundary from1 TO 5to1 TO 10, provided the rest of the algorithm remains correct.

Check Your Understanding

  • All actions happen in order.
  • No decision changes the path.
  • No repeated block is required.
  • It is the simplest control structure.
  • A condition changes what happens next.
  • Different cases can have different actions.
  • IF is commonly used in pseudocode.
  • A flowchart diamond can represent it.
  • The same work repeats.
  • FOR suits a known number of repetitions.
  • WHILE or REPEAT can suit condition-controlled repetition.
  • The loop needs a suitable stopping rule.
  • Yes.
  • Sequence can contain selection.
  • Loops can contain selection.
  • The structures can be combined to solve larger problems.
  • Compare the old and new requirements.
  • Identify the affected control structure.
  • Change the necessary part.
  • Test the whole solution afterwards.

6. Test the New or Amended Algorithm

TestPurposeExample
NormalCheck an ordinary valid case65 for Pass at 50
BoundaryCheck the exact limit and nearby values49, 50, 51
AbnormalCheck invalid/out-of-range input-5 or 105 for marks 0–100
Changed requirementCheck new behaviour79, 80, 81 for Distinction at 80

Worked test set

MarkExpected output
45Fail
50Pass
79Pass
80Distinction
95Distinction

Exam tip

When a rule uses a boundary such as 50 or 80, test the boundary itself and values immediately below and above it. This helps detect mistakes such as > instead of >=.

Activity 6A: Select boundary tests

Classroom / homework activity

For Pass if Mark >= 50, choose three useful tests.

InputReason
49Below boundary
50At boundary
51Above boundary

Activity 6B: Test the new Distinction rule

Classroom / homework activity

For Distinction at 80 or above, choose three boundary tests.

InputExpected
79Pass
80Distinction
81Distinction

Check Your Understanding

  • It is where a decision can change branch.
  • It checks the comparison operator.
  • It confirms the threshold is handled correctly.
  • Nearby values add further evidence.
  • An ordinary valid input.
  • It checks expected everyday behaviour.
  • It normally follows the main path.
  • It helps confirm the solution works normally.
  • An invalid or out-of-range input.
  • It checks validation or rejection.
  • It can reveal missing conditions.
  • The chosen value should match the limits in the requirement.
  • Old behaviour may still be required.
  • The amendment could break a previously correct case.
  • New cases must show the changed requirement works.
  • Testing both groups checks the complete solution.
  • It records changing variable values.
  • It shows which branches are followed.
  • It can reveal where new logic becomes incorrect.
  • It supports checking the final output.

7. Exam-Style Writing & Amending Practice

Worked Example — Create then amend

Original:input five scores and output the total.

CIE pseudocode
DECLARE Total : INTEGER
DECLARE Score : INTEGER

Total ← 0

FOR Counter ← 1 TO 5
    INPUT Score
    Total ← Total + Score
NEXT Counter

OUTPUT Total

Changed requirement:also output "High" when Total is 240 or more; otherwise "Standard".

CIE pseudocode amendment
DECLARE Total : INTEGER
DECLARE Score : INTEGER

Total ← 0

FOR Counter ← 1 TO 5
    INPUT Score
    Total ← Total + Score
NEXT Counter

OUTPUT Total

IF Total >= 240
  THEN
    OUTPUT "High"
  ELSE
    OUTPUT "Standard"
ENDIF

Flowchart Version — Total of Five Scores

STARTTotal ← 0INPUT ScoreTotal ← Total + ScoreMore scores?OUTPUT TotalENDYESNO

The loop in the pseudocode becomes a repeated flowchart path back to INPUT Score.

FlowchartStartTotal ← 0Input ScoreTotal ← Total + ScoreMorescores?YesNoOutput TotalEnd

Activity 7A: 5-mark algorithm

Classroom / homework activity

Write an algorithm to input five numbers and output the total.

Model answer
DECLARE Total : INTEGER
DECLARE Number : INTEGER
Total ← 0

FOR Counter ← 1 TO 5
    INPUT Number
    Total ← Total + Number
NEXT Counter

OUTPUT Total

Activity 7B: 4-mark amendment

Classroom / homework activity

Add an output for Total >= 100.

  • Keep the existing total calculation.
  • Add a condition testing Total >= 100.
  • Output the required message when TRUE.
  • Provide the specified alternative behaviour when FALSE.

Check Your Understanding

  • A condition changes the action.
  • Words such as if or otherwise are clues.
  • Different cases require different behaviour.
  • Use an IF or a flowchart decision.
  • Work is repeated.
  • Words such as each or every are common clues.
  • A FOR loop can handle a known repetition count.
  • A condition-controlled loop needs a stopping condition.
  • It already satisfies part of the requirement.
  • Unnecessary changes increase the risk of errors.
  • The amendment is easier to review.
  • Testing becomes more focused.
  • They represent the same underlying algorithm.
  • The processing should remain equivalent.
  • The same conditions should produce the same branches.
  • Only the representation changes.

Key Takeaways

  • Understand the requirement before writing or changing the algorithm.
  • Identify input, processing and output.
  • Break complex requirements into clear steps.
  • Use sequence, selection and iteration to match the task.
  • Use Cambridge IGCSE pseudocode conventions for pseudocode answers.
  • Read the existing solution before amending it.
  • Make the smallest necessary change and preserve correct logic.
  • Use flowchart symbols and arrows to show the same underlying logic.
  • Test normal, boundary and abnormal cases where appropriate.
  • Test both the new behaviour and important existing behaviour.
  • Use tracing/dry-runs to check changing values and decisions.

Question Bank

  • Identify the required inputs.
  • Identify the processing or calculations.
  • Identify the required outputs.
  • Choose suitable sequence, selection and/or iteration.
  • Write the solution and test it.
  • Read the original algorithm.
  • Identify the changed requirement.
  • Change the necessary part while keeping correct logic.
  • Test the complete amended solution.
  • Pseudocode describes the logic with structured text.
  • A flowchart describes the logic with symbols and arrows.
  • Both can represent sequence, selection and iteration.
  • The control flow should be equivalent.
  • The representation changes, but the underlying logic remains the same.
  • Use normal valid data.
  • Test boundary values.
  • Test abnormal or invalid data when appropriate.
  • Check expected and actual outputs.
  • Retest important existing cases as well as new cases.
  • Sequence executes steps in order.
  • Selection chooses a path based on a condition.
  • Iteration repeats a statement or block.
  • Choose the structure that matches the requirement.
  • Keep the existing input.
  • Keep the outer pass condition.
  • Add a second condition for 80 or above.
  • Output Distinction when the second condition is TRUE and Pass otherwise.
  • Test values below 50, 50–79 and 80 or above.
  • Meaningful identifiers make data and purpose clear.
  • Cambridge uses mixed/Pascal case identifiers.
  • Indentation shows contained statements.
  • Clear structure makes the algorithm easier to follow.
  • Identify START and END.
  • Add input and output symbols.
  • Add process steps for actions/calculations.
  • Add decision symbols and connect each branch.
  • Check that the arrows give a complete control flow.