0% found this document useful (0 votes)
7 views

7 & 8 - Algorithm Design and Problem Solving

The document outlines the program development life cycle, which consists of five stages: analysis, design, coding, testing, and maintenance. It emphasizes the importance of abstraction and decomposition in problem-solving, along with the use of algorithms, flowcharts, and pseudocode for designing solutions. Additionally, it discusses control structures, variables, constants, and the significance of syntax in programming languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

7 & 8 - Algorithm Design and Problem Solving

The document outlines the program development life cycle, which consists of five stages: analysis, design, coding, testing, and maintenance. It emphasizes the importance of abstraction and decomposition in problem-solving, along with the use of algorithms, flowcharts, and pseudocode for designing solutions. Additionally, it discusses control structures, variables, constants, and the significance of syntax in programming languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Unit 7 & 8:

Algorithm Design and Problem


Solving & Programming Concepts

Computer Science 2210


Compiled By: Bilal Khan
The program development life cycle
The program development life cycle is divided
into five stages:
• Analysis
• Design,
• Coding
• Testing
• Maintenance

Computer Science 2210


Compiled By: Bilal Khan
Analysis
Before any problem can be solved, it needs to be
clearly defined and set out so anyone working
on the solution understands what is needed.
This is called the ‘requirements specification’
for the program.
The analysis stage uses abstraction and
decomposition tools to identify exactly what
is required from the program.

Computer Science 2210


Compiled By: Bilal Khan
Abstraction:
Abstraction keeps the key elements required for the
solution to the problem and discards any
unnecessary details and information that is not
required.
For example, a map only shows what is required for
travelling from one place to another. Different
methods of transport will require different types of
map.
Decomposition:
Decomposition breaks down a complex problem
into smaller parts, which can then be subdivided
into even smaller parts, that can be solved easily.
Any daily task can be divided into its constituent
parts.

For example, getting dressed:


• Select items to wear
• Remove any clothes being worn
• Put selected items on in order.
Computer Science 2210
Compiled By: Bilal Khan
Decomposing a problem
Any problem that uses a computer system for its solution needs to
be decomposed into its component parts. The component parts
of any computer system are:
• inputs – the data used by the system that
needs to be entered while the system is
active
• processes – the tasks that need to be Input Process Output
performed using the input data and any
other previously stored data
• outputs – information that needs to be Storage
displayed or printed for the users of the
system
• storage – data that needs to be stored in
files on an appropriate medium for use in
the future.
Computer Science 2210
Compiled By: Bilal Khan
Example: An alarm app
For example, the alarm app can be decomposed into:

• inputs – time to set the alarm, remove a previously set alarm


time, switch an alarm off, press snooze button
• processes – continuously check if the current time matches
an alarm time that has been set, storage and removal of alarm
times, management of snooze
• outputs – continuous sound/tune (at alarm time or after
snooze time expired)

• storage – time(s) for alarms set.

Computer Science 2210


Compiled By: Bilal Khan
Design
The program specification from the analysis stage is used
to show to how the program should be developed.
When the design stage is complete, the programmer
should know what is to be done, i.e. all the tasks that
need to be completed, how each task is to be
performed and how the tasks work together.

This can be formally documented using


• structure charts
• flowcharts
• pseudocode
Computer Science 2210
Compiled By: Bilal Khan
Structure diagrams:
Structure diagrams can be used to show top-down
design in a diagrammatic form.
Structure diagrams are
hierarchical, showing
how a computer system
solution can be divided
into sub-systems with
each level giving a more
detailed breakdown.

If necessary, each sub-system can be further divided.


Computer Science 2210
Compiled By: Bilal Khan
Example 1 : Alarm app for a smart phone
Consider the alarm app computer system for a smart phone; this
could be divided into three sub-systems, setting the alarm,
checking for the alarm time, sounding the alarm. These sub-
systems could then be further sub-divided; a structure
diagram makes the process clearer.

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:

Draw a structure diagram of a satellite


navigation system.

Computer Science 2210


Compiled By: Bilal Khan
Satellite Navigation System
Structure diagram showing how a satellite navigation
system can be broken down into smaller sub-
systems.
Algorithms
An algorithm is a set of instructions/steps that are followed
to solve a problem.

