KEMBAR78
Pseudo Code | PDF | Algorithms | Iteration
0% found this document useful (0 votes)
16 views15 pages

Pseudo Code

This chapter covers algorithm design and problem-solving, focusing on the program development life cycle, which includes analysis, design, coding, testing, and maintenance. It discusses the importance of decomposition in computer systems, the use of structure diagrams, flowcharts, and pseudocode for designing solutions, and introduces standard methods of solution such as totalling and counting. The document also emphasizes validation and verification checks, as well as the significance of using different types of test data.

Uploaded by

stemsinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

Pseudo Code

This chapter covers algorithm design and problem-solving, focusing on the program development life cycle, which includes analysis, design, coding, testing, and maintenance. It discusses the importance of decomposition in computer systems, the use of structure diagrams, flowcharts, and pseudocode for designing solutions, and introduces standard methods of solution such as totalling and counting. The document also emphasizes validation and verification checks, as well as the significance of using different types of test data.

Uploaded by

stemsinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

7 Algorithm design and problem

solving

Key objectives
The objectives of this chapter are to revise: ● validation and verification checks when data is
● the stages in the program development cycle: input
– analysis, design, coding, testing ● use of different types of test data including:
● computer systems and sub-systems – documentation of a dry run using a trace
● problem decomposition into component parts table
● methods used to design and construct ● writing, amending identifying, and correcting
solutions to problems errors in:
● the purpose of an algorithm and the processes – flowcharts, programs, pseudocode
involved in it
● standard methods of solution:
– totalling, counting, finding maximum,
minimum, average, linear search, bubble
sort

7.1 Program development life cycle


The program development life cycle is divided into five stages: analysis,
design, coding, testing and maintenance.

Stage

Analysis Design Coding Testing Maintenance

Process of Uses structure charts, Writing and iterative Testing the completed (Not a part of the
investigation, using flowcharts and testing of the program to make sure syllabus)
abstraction and pseudocode with the program or suite of that it works under all
decomposition to program specification programs. conditions.
specify what a from analysis to
program does. show to how the
program should be
developed.
It is illegal to photocopy this page

Sample questions and answers


The program development life cycle is divided into different stages.
Analysis is the first stage.
a) Describe the purpose of analysis.
b) Identify and describe two other stages of the program life cycle. [6]

98 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 99 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

Sample high-level answer Tips

a) Analysis is used to investigate the problem that requires a solution and find Part a) of this question
out what the program needs to do to provide that solution. asks for the purpose, so
b) Two other stages are: your answer must state
why the analysis stage is
design – document the tasks required for the solution using structure charts
needed, not what actions
and pseudocode
are to be performed. It
coding – write the programs that are required. is always very important
to read the question
carefully. Part b) of
Sample low-level answer this question involves
a) A systems analyst will use interviews and questionnaires to find out what is ‘identify’. This means
needed. your answer must either
name the two stages of
b) The stages of program development are analysis, design, coding and testing.
the program life cycle or
There is lots to do as the programs need to be written and fully tested.
clearly identify each one
with a short statement.
Teacher’s comments
The first answer would gain full marks since there is a clear description of
the purpose of analysis. Two other stages of types of the program life cycle
have been identified and described with each description clearly shown for
the stage identified.

The second answer would gain one mark for part a) since the statement
‘analysis is to find out what is needed’ partially describes the purpose of
analysis. Including the methods used answered a different question about
how analysis is carried out. For part b) all the stages of the program life
cycle are identified not just the two required. There is a description of the
actions required but it is unclear which stage these apply to. A maximum of
two marks could be awarded.

7.2 Computer systems, sub-systems and


decomposition
Each computer system is made up of software, data, hardware,
communications and people. Each computer system can be divided up into
a set of sub-systems and each sub-system can be further divided into sub-
systems and so on until each sub-system just performs a single action. The
process of decomposition into sub-systems so that a system can be more
easily represented and understood is the basis of top-down design.
Any problem that uses a computer system for its solution needs to be
decomposed into its component parts. These are inputs, processes,
It is illegal to photocopy this page

outputs and storage. The methods used to design and construct a


