Uploaded by trevor zvidza

ALGORITHM DESIGN AND PROBLEM SOLVING

advertisement
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.1 Development Life CycleProgram Development Life Cycle - Analysis
Program Development Life
Cycle - Analysis
Abstraction
This is the act of removing unimportant details of the problem to focus on important
elements. An example of abstraction would be the London underground train route
map; travellers do not need to know the geographical layout of the routes, only that
getting on at stop A will eventually transport you to stop B
Figure 1: London Underground train route map
Figure 2: The geographical London underground train map
Requirements
•
Identification of the problem: Before tackling a problem, it needs to be
clearly understood by everyone working on it. The overall goal of the solution
needs to be agreed as well as any constraints such as limited resources or
requiring a platform specific solution
•
Requirements: To create a solution, a requirements document is created to
define the problem and break it down into clear, manageable, understandable
parts by using abstraction and decomposition. A requirements document
labels each requirement, gives it a description as well as success criteria
which state how we know when the requirement has been achieved
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.1 Development Life CycleProgram Development Life Cycle - Design
Program Development Life
Cycle - Design
Download PDF
Test Yourself
Decomposition
•
This is the act of breaking down a large problem into smaller, clear,
manageable and understandable sub-parts. Sub-parts can be divided until
they are easily solvable and cannot be broken down any further. An example
of decomposition could be getting ready in the morning to go to school
o Step 1: Wake up
o Step 2: Get breakfast
o Step 3: Brush teeth
o Step 4: Put clothes on
o Step 5: Make sure the bag and school supplies are ready
o Step 6: Find transport to school e.g. walk, bus, car, bike, etc
•
These steps could be further subdivided, for example, “Step 2: Get breakfast”
would entail:
o Step 2.1 Get a bowl
o Step 2.2 Get cereal
o Step 2.3 Get milk
o Step 2.4 Get a spoon
o Step 2.5 Put cereal in a bowl
o And so on…
•
Once the requirements document has been created, developers need to
design the structure and algorithms to solve the problem:
o Structure charts are created to show the breakdown of tasks in a
hierarchy
o Flowcharts may be created to visually show how tasks should be
carried out
o
Pseudocode is created, sometimes from flowcharts, to allow
programmers to easily translate task instructions into programming
code
•
The design of a solution identifies what tasks need completing, how to
complete the tasks and how each task works together with other tasks
•
A computer system includes several components that work together:
software, hardware, data, networking and people
Systems can be broken down into sub-systems that can be further broken
down into more sub-systems, until each sub-system has a single purpose.
This decomposition is known as top-down design
•
Decomposing a system
•
•
•
•
•
•
To create an overall system and solve a problem, it must first be broken down
into subsystems that are easier to solve and create. The act of breaking down
the problem is known as stepwise refinement
Decomposing the problem this way creates smaller, more manageable and
more easily understandable sub-parts
Each sub-system can be assigned to a developer or group of developers who
create subroutines from these sub-systems. Each sub-system can then be
created at the same time, reducing development and testing time, especially
on large projects
Decomposing the system using stepwise refinement requires developers to
think about four key areas:
o Inputs: data entered into the system
o Processes: subroutines and algorithms that turn inputs and stored data
into outputs
o Outputs: data that is produced by the system, such as information on a
screen or printed information
o Storage: data that is stored on a physical device, such as on a hard
drive
To solve a problem all aspects must be thoroughly understood by the
developers
There are many methods used to design and construct solutions. Three such
methods are illustrated below:
Structure Diagrams
•
Structure diagrams show hierarchical top-down design in a visual form. Each
problem is divided into sub-problems and each sub-problem divided into
further sub-problems. At each level the problem is broken down into more
detailed tasks that can be implemented using a single subroutine
Figure 1: A structure diagram
Figure 2: A structure diagram for a mobile application
Worked example
A satellite navigation system is an example of a computer system that is made up of
subsystems. Part of a satellite navigation system: allows the user to enter details for
a new destination or select a previously saved destination and displays directions in
the form of a visual map or as a list. Draw a structure diagram for this part of the
satellite navigation system.
[4]
•
•
[1] for a hierarchical structure
[1] for suitable names for the sub-systems
• [1] for identifiable inputs
• [1] for identifiable outputs
Flowcharts
•
•
•
Flowcharts show how algorithms can be represented visually in a
diagrammatic format
Each flowchart has a start and an end with arrows showing the order each
task or instruction needs to be carried out in
Flowcharts are made of several symbols:
o Terminator symbols: Also known as Begin/End symbols. These
indicate where a flowchart starts and stops
o Process symbols: These show simple actions being performed such as
assigning values or performing arithmetic operations on values
▪ Processes can also represent other flowcharts or summarised
actions. For example, searching or sorting a list is a complex
process which would require its own flowchart. A process
symbol could be used to represent sorting or searching in a
separate flowchart. This is represented by a process with an
additional bar on either side of the symbol
o Input/Output symbols: These show the input of data and output of data
o Decision symbols: These symbols are used to decide whether to take
one of two routes by answering a true/false or yes/no question. They
can be used for selection and iteration
o Flow lines: Flow lines use arrows to show the direction of flow and what
task to perform next. Usually these are top to bottom and left to right
Figure 3: Flowchart symbols: terminator, input/output, process, decision (left
to right)
Figure 4: Flowchart for finding the largest of ten numbers
Pseudocode
•
•
Pseudocode is a programming-like language that does not have syntax. It can
be considered “fake” code.
It uses english words and phrases to represent instructions and is very similar
to programming code but does not and cannot run on any computer
•
•
The purpose of pseudocode is to allow developers to understand how to
create a program regardless of the programming language used to implement
the solution
While pseudocode has no specific syntax, it is important to stick to a
consistent style. This will make it easier and quicker for programmers to read
and create programs from the pseudocode
o Examples of a consistent style can include:
▪ Keywords are written in capital letters e.g. INPUT, OUTPUT, IF,
THEN, ELSE
▪ Variable and subroutine names start with capital letters e.g. Age,
Name, Date, CalculateArea, Sortlist
▪ Indentation can be used for iteration and selection
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.1 Development Life CycleProgram Development Life Cycle - Coding
Program Development Life
Cycle - Coding
Download PDF
Test Yourself
Writing Code
•
•
Developers begin programming modules in a suitable programming
language that works together to provide an overall solution to the problem
As each developer programs, they perform iterative testing. Iterative testing is
where each module is tested and debugged thoroughly to make sure it
interacts correctly with other modules and accepts data without crashing or
causing any errors. Developers may need to retest modules as new modules
are created and changed to make sure they continue to interact correctly and
do not cause errors
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.1 Development Life CycleProgram Development Life Cycle - Testing
Program Development Life
Cycle - Testing
Download PDF
Test Yourself
Testing
•
•
Once the overall program or set of programs is created, they are run many
times using varying sets of test data. This ensures the program or programs
work as intended as outlined in the initial requirements specification and
design and rejects any invalid data that is input
Examples of test data include alphanumeric sequences to test password
validation routines. Such validation routines could be:
o
A password must be between 8-20 characters
▪ Test data would be passwords of less than 8 characters or
greater than 20 characters
o A password must include only alphanumeric characters
▪ Test data would be passwords including non-alphanumeric
symbols such as @, ?, #, !, $ or %, etc
Explaining Algorithms
Download PDF
Test Yourself
Explaining Algorithms
•
•
Algorithms can be written using flowcharts, pseudocode or high-level
programming language code such as Python
The purpose of an algorithm is to achieve some goal. It isn’t always initially
clear what the goal may be. By following the algorithm instructions, the
purpose can become clear
Exam Tip
•
Comments in an algorithm or program usually describe why something has
been done or provide useful information to the reader. Each line of code
should otherwise be self-explanatory
•
The purpose of the algorithm below is to add ten user-entered numbers
together and output the total. The processes are:
o initializing three variables (Count, Number, Total)
o inputting a user number
o adding to two variables (Total, Count)
o repeating nine more times
o outputting the final Total value
Count ← 1
Number ← 0
Total ← 0
REPEAT
INPUT Number
Total ← Total + Number
Count ← Count + 1
UNTIL Count > 10
OUTPUT Total
Worked example
The Pseudocode Algorithm shown has been written by a teacher to enter marks for
the students in her class and then to apply some simple processing.
Count ← 0
REPEAT
INPUT Score[Count]
IF Score[Count] >= 70
THEN
Grade[Count] ← "A"
ELSE
IF Score[Count] >= 60
THEN
Grade[Count] ← "B"
ELSE
IF Score[Count] >= 50
THEN
Grade[Count] ← "C"
ELSE
IF Score[Count] >= 40
THEN
Grade[Count] ← "D"
ELSE
IF Score[Count] >= 30
THEN
Grade[Count] ← "E"
ELSE
Grade[Count] ← "F"
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
Count ← Count + 1
UNTIL Count = 30
Describe what happens in this algorithm.
[3]
Any 3 of:
• Inputted marks are stored in the array Score[] [1]
Marks are then checked against a range of boundaries [1]
A matching grade is assigned to each mark that has been input [1]
• The grade is then stored in the array Grade[] [1]
• At the same index as the inputted mark [1]
• The algorithm finishes after 30 marks have been input [1]
•
•
•
Other
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.3 Standard MethodsStandard Methods
Standard Methods
Download PDF
Linear Search
•
•
The linear search is a standard algorithm used to find elements in an
unordered list. The list is searched sequentially and systematically from the
start to the end one element at a time, comparing each element to the value
being searched for
o If the value is found the algorithm outputs where it was found in the list
o If the value is not found it outputs a message stating it is not in the list
An example of using a linear search would be looking for a specific student
name in a list or searching for a supermarket item in a shopping list
OUTPUT “Enter a value to find”
INPUT Number
Found ← FALSE
Index ←1
REPEAT
IF Number = Mylist[Index]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found =TRUE OR Counter > LENGTH(Mylist)
IF Found = TRUE
THEN
OUTPUT Number, “ found at position “, Counter
ELSE
OUTPUT Number, “ not found”
ENDIF
Bubble Sort
•
•
•
•
•
•
Bubble sort sorts items into order, smallest to largest, by comparing pairs of
elements and swapping them if they are out of order
The first element is compared to the second, the second to the third, the third
to the fourth and so on, until the second to last is compared to the last. Swaps
occur if each comparison is out of order. This overall process is called a pass
Once the end of the list has been reached, the value at the top of the list is
now in order and the sort resets back to the start of the list. The next largest
value is then sorted to the top of the list
More passes are completed until all elements are in the correct order
A final pass checks all elements and if no swaps are made then the sort is
complete
An example of using a bubble sort would be sorting into alphabetical order an
array of names, or sorting an array of student marks from a test
Mylist ← [5, 9, 4, 2, 6, 7, 1, 2, 4, 3]
FirstElement ← 1
LastElement ← LENGTH(Mylist)
REPEAT
Swap ← FALSE
For Index ← FirstElement TO LastElement - 1
IF Mylist[Index] > Mylist[Index + 1]
THEN
Temp ← Mylist[Index]
Mylist[Index] ← Mylist[Index + 1]
Mylist[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
LastElement ← LastElement - 1
UNTIL Swap = FALSE OR LastElement = 1
OUTPUT “Your sorted list is:”, Mylist
Totalling & Counting
Totalling
•
•
Totalling involves keeping a running total of values entered into the algorithm.
An example may be totalling a receipt for purchases made at a shop
The Total below starts at 0 and adds up the user inputted value for each item
in the list. For example, if the user has a receipt for four items: an apple
(£0.50), a drink (£1), a sandwich (£2) and a bar of chocolate (£1). The
algorithm will total the cost for each item one at a time. The output Total will
be 4.50
Total ← 0
FOR Count ← 1 TO ReceiptLength
INPUT ItemValue
Total ← Total + itemValue
NEXT Count
OUTPUT Total
Counting
•
•
Counting works similarly to totalling except the count
is incremented or decremented by a fixed value, usually 1, each time it
iterates. Counting keeps track of the number of times an action has been
performed. Many algorithms use counting, including the linear search and
binary search to track which element is currently being considered
The count is incremented and each pass number is output until fifty outputs
have been produced
Count ← 0
DO
OUTPUT “Pass number”, Count
Count ← Count + 1
UNTIL Count >= 50
•
The count is decremented from fifty until the count reaches zero. An output is
produced for each pass
Count ← 50
DO
OUTPUT “Pass number”, Count
Count ← Count - 1
UNTIL Count <= 0
Maximum, Minimum & Average
•
Finding the largest and smallest values in a list is a frequently used method in
algorithms. Examples could include the maximum and minimum student
grades or scores in a game
Max ← Score[1]
Min ← Score[1]
FOR Count ← 2 TO ScoreSize
IF ScoreSize[Count] > Max
THEN
Max ← ScoreSize[Count]
ENDIF
IF ScoreSize[Count] < Min
THEN
Min ← ScoreSize[Count]
ENDIF
Next Count
•
•
•
•
•
In the above algorithm, the initial max and min are set to the first value in the
list and the for loop starts at the second element instead of the first as the first
value is the benchmark to compare all other items to
If no value is larger than the initial max, then Max doesn’t change. If no value
is smaller than the initial min, then Min doesn’t change
The algorithm loops over each element asking whether the current value is
larger than Max and whether it is smaller than Min. Max and Min adjust their
values throughout the program depending on these conditions
The program will eventually output the smallest and largest value in Min and
Max respectively
Average values (also known as the mean) involve totalling all the list values
and then dividing by the number of values in the list
Total ← 0
FOR Count ← 1 TO ScoreSize
Total ← Total + ScoreSize[Count]
NEXT Count
Average ← Total / ScoreSize
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.4 Validation and TestingValidation
Validation
Download PDF
Test Yourself
Validation
•
Validation and verification are used to ensure input data is correct, reasonable
and accurate
•
Validation generally is about making sure data follows a set of specified rules
created by the programmer. Verification is about double-checking input data
to make sure it's what the user intended to enter
Validation
•
•
Validation is the method of checking input data so that it is of an expected
value and therefore accepted by the system
Programmers create rules using code that automatically checks user input
data to make sure the data is reasonable. If it is not reasonable then the data
is rejected, usually with a message explaining why it was rejected and
allowing the user to reenter the data
The different types of validation rules or checks are as follows:
o Range checks
o Length checks
o Type checks
o Presence checks
o Format checks
o Check digits
•
Each piece of data may require multiple different checks to be fully valid
•
Range check
•
Range checks make sure the input data is a value between a user-defined
minimum and maximum value, for example, a percentage being between 0
and 100 inclusive or a date in April is between 1 and 30 inclusive
OUTPUT “Enter a number between 0 and 100”
REPEAT
INPUT Number
IF Number < 0 OR Number > 100
THEN
OUTPUT “Number is not between 0 and 100, please try again”
ENDIF
UNTIL Number >= 0 AND Number <= 100
Length check
•
•
Length checks check either that the input data is of an exact number of
characters or in a user specified number range of characters
A bank 4-digit PIN number may be an example of an exact number of
characters. If it is not 4 digits in length it should be rejected
OUTPUT “Please enter your 4 digit bank PIN number”
REPEAT
INPUT Pin
IF LENGTH(Pin) <> 4
THEN
OUTPUT “Your pin number must be four characters in length, please try again”
ENDIF
UNTIL LENGTH(Pin) = 4
•
Passwords usually have a specified range, for example, eight to twenty
characters in length. If it does not fall within this range, it should be rejected
OUTPUT “Please enter a password between 8 and 20 characters”
REPEAT
INPUT Password
IF LENGTH(Password) < 8 OR LENGTH(Password) > 20
THEN
OUTPUT “Your password must be between 8 and 20 characters in length, please try again”
ENDIF
UNTIL LENGTH(Password) >= 8 AND LENGTH(Password) <= 20
Type checks
•
Type checks make sure the input data is of the correct data type. For
example, someone's age should be an integer (a whole number) whereas
their name should be a string (a bunch of characters)
OUTPUT “Enter an integer number”
REPEAT
INPUT Number
IF Number <> DIV(Number, 1)
THEN
OUTPUT “Not a whole number, please try again”
ENDIF
UNTIL Number = DIV(Number , 1)
Presence check
•
•
Presence checks make sure that input data has been entered and that the
input box has not been left empty
A login system requires presence checks as both a username and password
are required for authentication
OUTPUT “Enter your username”
REPEAT
INPUT Username
IF Username = “”
THEN
OUTPUT “No username entered, please try again”
ENDIF
UNTIL Username <> “”
Format check
•
•
•
•
•
•
Format checks make sure that input data is of a predefined pattern
Identification codes e.g. AP1234 and dates are examples of patterns
Format checks are done using pattern matching and string handling
The algorithm below checks a six digit identification number against the
format “XX9999” where X is an uppercase alphabetical letter and 9999 is a
four digit number
The first two characters are checked against a list of approved characters.
The first character is compared one at a time to each valid character in the
ValidChars array. If it finds a match it stops looping and sets ValidChar to
True. The second character is then compared one at a time to each valid
character in the ValidChars array. If it finds a match then it also stops looping
and sets ValidChar to True
Casting is used on the digits to turn the digit characters into numbers. Once
the digits are considered a proper integer they can be checked to see if they
are in the appropriate range of 0-9999
•
If any of these checks fail then an appropriate message is output
INPUT IDNumber
IF LENGTH(IDNumber) <> 6
THEN
OUTPUT “ID number must be 6 characters long”
END IF
ValidChars ← “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
FirstChar ← SUBSTRING(IDNumber, 1, 1)
ValidChar ← False
Index ← 1
WHILE Index <= LENGTH(ValidChars) AND ValidChar = False DO
IF FirstChar = ValidChars[Index]
THEN
ValidChar ← True
ENDIF
Index ← Index + 1
ENDWHILE
IF ValidChar = False
THEN
OUTPUT “First character is not a valid uppercase alphabetical character”
ENDIF
SecondChar ← SUBSTRING(IDNumber, 2, 2)
ValidChar ← False
Index ← 1
WHILE Index <= LENGTH(ValidChars) AND ValidChar = False DO
IF SecondChar = ValidChars[Index]
THEN
ValidChar ← True
ENDIF
Index ← Index + 1
ENDWHILE
IF ValidChar = False
THEN
OUTPUT “Second character is not a valid uppercase alphabetical character”
ENDIF
Digits ← INT(SUBSTRING(IDNumber, 3, 6))
IF Digits < 0000 OR Digits > 9999
THEN
OUTPUT “Digits invalid. Enter four valid digits in the range 0000-9999”
ENDIF
Check digits
•
•
Check digits are numerical values that are the final digit of a larger code such
as a barcode or an International Standard Book Number (ISBN). They are
calculated by applying an algorithm to the code and are then attached to the
overall code
Check digits help to identify errors in data entry such as mistyping,
miscanning or misspeaking
o Such errors include missing or additional digits, swapped digits,
mispronunciation of digits or simply an incorrect digit
Barcode ← “9780201379624”
Total ← 0
FOR Index = 1 to LENGTH(Barcode) - 1
IF Index MOD 2 = 0
THEN
Total ← Total + CAST_TO_INT(Barcode[Index])*3
ELSE
Total ← Total + CAST_TO_INT(Barcode[Index])*1
ENDIF
NEXT Index
CheckDigit ← 10 - Total MOD 10
IF CheckDigit = Barcode[LENGTH(Barcode)]
THEN
OUTPUT “Valid check digit”
ELSE
OUTPUT “Invalid check digit”
ENDIF
Verification
•
•
•
Verification is the act of checking data is accurate when entered into a system
Mistakes such as creating a new account and entering a password incorrectly
mean being locked out of the account immediately
Verification methods include: double entry checking and visual checks
Double entry checking
•
Double entry checking involves entering the data twice in separate input
boxes and then comparing the data to ensure they both match. If they do not,
an error message is shown
REPEAT
OUTPUT “Enter your password”
INPUT Password
OUTPUT “Please confirm your password”
INPUT ConfirmPassword
IF Password <> ConfirmPassword
THEN
OUTPUT “Passwords do not match, please try again”
ENDIF
UNTIL Password = ConfirmPassword
Visual check
•
Visual checks involve the user visually checking the data on the screen. A
popup or message then asks if the data is correct before proceeding. If it isn’t
the user then enters the data again
REPEAT
OUTPUT “Enter your name”
INPUT Name
OUTPUT “Your name is: “, Name, “. Is this correct? (y/n)”
INPUT Answer
UNTIL Answer = “y”
Worked example
Describe the purpose of validation and verification checks during data entry. Include
an example for each.
[4]
Validation check
[1] for description:
•
•
To test if the data entered is possible / reasonable / sensible
A range check tests that data entered fits within specified values
[1] for example:
•
Range / length / type / presence / format
Verification check
[1] for description:
•
•
To test if the data input is the same as the data that was intended to be input
A double entry check expects each item of data to be entered twice and
compares both entries to check they are the same
[1] for example:
•
Visual / double entry
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.4 Validation and TestingTest Data
Test Data
Download PDF
Test Yourself
Test Data
Suggesting and applying suitable test data
•
•
•
Before a system is used, each sub-system must be tested to ensure it works
correctly and interacts correctly with other sub-systems
Programs are tested by running them on a computing device while
pseudocode and flowcharts must be dry run manually. Both require suitably
chosen and different sets of test data. The outputs are then compared to the
expected output to check if the algorithm works as intended
Test data comes in several generic types:
o Normal
•
o
▪
o
o
Normal test data is data that a system would be expected to
handle on a day-to-day basis, be accepted by the algorithm and
produce expected results
▪ Examples could include entering people's names and
addresses, phone numbers, student grades as a percentage,
etc
▪ Student percentage grades could involve test data such as: 34,
41, 56, 78, 12, 92
Abnormal
▪ Also known as erroneous data, abnormal data is data that is
expected to fail and should be rejected by the system. This is
used to prove that the system works correctly by rejecting
incorrect data
▪ Examples of abnormal data would be entering numbers instead
of someone's name, or entering text instead of numbers
▪ Student percentage grades abnormal data could involve test
data such as: abc, 7&n, Harry, £300, <!%, etc
Extreme
▪ Extreme test data is the maximum and minimum values of
normal data that are accepted by the system
▪ Examples could include percentages (0 and 100), days in April
(1 and 30) or the number of characters in a passwords range
▪
o
For an 8-20 character range password, a password of 8
characters and another of 20 characters would be tested
Boundary
▪ Boundary test data is similar to extreme data except that the
values on either side of the maximum and minimum values are
tested
▪ The largest and smallest acceptable value is tested as well as
the largest and smallest unacceptable value
▪ For example, a percentage boundary test would be 0 and -1 (for
0) and 100 and 101 (for 100). For the days in April, 1 and 0 (for
day 1) and 30 and 31 (for day 30) would be tested. For an 8-20
length password, an 8 and 7 character password would be
tested as well as a 20 and 21 character password
Worked example
A programmer has written an algorithm to check that prices are less than $10.00
These values are used as test data:
10.00
9.99
ten
State why each value was chosen as test data.
[3]
10.00 is boundary or abnormal data and should be rejected as it is out of range [1]
9.99 is boundary, extreme and normal data and should be accepted as it is within the
normal range [1]
Ten is abnormal data and should be rejected as it is the wrong value type [1]
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.4 Validation and TestingTrace Tables
Trace Tables
Download PDF
Test Yourself
Trace Tables
•
•
•
Trace tables are used to follow algorithms and make sure they perform the
required task correctly. Test data is usually used in conjunction with a trace
table to ensure the correctness of the algorithm. Manually tracing an algorithm
with test data in this way is known as a dry run
Trace tables can be used with flowcharts or pseudocode or even real code if
necessary
Trace tables can also be used to discover the purpose of an algorithm by
showing output data and intermediary steps
•
•
•
Trace tables record the state of the algorithm at each step or iteration. The
state includes all variables that impact the algorithms output as well as the
output itself
A trace table is composed of columns where each variable and the output is a
column
Whenever a value changes or an output is produced the relevant column and
row is updated to reflect the change
Trace Table Walkthrough
•
•
•
•
•
Below is a flowchart to determine the highest number of ten user entered
numbers
The algorithm prompts the user to enter the first number which automatically
becomes the highest number entered
The user is then prompted to enter nine more numbers. If a new number is
higher than an older number then it is replaced
Once all ten numbers are entered, the algorithm outputs which number was
the highest
Example test data to be used is: 4, 3, 7, 1, 8, 3, 6, 9, 12, 10
Figure 1: A flowchart to determine the highest of ten user entered numbers
Trace table for Figure 1: Highest number
Count
Highest
Number
Output
1
Enter ten numbers
4
2
Enter your first number
3
3
7
4
Enter your next number
7
1
5
8
8
6
3
7
6
8
9
9
9
12
12
10
10
12 is your highest number
Exam Tip
•
When asked to identify the purpose of an algorithm in the exam, the variables
listed will often be a single letter rather than a meaningful identifier
Worked example
The flowchart represents an algorithm. The algorithm will terminate if –1 is entered.
Complete the trace table for the input data: 50, 75, 99, 28, 82, 150, –1, 672, 80
[4]
Value
Diff1
Diff2
Output
[1] for each correct column
Value
50
75
99
28
82
150
-1
Diff1
50
25
1
Diff2
0
25
49
18
32
Output
Accept: Extreme
Accept: Normal
Accept: Normal
Reject: Abnormal
Accept: Normal
Reject: Abnormal
CIE IGCSE Computer Science
Revision Notes
IGCSEComputer ScienceCIERevision Notes7. Algorithm design and problemsolving7.5 Identifying ErrorsIdentifying Errors
Identifying Errors
Download PDF
Test Yourself
Identifying Errors
Errors can be identified in algorithms using trace tables as well as scanning and
debugging code manually
Two types of errors are as follows:
•
Syntax error
▪
▪
▪
•
Syntax refers to the grammar of language, that is, are the words
in the right order and is everything spelled correctly
Unlike programs written on computers, syntax errors in
flowcharts and pseudocode do not cause the algorithm to fail
however they do make the code more difficult to read and
understand
Transcribing flowcharts and pseudocode to real programming
code may cause problems if it is difficult to read and understand
said flowcharts and pseudocode
Logical error
o
Logical errors occur when the program finishes but produces the wrong
output during or after
o
o
•
•
Flowcharts and pseudocode, unlike real programs, do not crash when
dry run, however may still produce logic errors by producing incorrect
output
Logical errors are the most difficult error to fix as developers must
reconsider their logic and processes. If they do not understand the
problem they cannot produce an accurate solution
Below is an algorithm that asks the user to enter their age to determine what
rated movie they could watch at the cinema
There are syntax and logical errors with this pseudocode
OUTPUT Age
INPUT “Enter an age”
IF Age > 18
THEN
OUTPUT “You can watch an 18 movie”
ELSE
IF Age > 15
THEN
OUTPUT “You can watch a 15 movie
ELSE
IF Age > 12
THEN
OUTPUT “You can watch a 12 movie”
ELSE
IF Age < 9
THEN
OUTPUT “You can watch a PG movie”
ELSE
OUTPUT “You can watch a U movie”
END IF
ENDIF
ENDIF
ENDIF
•
•
•
•
•
Syntax and logic: OUTPUT Age and INPUT “Enter an age” are both syntax
and logical errors
o Syntax: Age does not yet have a value so OUTPUT cannot display it
while INPUT does not have a variable to store the data as strings
cannot store data
o Logical: Both are the wrong way around. OUTPUT Age should be
OUTPUT “Enter an age” and INPUT “Enter an age” should be INPUT
Age
Syntax: THEN is missing from the first IF statement
Syntax: A quote is missing from the OUTPUT “You can watch a 15 movie”
Logic: Age < 9 should be Age > 9 so that it follows the other IF statement
logic
Syntax: ENDIF is missing from the first IF statement
Worked example
1. An algorithm has been written in pseudocode to input some numbers. It only
outputs any numbers that are greater than or equal to 100. The number 999 is
not output and stops the algorithm.
INPUT Number
WHILE Numbers <> 999 DO
IF Number > 100
THEN
OUTPUT Number
ENDIF
ENDWHILE
OUTPUT Number
(a) Identify the four errors in the pseudocode and suggest corrections.
[4]
Numbers should be Number [1]
IF Number > 100 should be IF Number >= 100 [1]
INPUT Number is missing from inside the loop, insert INPUT Number after the
ENDIF statement [1]
The final OUTPUT Number is not needed, remove it [1]
(b) Write a pseudocode statement to change the corrected algorithm to output all
numbers between 100 and 200 inclusive. You do not need to rewrite the whole
algorithm
[2]
[1] for both ends of the range and correct inequality symbols
[1] for the AND
IF Number >= 100 AND Number <= 200
THEN
OUTPUT Number
ENDIF
Download