We follow algorithms every day, maybe without even


realising we are doing so.

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

Computer Science 2210


Compiled By: Bilal Khan
Syntax
Syntax is the set of rules about how to use a
particular language. In the same way that
English has rules, so do programming
languages.
Program statements have to be used in certain ways for the
computer to recognise them. For example, in Python 'print' is
always typed in lower case and then followed by
an argument - the thing you are asking it to print. If you don't
type print ("Hello World") exactly as it is shown here, you will
get a syntax error.

Computer Science 2210


Compiled By: Bilal Khan
Flowcharts:
A flowchart shows diagrammatically the steps
required to complete a task and the order that
they are to be performed.

Computer Science 2210


Compiled By: Bilal Khan
Flowchart symbols
• Begin/End (Start or Stop)
Terminator flowchart symbols are used at the
beginning and end of each flowchart.

Computer Science 2210


Compiled By: Bilal Khan
Flowchart symbols
• Input and output (I/O)
The same flowchart symbol is used to show the
input of data and output of information.

Computer Science 2210


Compiled By: Bilal Khan
Flowchart symbols
• Process
Process flowchart symbols are used to show actions,
for example, when values are assigned to variables. If
a process has been defined elsewhere then the name
of that process is shown.

Computer Science 2210


Compiled By: Bilal Khan
Flowchart symbols
• Decision
Decision flowchart symbols are used to decide which
action is to be taken next; these can be used for
selection and repetition/iteration. There are always
two outputs from a decision flowchart symbol.

Computer Science 2210


Compiled By: Bilal Khan
Flowchart symbols
• Flow lines
Flowchart flow lines use arrows to show the direction
of flow, which is usually, but not always, top to
bottom and left to right.

• 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

Computer Science 2210


Compiled By: Bilal Khan
Sequence:
• One statement simply follows another in
sequence.
Start

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

Computer Science 2210


Compiled By: Bilal Khan
Mathematical operators

Computer Science 2210


Compiled By: Bilal Khan
Selection:
The selection control structure is used to make
logical decisions. This control structure has
two forms.

• IF-THEN
• IF-THEN-ELSE

Computer Science 2210


Compiled By: Bilal Khan
Selection:
IF-THEN IF-THEN-ELSE
IF (the condition is true), THEN IF (the condition is true) THEN
[execute an instruction] - but if it [execute an instruction], ELSE
is not true, then do nothing. [execute another instruction].

Start Start

Test
True Test True
condition condition

False True
True
False False statement
statement
statement

Stop Computer Science 2210


Compiled By: Bilal Khan
Stop
Comparison operators

Computer Science 2210


Compiled By: Bilal Khan
Activities:
1. Draw a flowchart for an algorithm, to input two different numbers, and
output the larger number.

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.

4. Draw a flowchart to find, If the number entered is a positive number or


negative number.

5. Draw a flowchart to input temperature, if temperature is less than 20, then


print “below freezing point” otherwise print “above freezing point”.

6. Write a program that input marks. If marks are 80 or above, it displays “A


Grade”, if marks are between 60 to 79, it displays “B Grade”, if marks are
between 40 and 59, it displays “C Grade”, otherwise display “Failed”.

Computer Science 2210


Compiled By: Bilal Khan
Draw a flowchart to find, If the number entered by the
user is odd or even number.

Computer Science 2210


Compiled By: Bilal Khan
Calculate the Interest of a Bank Deposit

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,

Computer Science 2210


Compiled By: Bilal Khan
Calculate the Interest of a Bank Deposit

Computer Science 2210


Compiled By: Bilal Khan
Draw a flowchart to
login facebook account.

Computer Science 2210


Compiled By: Bilal Khan
Login to facebook account.
Algorithm:
Start
1. Enter www.facebook.com in your browser. (I/O)
2. Loading facebook home page (PROCESS)
3. Enter Email ID and Password (I/O)
4. Is Email ID and Password valid (DECISION)
if NO then
Login error (PROCESS)
go to step 3
else
Display facebook Account (I/O)
Stop
Computer Science 2210
Compiled By: Bilal Khan
flowchart to login facebook account

Computer Science 2210


Compiled By: Bilal Khan
Example :