solution are:
● structure diagrams
● flowcharts
● pseudocode.

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 99
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 100 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.2 Computer systems, sub-systems and decomposition

7.2.1 Structure diagrams


A structure diagram shows hierarchically how each computer system can
be divided up into a set of sub-systems.
System

Sub-system 1 Sub-system 2 Sub-system 3

Sub-system 1.1 Sub-system 1.2

7.2.2 Flowcharts
A flowchart shows diagrammatically the steps required to complete a task
and the order that they are to be performed. These steps, together with
the order, are called an algorithm.
Flowcharts are drawn using standard flowchart symbols as shown in
this table.
Use Symbol Description

Terminator Used at the beginning and end of each flowchart.


START STOP
Start/Stop At least two outputs.
A ← 0 Used to show actions, for example, when values are assigned to
Process B ← 0 variables.

The same flowchart symbol is used to show the input of data and
Input/output INPUT X output of information.

Used to decide which action is to be taken next.


Decision x > B? These can be used for selection and repetition/iteration.
There are always two outputs from a decision flowchart symbol.
Flow lines Used to show the direction of flow.

7.2.3 Pseudocode
Pseudocode is a simple method of showing an algorithm. It describes
what the algorithm does by using English key words that are very similar
to those used in a high-level programming language and meaningful
identifier names.
It is illegal to photocopy this page

Mathematical operators
Operator Action
+ Add
− Subtract
* Multiply
/ Divide
^ Raise to the power
() Group

100 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 101 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

Pseudocode statements
Pseudocode statement Examples

Assignment Cost ← 10
A value is assigned to an item/variable using the ← operator. SellingPrice ← Price + Tax
Conditional 1 IF Age < 18
A condition that can be true or false: IF … THEN … ENDIF or IF … THEN
THEN … ELSE … ENDIF OUTPUT "Child"
For an IF condition the THEN path is followed if the condition is true, ELSE
and the ELSE path if it is false (an ELSE may not be required). The
OUTPUT "Adult"
end of the statement is followed by ENDIF
ENDIF
Conditional 2 CASE OF Grade
A choice between several different values: CASE OF … OTHERWISE "A" : OUTPUT "Excellent"
… ENDCASE "B" : OUTPUT "Good"
For a CASE statement, the value of the variable decides the path "C" : OUTPUT "Average"
taken. Several variables are usually specified. OTHERWISE path is
OTHERWISE OUTPUT "Improve"
taken for all other values. The statement is ended by ENDCASE
ENDCASE
Iteration 1 FOR Counter ← 1 to 10
FOR … TO … NEXT a variable is set up, with a start value and an OUTPUT "*"
end value, this variable is incremented in steps until the end value is
NEXT Counter
reached and the iteration finishes.
Iteration 2 Counter ← 0
REPEAT … UNTIL is used when the number of repetitions/ REPEAT
iterations is not known, and the actions are repeated UNTIL a
OUTPUT "*"
given condition becomes true. The actions in this loop are always
completed at least once. Counter ← Counter + 1
UNTIL Counter >= 10
Iteration 3 Counter ← 0
WHILE … DO … ENDWHILE is used when the number of WHILE Counter < 10 DO
repetitions/iterations is not known, and the actions are only
OUTPUT "*"
repeated WHILE a given condition is true. If the WHILE condition is
untrue then the actions in this loop are never performed. Counter ← Counter + 1
ENDWHILE
Input INPUT Name
INPUT used for data entry. INPUT StudentMark
Output PRINT "Your name is", Name
OUTPUT or PRINT used to display information. OUTPUT Name1, "Ali", Name3
Nesting 1 IF Age < 18
Nested IF makes use of two IF statements; the second IF THEN
statement is part of the first ELSE or THEN path
It is illegal to photocopy this page

OUTPUT "Child"
ELSE
IF Age > 65
THEN
OUTPUT "Senior"
ELSE
OUTPUT "Adult"
ENDIF
ENDIF

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 101
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 102 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.2 Computer systems, sub-systems and decomposition

Pseudocode statement Examples

Nesting 2 FOR Number ← 1 to 10


Nested iteration makes use of two loops; the second loop is inside OUTPUT Number
the first loop.
FOR Counter ← 1 to Number
OUTPUT "*"
NEXT Counter
NEXT Number

