7 & 8 - Algorithm Design and Problem Solving
7 & 8 - Algorithm Design and Problem Solving
Activity: Break down the ‘Check time’ sub-system from the smart phone alarm app into
further sub-systems. Computer Science 2210
Compiled By: Bilal Khan
Activity:
Examples of algorithms:
• routine for cleaning your teeth
• a cooking recipe
• series of memorised steps when making a drink of tea
• a set of instruction in a kit telling you how to put the kit
together
Computer Science 2210
Compiled By: Bilal Khan
Algorithm design
There are two common methods to express
algorithm designs, they are:
• flowcharts
• pseudocode
• Connectors
Whenever a flowchart becomes complex enough that
the number and direction of flow lines is confusing or
spread over more than one page, it is useful to utilise
the connector symbols.
Computer Science 2210
Compiled By: Bilal Khan
Flowchart
symbols
Control Structures
In a program the control structure controls how
the program executes. There are three basic
control structures.
• Sequence
• Selection
• Iteration/Repetition
Statement a
Statement b
Stop
Computer Science 2210
Compiled By: Bilal Khan
Example:
Draw a flowchart for an algorithm to input two numbers, add
them and output the total .
Start
Input n1, n2
sum = n1 + n2
output sum
Stop
Computer Science 2210
Compiled By: Bilal Khan
Activities:
1. Draw a flowchart to make a cup of tea.
2. Draw a flowchart for an algorithm to input
two numbers and output the total and
average.
3. Draw a flowchart to read the price and
quantity through the keyboard and write
the total value on the screen
• IF-THEN
• IF-THEN-ELSE
Start Start
Test
True Test True
condition condition
False True
True
False False statement
statement
statement
2. Draw a flowchart that read marks from the keyboard and print “Passed” on
the screen, if marks are more than 60. Otherwise print “Failed”.
3. Draw a flowchart to find, If the number entered by the user is odd or even
number.
Algorithm:
• Step 1: Read amount,
• Step 2: Read years,
• Step 3: Read rate,
• Step 4: Calculate the interest with the formula
Interest=Amount*Years*Rate/100
• Step 5: Print interest,
• Counting:
Keeping a count of the number of times an action is
performed. Counting is used with repetition with the
counter increased by 1 every time the loop is repeated.
For example, counting the number of students that were
awarded a pass mark.
Computer Science 2210
Compiled By: Bilal Khan
Activities:
flowchart showing
how the checking
for the alarm time
sub-system works.
• Constant
A named data store that contains a value that does not
change during the execution of a program.
Note:
In order to make programs understandable to others, variables and
constants should be given meaningful names.
Both constants and variables can only hold one piece of data at a time.
When a new value is assigned to variable (using an assignment
statement) it automatically replaces whatever was stored
previously.
Computer Science 2210
Compiled By: Bilal Khan
Pseudocode
You can write algorithms in plain English before
you decide which programming language you
want to use. Writing algorithms this way is
called pseudocode.
• range checks
• length checks
• type checks
• presence checks
• format checks
• check digits
Computer Science 2210
Compiled By: Bilal Khan
Range check:
Checks that a value falls within the specified
range
OR
Value of a number is between an upper value
and a lower value.
Examples :
Value1 MOD(10,3) returns the remainder of 10 divided by 3
Value2 DIV(10,3) returns the quotient of 10 divided by 3
Value3 ROUND(6.97354, 2) returns the value rounded to 2 decimal places
Value4 RANDOM() returns a random number between 0 and 1 inclusive
Computer Science 2210
Compiled By: Bilal Khan
Format check:
A format check checks that the characters entered
conform to a pre-defined pattern,
OR
Checks the data is in the right format.