Computer Science 2210


Compiled By: Bilal Khan
Iteration/Repetition
The repetition control structure is a looping
mechanism. The only necessary repetition structure
is the DO WHILE structure.
Start
OR
Iteration is the
process of repeating Test False
sections of a condition Stop

program to achieve a True


particular target or
statement
goal.
Computer Science 2210
Compiled By: Bilal Khan
Iteration/Repetition

Computer Science 2210


Compiled By: Bilal Khan
Example:
Draw a flowchart to print “Hello world” 10 times.

Algorithm (in simple English)

Initialize count = 0 (PROCESS)


Print Hello World (I/O)
Increment count by 1 (PROCESS)
Is count < 10 (DECISION)
if YES go to step 2
else Stop

Computer Science 2210


Compiled By: Bilal Khan
Totalling and Counting
• Totalling:
Keeping a total that values are added to. Totalling is
used with repetition with the total updated every time
the loop is repeated.
For example, keeping a running total of the marks awarded to
each student in a class.

• 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:

1. Draw a flowchart to enter your name once


and output it five times.
2. Draw a flowchart to input 5 different
numbers and find the sum of these 5
numbers.

Computer Science 2210


Compiled By: Bilal Khan
Draw a flowchart to input 5 numbers and find
the sum of these numbers

In this question we are asked to find the sum of 5 numbers.


So, we will take two variables - sum and count and set both
of them to zero. The sum variable will store the result while
the count variable will keep track of how many numbers we
have read.

Computer Science 2210


Compiled By: Bilal Khan
Draw a flowchart to input 5 numbers and find
the sum of these numbers

Algorithm (in simple English)


Initialize sum = 0 and count = 0 (PROCESS)
Enter n (I/O)
Find sum + n and assign it to sum and then
increment count by 1 (PROCESS)
Is count < 5 (DECISION)
if YES go to step 2
else
Print sum (I/O)

Computer Science 2210


Compiled By: Bilal Khan
Subroutine/ Predefined Process
If a process has been defined elsewhere
then the name of that process is
shown.
These are used to show complex
processing steps which may be
detailed in a separate flowchart

The same object is also called a


Predefined Process. This means the
flowchart for the predefined process
has to be already drawn, and you
should reference the flowchart for
more information.
Computer Science 2210
Compiled By: Bilal Khan
Example : Checking for the alarm time

flowchart showing
how the checking
for the alarm time
sub-system works.

Computer Science 2210


Compiled By: Bilal Khan
Variables and constants
• Variable
A named data store that contains a value that may
change during the execution of a program.

• 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.

This is a good way of working out exactly what you need to do


before you start coding. It is also a good way of sharing your
ideas with other people. There is no 'exactly right' form for
a pseudocode algorithm - as long as it makes sense to others.

Computer Science 2210


Compiled By: Bilal Khan
Pseudocode in the IGCSE Computer
Science syllabus
The pseudocode is written in the following way to match the
pseudocode given in the IGCSE Computer Science syllabus and
to help you understand the algorithms more easily:
• a non-proportional font is used throughout
• all keywords (words used to describe a specific
action e.g. INPUT) are written in capital letters
• all names given to data items and subroutines
start with a capital letter
• where conditional and loop statements are used,
repeated or selected statements are indented by
two spaces.
Computer Science 2210
Compiled By: Bilal Khan
Important things about a pseudocode
algorithm are:
• it is correct
• it is written in a form that can be understood
by fellow programmers
• what is intended is clear, and not at all
ambiguous
• it can be implemented on the computer
system it is written for

Computer Science 2210


Compiled By: Bilal Khan
Syntax
Syntax is the set of rules about how to use a
particular language. In the same way that
English has rules, so do programming
languages.
Program statements have to be used in certain ways for the
computer to recognise them. For example,
in Python 'print' is always typed in lower case and then
followed by an argument - the thing you are asking it to
print. If you don't type print ("Hello World") exactly as it
is shown here, you will get a syntax error.
Computer Science 2210
Compiled By: Bilal Khan
• Write a pseudocode to input three numbers
through the keyboard and output the total
and the average.

Computer Science 2210


Compiled By: Bilal Khan
Validation and Verification
Validation and verification are two ways to
check that the data entered into a computer is
correct.