Comparison operators
Operator Comparison
> Greater than
< Less than
= Equal
>= Great than or equal
<= Less than or equal
<> Not equal
AND Both
OR Either
NOT Not

Sample questions and answers


a) A computer system can be decomposed into its component parts. One
of the parts is input.
Identify two other component parts.
b) Formal methods are used to show a proposed solution.
Method 2
i) Identify the formal methods shown.
START

Method 1

Password checker Password


checker

Check for eight or Check for at least Check for at least


more characters one digit one special character LENGTH
(Password) yes Accept ← TRUE
>= 8?

no
It is illegal to photocopy this page

Accept ← FALSE

STOP

ii) Identify another formal method that could be used to check if the
password has more than eight characters.
iii) Use the formal method you identified in part b)ii) to show the
same routine to check if the password has more than eight
characters. [8]

102 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 103 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

Tip
Part a) of this question asks for two other component parts; you
must not use the part mentioned in the question. It is surprising how
many students do this and throw away a mark. Part b)i) and ii) of this
question involves ‘identify’. This means your answer must either name
the methods or clearly identify each one with a short statement. Part
b)iii) needs the same routine to be rewritten using pseudocode, not a
programming language.

Sample high-level answer


a) Process and output
b) i) The two methods are:
Method 1 – structure diagram
Method 2 – flowchart
ii) Pseudocode
iii) IF LENGTH (Password) >= 8
THEN Accept ← TRUE
ELSE Accept ← FALSE
ENDIF

Sample low-level answer


a) input and output
b) i) The two methods are:
Method 1 – structure chart
Method 2 – flow diagram
ii) Code
iii) IF Pwd.length > 8:
OK = Yes
ELSE: OK = No

Teacher’s comments
The first answer would gain full marks since two other components are
identified, the methods are identified correctly, and the routine rewritten
exactly matched the flowchart given and used the same identifiers,
Password and Accept.

The second answer would gain one mark for part a) since only one other
method was identified. For part b)i) the methods are identified, but the
terminology used was not accurate, so one mark. For parts b)ii) and iii)
code is used for the actual solution and the student has gone on to write a
It is illegal to photocopy this page

solution in Python that did not exactly match the flowchart. A maximum of
two marks could be awarded.

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 103
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 104 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.4 Standard methods of solution

7.3 Explaining the purpose of an algorithm


An algorithm sets out the steps to complete a given task. This is usually
shown as a flowchart or pseudocode, so that the purpose of the task and
the processes needed to complete it are clear to those who study it.

Worked example
START INPUT Num1, Num2
IF Num1 > Num2

INPUT
THEN PRINT NUM1, " is largest"
Num1, Num2 ELSE PRINT NUM2, " is largest"
ENDIF

Num1 > yes PRINT Num1, Purpose


Num2? " is largest"
To input two numbers, divide the larger number by
the smaller number and output the result.
no

PRINT Num2,
" is largest"

STOP

7.4 Standard methods of solution


You need to be able to use and understand these standard methods used
in algorithms.
Totalling
Sample pseudocode Notes
Total ← 0 Totalling adds up all the marks for
FOR Count ← 1 TO ClassSize students in the class.
INPUT Mark When totalling, the total is increased by
the value of the mark.
Total ← Total + Mark
Always set the total to zero before
NEXT Count checking the mark input.

Counting
It is illegal to photocopy this page

Sample pseudocode Notes


PassCount ← 0 Counting counts the number of students
FOR Counter ← 1 TO ClassSize whose mark is over 50.
INPUT Mark When counting the counter is always
increased by 1.
IF Mark > 50
THEN
PassCount ← PassCount + 1
ENDIF
NEXT Counter

104 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 105 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

Finding maximum, minimum, average


Sample pseudocode Notes
Total ← 0 To find the maximum and minimum
MaxMark ← 0 values every value must be checked.
To find the average all values must be
MinMark ← 100 totalled then divided by the number of
FOR Count ← 1 TO ClassSize values.

INPUT Mark Always set the minimum value to the


