Topic 7 - Algorithm Design
Topic 7 - Algorithm Design
Life-cycle
Abstraction and Decomposition
When designing a computer system, abstraction is the process of getting out all the important
details. For example in the previous example about the toy company we need to know about:
• the people in the business (designers, sales people) and what they actually do.
• we may also need to know how much it costs to make a toy
• how much a toy sells for.
But do we need to know about where the staff room is located? (no we don't)
Do we need to know where employees park their cars? (no we don't)
Decomposition
Is the process of breaking down every important process in an organisation in order to create
a computer system that will consider everything the organisation does
Structure Diagram
Structure diagrams are a graphical way of
representing a problem, showing the
different levels of detail.
They are a great way to illustrate all the
systems and subsystems.
This is a hierarchical diagram meaning top
down, looking from above!
This is a way of decomposing the
problems
Algorithm Design
An algorithm is a set of instructions/steps/rules that are followed to solve a
problem.
We can start designing algorithms using flowcharts or pseudocode.
Flowcharts
Flowcharts are a graphical way of representing an algorithm design using specific
symbols
Pseudocode algorithms
Pseudocode is lines of instructions written in a language close to English but with
common programming terms used where possible (if, while). People with limited
programming knowledge should be able to follow pseudocode as it normally
doesn’t resemble any one programming language.
Top down design
is the name given to breaking a problem down into increasingly smaller and smaller
manageable parts (also known as decomposition).
Development Life Cycle
To create a computer program we must go through different stages before we can come up with
a complete solution we call this the program life cycle
Analysis
This is the fact finding stage where everything is analysed. All the process that are performed by the
organisation. How they are performed and how everything is linked together.
Design
Elements of the new system are designed using structure charts, flow diagrams,
Coding
The actual stage of writing code. This could be a team of developers or a single developer
Testing
Once the program is complete various types of testing are performed to ensure the program functions as it
should.
TASK:
For each section of the Program Life Cycle make a list of tasks that must be carried out in each stage. What is
it? Who does the task?
Topic 7 - Algorithm Design
Sub-Systems
Sub-systems / Top-down Design
When thinking about a computer system many organisations have various departments
and process. For example a school can contain a Primary and Secondary section. In the
secondary section there are various faculties such as English and Science. A school also has
students and teachers. Teachers must get paid there is a process of paying a salary
Students must be registered which is another process. Exam results must be stored
somewhere that is another process.
In programming these smaller chucks/process sometimes become:
● Subroutines
● Modules
● Procedures
Topic 7 - Algorithm Design
Verification,Validation
WHAT WE ALL EXPECT
▪ Programs should never BREAK
▪ Never produce ERRORS
▪ Do what they need to do
▪ Save our data correctly
Can this be achieved really….really?
Verification
Verification is the process of checking that data has been copied accurately
from one medium to another such as paper to computer.There are generally
two methods:
1. Double entry
Data is entered twice and the computer checks that they match up
e.g type in email twice to confirm its correct
1. Visual Check
The user manually reads and compares the newly inputted data against the original
source to ensure they match
E.g double check we enter a bank account correctly
Verifications checks if data is accurate not if it is correct
VALIDATION
Validation is the process of checking user inputted data and making sure it is sensible,
reasonable, appropriate. It does not check the accuracy of data!
Range Check Checks the data falls between an acceptable upper and lower value, within a set range. e.g
age can only be between 0-120
Length Check Checks the number of characters meets expectations, e.g. an 8 character password
Presences Check Checks that the user has at least inputted something, stopping them from accidentally
entering nothing
Type Check Checks that the data entered is of an expected type, e.g. text or a number
Format Check Checks that the format of the data is correct . e.g dates dd//mm/yyyy or mm/dd/yy
Check digit An extra digit added to a number which is calculated from the other digits, this ensures the
rest of the number has been entered properly
Can you provide examples of where we could use these checks?
Range Check
Length Check
Type Check
Presence Check
Topic 7 - Algorithm Design
Testing
CODE MAINTAINABILITY
What are comments?
The read text with the hashtag at the start is a comment
# check to see if the person is over 15
If personAge >= 15
Print (“How many tickets do you want?”)
Comments in programs serve a number of purposes
1. To inform them reader of a bug or issues
2. To explain the code and its function in more detail
3. To stop a line of section of code from executing
ERRORS
Syntax Error: Grammatical mistakes
priint (“Hello”)
Logic Error: Fault is in the logic
If weather == “Hot”:
Print (“Its Cold”)
TESTING
● Testing is an important part of software development. It ensures that a
program operates correctly.
● It is important to test algorithms to check how they perform under a
range of conditions.
● This includes testing any validation you have created to ensure it
performs as expected
● There are number of methods of testing including final testing, user
testing, beta testing, iterative testing.
● Testing can be performed by the programmer or by specialist testers.
Testing and Test Data
There are 4 types of test data used for testing a program:
1. Normal: tests with normal/expected data. e.g. a secondary school age, 11-16, normal
would be any number between 11 to 16.
2. Abnormal (erroneous): data that cannot be accepted, incorrect inputs. E.g. you type
in a string instead of an integer. E.g. two instead of 2.
3. Boundary: tests under extreme conditions. The largest/smallest value that is acceptable
and also checks what largest/smallest values must be rejected. e.g. firstname max length
of 25. Range driving license age min 17 and max 65, the boundary would be low 17, high
65.
4. Extreme: extreme data is test data at the upper or lower limits of expectations that
should be accepted by the system.
Test Plan Example
A test plan is created for every input. Testing is carried out once the program is complete.
In this test plan below we are testing the input for age. A person's age can only be between 1 and 110 years old.
Test Num Type Field / Input Purpose Test Data Expected Output Actual
Results Results
1 Normal Age To test normal age 5,22,55 Accepted -
entry
2 Abnormal Age Test to make sure 20.2, twenty Rejected Message
abnormal values for name can
age are rejected only be 15
characters
3 Boundary Age Test the boundaries 1st pair:0,1 Accept 1 and -
2nd pair: 110,111 110, Reject 0
and 111
4 Extreme Age Test max and 1, 110 Accepted Message
minimum ages should be
numeric
Topic 7 - Algorithm Design
Searches, Sorting
Linear Search
Start at the beginning and go through each item till you find the
item.
2 3 11 1 9 5 8 10 6
● A very simple search compared with others
● Not as efficient, can take a very long time or a short time depending where the item
is.
● Can be used on any type of list (unordered list)
● A Binary search is much more efficient than a linear search
Bubble Sort
First pass
33 12 55 47 8 23
12 33 55 47 8 23
12 33 55 47 8 23
12 33 47 55 8 23 • Only focuses on two items rather than
12 33 47 8 55 23 whole list
12 33 47 8 23 55
12 33 47 8 23 55 • Simple algorithm especially if already
Second pass in order
12 33 47 8 23 55
12 33 47 8 23 55 • Doesn’t use much memory but can
12 33 47 8 23 55 take long time with large lists
12 33 8 47 23 55
12 33 8 23 47 55
Third pass
12 33 8 23 47 55
If there are 5 items in a list then it will do 4
12 33 8 23 47 55 passess
12 8 33 23 47 55
12 8 23 33 47 55
12 8 23 33 47 55 If there are 6 items in a list then it do 5
Fourth pass
12 8 23 33 47 55 passess
8 12 23 33 47 55
8 12 23 33 47 55
8 12 23 33 47 55 And so on
Fifth pass
8 12 23 33 47 55
8 12 23 33 47 55
Topic 7 - Algorithm Design
Trace Table
Trace Table
● A trace table is a technique used to test an algorithm and predict
step by step how the computer will run the algorithm.
● It can be used to understand or predict what an algorithm is
doing and to identify potential logic errors
● Trace tables are used when performing a dry-run of an
algorithm.
Example 1 Line
num
number i Output
1 3
2 3
number ← 3 3 1
PRINT number 4 8
FOR i from 1 to 3
5 8
number=number+5
3 2
PRINT number
4 13
5 13
PRINT “?”
3 3
When do we know how to move to the next line of the table?
How do we know what the column headings are?
Example 2
The algorithm below contains 1 variable (num), 1 condition (num < 500) and 1 output
(OUTPUT num). The trace table shows how this algorithm would run if the user
entered an input of 4.
Num Num < 500 Output
num ← USERINPUT
4 TRUE
WHILE num < 500 THEN
8
num ← num * 2
END WHILE 16
OUTPUT num 32
64
128
256
512 FALSE
512
Example 3
Although the algorithm above only contains 1 variable, trace tables can keep track of multiple
variables by using additional columns. The algorithm below is an extension of the algorithm
above, and this time the program will also keep track of how many times the while loop has
repeated. In this example the user enters an input of 16.
Num count Num < Output
num ← USERINPUT 500
count ← 0 16 0 true
WHILE num < 500 THEN 32 1
num ← num * 2 64 2
count ← count + 1 128 3
END WHILE 256 4
512 5 False
OUTPUT num 512
OUTPUT count 5