Computer Science 2210


Compiled By: Bilal Khan
Validation:
Validation is an automatic computer check to ensure that
the data entered is sensible and reasonable. It does not
check the accuracy of data.

For example, a secondary school student is likely to be


aged between 11 and 16. The computer can be
programmed only to accept numbers between 11 and
16. This is a range check.

However, this does not guarantee that the number typed


in is correct. For example, a student's age might be 14,
but if 11 is entered it will be valid but incorrect.
Computer Science 2210
Compiled By: Bilal Khan
Types of validation checks:

• 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.

Example: Number of hours worked must be less


than 50 and more than 0
Computer Science 2210
Compiled By: Bilal Khan
Range check:
Example: checking that percentage marks are between
0 and 100 inclusive:

Computer Science 2210


Compiled By: Bilal Khan
Length check:
Checks the data isn't too short or too
long
OR
that data contains an exact number of
characters.

Example: A password which needs to be six


letters long.
Computer Science 2210
Compiled By: Bilal Khan
Length check:
Example: password must be exactly eight characters in length so
that passwords with seven or fewer characters or nine or
more characters would be rejected, for instance:

Computer Science 2210


Compiled By: Bilal Khan
Length check:
• Example: or that the data entered is a reasonable number of
characters, for example, a family name could be between two
and thirty characters inclusive so that names with one
character or thirty-one or more characters would be rejected:

Computer Science 2210


Compiled By: Bilal Khan
Presence check:
Checks that data has been entered into a field. In most
databases a key field cannot be left blank.
Example: an email address for an online transaction
must be completed.

Computer Science 2210


Compiled By: Bilal Khan
Type check:
Checks that the data entered is of a given data type
Example: The number of brothers or sisters would be
an integer (whole number).

Computer Science 2210


Compiled By: Bilal Khan
Library routines
Many programming language development systems include library
routines that are ready to incorporate into a program.
These routines are fully tested and ready for use. These standard
library routines perform many types of task:

• MOD – returns remainder of a division


• DIV – returns the quotient (i.e. the whole number part) of a
division
• ROUND – returns a value rounded to a given number of decimal
places
• RANDOM – returns a random number

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.

Example: A National Insurance number is in the


form LL 99 99 99 L where L is any letter and 9 is
any number
OR
Format for date is MM/DD/YY or DD/MM/YYYY
Computer Science 2210
Compiled By: Bilal Khan
Check digit:
(Note: How to find check digit? See Chapter2: Data Transmission: Method of error detection)

A check digit is the final digit included in a code;


it is calculated from all the other digits in the
code.
Example: Check digits are used for barcodes, product codes,
International Standard Book Numbers (ISBN) and Vehicle
Identification Numbers (VIN).

Computer Science 2210


Compiled By: Bilal Khan
Check digit:
Check digits are used to identify errors in
data entry caused by mis-typing or mis-
scanning a barcode.
They can usually detect the following types of error:
• an incorrect digit entered, for example, 5327 entered instead
of 5307
• transposition errors where two numbers have changed order
for example 5037 instead of 5307
• omitted or extra digits, for example, 537 instead of 5307 or
53107 instead of 5307
• phonetic errors, for example, 13, thirteen, instead of 30, thirty.
Computer Science 2210
Compiled By: Bilal Khan
Verification
Verification is checking that data has been
accurately copied from one source to another
OR
To ensure that the data entered exactly matches the
original source

There are two main methods of verification:


• Double entry -
• Screen/Visual Check/Proofreading data

Computer Science 2210


Compiled By: Bilal Khan
Double entry:
Entering the data twice,
sometimes by different
operators.
The computer system
compares both entries
and if they are different
outputs an error
message requesting
that the data is entered
again.
Computer Science 2210
Compiled By: Bilal Khan
Screen/Visual Check/Proofreading data:

A screen/visual check is a manual check completed


by the user who is entering the data.
When the data entry is complete the data is
displayed on the screen and the user is asked to
confirm that it is correct before continuing.
The user either checks the data on the screen
against a paper document that is being used as
an input form or, confirms whether it is correct
from their own knowledge.
Computer Science 2210
Compiled By: Bilal Khan

You might also like