largest value possible, always set the
IF Mark > MaxMark maximum value to the lowest value
possible, before checking the mark
THEN
input.
MaxMark ← Mark
ENDIF
IF Mark < MinMark
THEN
MinMark ← Mark
ENDIF
Total ← Total + Mark
NEXT Count
Average ← Total / ClassSize

Linear search
Sample pseudocode Notes
OUTPUT "Enter name to find " A linear search inspects each
INPUT Name name in a list in turn to see if
the name matches the name
Found ← FALSE that was input.
Counter ← 1 The search stops as soon as the
REPEAT name is found.

IF Name = Name[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
It is illegal to photocopy this page

THEN
OUTPUT Name, " found"
ELSE
OUTPUT Name, " not found."
ENDIF

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 105
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 106 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.6 Test data

Bubble sort
Sample pseudocode Notes
First ← 1 A bubble sort compares each
Last ← ClassSize name in the list is with the next
name and swaps them if the
REPEAT names are in the wrong order,
Swap ← FALSE starting from the first name and
finishing with the next to last
FOR Index ← First TO Last - 1 name.
IF Name[Index] > Name[Index + 1] The last name is in the right
THEN place once the list has been
checked, this is repeated,
Temp ← Name[Index]
decreasing the size of list
Name[Index] ← Name[Index + 1] by one, until no names are
Name[Index + 1] ← Temp swapped or there are no names
left to be swapped.
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last -1
UNTIL (NOT Swap) OR Last = 1

7.5 Validation and verification


For data entry, validation ensures that only data that is reasonable is
For check digits, please
accepted. Verification is used to check that the data does not change as see Section 2.2.3 on
it is being entered. Different types of checks may be used on the same page 21, where they are
piece of data. covered more fully.

Data entry

Verification Validation

Double Screen/visual Range Length Type Presence Format


entry check check check check check check

The data is A manual Checks that Checks that Checks that Checks to Checks that
entered twice check to the value of the data the data ensure that the characters
and compared ensure the a number is entered is a entered is of some data has entered
to ensure both data on the between an reasonable a given data been entered conform to a
It is illegal to photocopy this page

entries are the screen is the upper value number or an type and the value pre-defined
same same as the and a lower exact number has not been pattern
form value of characters left blank

7.6 Test data


Test data ensures that an algorithm works as expected, completing all
parts of the solution with no errors. It is used with trace tables to check
pseudocode and flowcharts work as expected. Sets of test data are used
with programs to ensure outputs are as expected.

106 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 107 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

Test data

Abnormal/
Normal Boundary Extreme
erroneous

Test data that is Test data that is At each The largest and
accepted and the rejected by the boundary two smallest values
algorithm is algorithm as not values are that normal data
expected to suitable required; one can take
work with value is accepted
and the other
value is rejected

7.7 Trace tables


A trace table records the results from each step in an algorithm; it shows
the value of each variable every time that it changes. Working through an
algorithm step by step is called a dry run. A trace table is set up with a
column for each variable and a column for any output. For example:
A B C X OUTPUT
0 0 100

Test data is used to dry run the flowchart and record the results in the trace
table.

7.8 Identifying errors in algorithms


Trace tables and test data can be used to identify errors and correct
errors.

Sample questions and answers


Look at this algorithm.
01 Small ← 0
02 Counter ← 0
03 REPEAT
It is illegal to photocopy this page

04 INPUT Num
05 IF Num < Small
06 THEN
07 Small ← Num
08 ENDIF
09 Counter ← Counter + 1
10 UNTIL Counter ← 10
11 INPUT "Smallest number is ", Small

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 107
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 108 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.8 Identifying errors in algorithms

a) Find the three errors in the algorithm and suggest a correction for Tips
each error.
b) Write the corrected algorithm as a flowchart. Part a) of this question
asks you to identify the
c) Describe the purpose of the corrected algorithm.
errors and provide a
d) Complete the trace table for the corrected algorithm using this corrections; there will
input data: probably be only one
7, 11, 3, 9, 6, 8, 4, 2, −3, 13, −1, −99 mark for this. You need to
e) Extend the pseudocode algorithm to validate that data input is ensure that each error is
positive. clearly identified, quoting
f) Write an extended pseudocode algorithm to find the average of the the line number if given
data input. [18] in the question can help.
Part b) requires a
Sample high-level answer flowchart. For full marks
you will need to have the
a) 01 should be Small ← 10000
correct structure and
10 should be UNTIL Counter = 10
the correct content. For
11 should be OUTPUT Small the structure, you must
b) use the correct flowchart
START
symbols as given in the
syllabus, ensure that
each flowline has an
arrow and clearly label
Small ← 10000 the flowlines out of a
Counter ← 0 decision.
In part c) for the purpose,
you answer must
INPUT state exactly what the
Num algorithm does.
In part d), the trace table
must only contain the
data that the algorithm
NUM < yes uses; sometimes there
Small ← Num
Small?
can be extra values
included that are not
no needed.
In part e) where
Counter ← validation is required,
Counter + 1
the algorithm must only
allow further actions
if data that meets the
required rules is entered.
no Counter
In part f), any pseudocode
= 10?
written should be
correctly indented
It is illegal to photocopy this page

yes and have meaningful


identifier names so the
OUTPUT "smallest intended logic of the
number is ", Small algorithm written can
be easily understood by
the examiner reading the
STOP
answer.

108 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 109 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

c) Input ten numbers, find and output the smallest number input.
Teacher’s comments
d)
The first answer would
Counter Num Small OUTPUT
gain full marks since in
0 10000 part a) all the errors have
1 7 7 clearly identified and
corrected, quoting the
2 11
line number of the error.
3 3 3 The student has drawn
4 9 a flowchart showing the
corrected algorithm for
5 6 part b), it has the correct
6 8 structure and the correct
content. Part c) the
7 4
student states exactly
8 2 2 what the algorithm does.
9 −3 −3 Part d) the trace table
contains the data that the
smallest
10 13
number is −3 algorithm uses, and the
correct output.
e) Replace line 4 with:
04 REPEAT Part e) the input is
05 INPUT Num validated, and the
06 UNTIL Num > 0 algorithm only allows
f) further actions if data
that meets the required
01 Small ← 10000 rules has been entered. An
error message could have
02 Counter ← 0
been included. Part f) the
03 Total ← 0 pseudocode written is
04 REPEAT accurate and readable; the
only improvement could
05 REPEAT be to add comments to
06 INPUT Num explain the functionality
of the pseudocode.
07 UNTIL Num > 0
08 IF Num < Small
09 THEN
10 Small ← Num
11 ENDIF
12 Total ← Total + Num
13 Counter ← Counter + 1
14 UNTIL Counter = 10
It is illegal to photocopy this page

15 Average ← Total / Counter


16 
OUTPUT "Smallest number is ", Small," average is",
Average

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 109
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 110 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.8 Identifying errors in algorithms

Sample low-level answer Teacher’s comments


a) Small ← should be Small ← 1000 The second answer would
← 10 should be = 10 gain one mark for part a)
INPUT should be OUTPUT since only one error has
b) been uniquely identified
START
and corrected. The errors
in the assignment and
input could be in two
Small ← 1000
places as the line number
Counter ← 0
has not been quoted
and there is insufficient
code quoted to uniquely
Num identify the position.
The flowchart for part b)
has a STOP missing, not
all flowlines are clearly
labelled, and it is unclear
NUM <
Small?
Small ← Num where the variables are
being input or output.
no
There is also an error in
the logic as the values
are reinitialised every
Counter ←
Counter + 1 iteration of the loop.
Part c) the answer does
not state exactly what
the algorithm does,
the number of values is
equals
10? missing and the need
for input is also missing.
yes
Part d) the trace table
must only contain the
"Smallest number data that the algorithm
is ", Small uses; extra values
that are not needed
have been included for
c) Find the smallest number.
Num, showing that the
d) Counter Num Small OUTPUT
algorithm has not been
1000 traced, and providing
1 7 7 an incorrect output. The
output incorrectly has
2 11
quotation marks round it.
3 3 3 Part e) the algorithm still
4 9 allows further actions
5 6 if the data entered is
It is illegal to photocopy this page

negative; only a warning


6 8
message is output. Part f)
7 4 the pseudocode written
8 2 2 is not correctly indented
9 −3 −3 and does not have
meaningful identifier
10 13
names.
−1
“Smallest
−99 −99
number is −99”
e) 04 INPUT Num
05 IF Num < 0 THEN OUTPUT "Not positive"

110 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 111 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7 Algorithm design and problem solving

f)
01 Small ← 0
02 Counter ← 0
04 REPEAT
05 INPUT Num
06 IF Num < 0 THEN OUTPUT "Not positive"
07 IF Num < Small THEN Small ← Num
08 T ← T + Num
09 Counter ← Counter + 1
10 UNTIL Counter = 10
11 OUTPUT Small, T / Counter

7.9 Writing and amending algorithms


There are a number of stages when producing an algorithm for a problem.
1 Make sure that the problem is clearly specified – the purpose of the
algorithm and the tasks to be completed by the algorithm.
2 Break the problem down in to sub-problems. If it is complex, you may
want to consider writing an algorithm for each sub-problem. Most
problems, even the simplest ones can be divided into:
l set-up processes l permanent storage of data
l input (if required)
l processing of data l output of results.
3 Decide on how any data is to be obtained and stored, what is going to
happen to the data and how any results are going to be displayed.
4 Design the structure of your algorithm using a structure diagram.
5 Decide on how you are going to construct your algorithm, either using
a flowchart or pseudocode. If you are told how to construct your
algorithm, then follow the guidance.
6 Construct your algorithm, making sure that it can be easily read
and understood by someone else. Precision is required when writing
algorithms, just as it is when writing program code. This involves setting
it out clearly and using meaningful names for any data stores.
7 Use several sets of test data (normal, abnormal and boundary) to dry
run your algorithm and show the results in trace tables, to enable you
to find any errors.
8 If any errors are found, correct them and repeat the process until you
think that your algorithm works perfectly.
Exam-style questions
It is illegal to photocopy this page

1 Tick (✓) the appropriate column, in the following table, to indicate whether the description
applies to validation, verification or neither.
You may need to tick more than one column in each row.  [5]
Description Validation Verification Neither
Ensuring a number is positive
Ensuring a password is as intended
Ensuring a password is correct
Ensuring a password contains a number
Ensuring a password contains a special character

Hodder & Stoughton Limited © David Watson and Helen Williams 2022 111
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 112 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd

7.9 Writing and amending algorithms

2 In this question you will be given a statement followed by four possible answers. Select which
of the four answers you think is correct and tick (✓) the box next to your answer.
a) This is a conditional statement:
® Answer ← Number1 + Number2
® REPEAT … UNTIL Number1 = Number2
® IF Number1 = Number2 THEN …
® WHILE Number1 = Number2 …
b) What is meant by the term decomposition?
® Discarding unnecessary details.
® Breaking a complex problem into smaller parts.
® Showing how a program should be developed.
® Writing and testing a program.
c) What is meant by the term abstraction?
® Identifying data that needs to be stored.
® Breaking sub-systems down into smaller sub-systems.
® Keeping the key elements and discarding information not needed.
® Stage of the program development life cycle. [3]
3 a) Write pseudocode to input 12 numbers and store them in an array.
b) Change your pseudocode to use a different loop structure.
c) Identify another loop structure you could have used.
d) Write pseudocode to find the largest, smallest and the average of the numbers you have stored.
e) Write pseudocode to sort the numbers you have stored in descending order. [16]
4 Explain the difference between mathematical and comparison operators. Include an example
of each type in your answer. [4]
5 Identify three types of test data. Include an example of each
type in your answer with an explanation of why it is appropriate. [9]
6 Identify four errors in this algorithm. [4]

01 NumberProducts ← 50 // length of array ProductName[]


02 OUTPUT "Please enter product to find "
03 INPUT Product
04 Found ← FALSE
05 Counter ← 1
06 REPEAT
07 IF Product = ProductName[Counter]
08 THEN
09 Found ← FALSE
10 ELSE
11 Counter ← Counter - 1
12 ENDIF
13 UNTIL Found AND Counter > NumberProducts
It is illegal to photocopy this page

14 IF Found
15 THEN
16 OUTPUT Product, " found at position ", Counter, " in the list."
17 ELSE
18 OUTPUT Name, " not found."
19 ENDIF

112 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition

You might also like