3 - Lectures Note Week4
3 - Lectures Note Week4
Tool
Topic 3:
Concept & Properties of Algorithm
Lecture 3 Week4:
Lecturer
Dr. Ufuoma C. Ogude
2
Concept & Properties of Algorithms
Concept & properties of Algorithm
Design of Algorithms
Pseudocode
Flowchart
3
Problem Solving
1. Understand the Problem
2. Formulate a Model
3. Develop an Algorithm
5
Introduction
In our day-to-day life we perform activities by following
called an algorithm.
7
Algorithm
1st Definition:
Better Definition:
Output Information
Print: Print ‘Program Complete’
Write: Write record to file
Output: Output totalAmount
Display: Display ‘Program Complete’
Perform Arithmetic
Add: Add num1 to num2
Subtract: Subtract num1 from num2
Multiply: Multiply num1 by num2
Divide: Divide num1 by num2
12
NOTE
Assign Values
Initialise: Initialise totalPrice to zero
Set: Set totalPrice to zero
Store: Store zero in totalPrice
Compare Values
IF…THEN…ELSE
IF num1 > num 2 THEN
ADD num1 to total
ELSE
ADD num2 to total
ENDIF
Repeat Actions
DOWHILE
DOWHILE Num1 > Num2
ADD num1 to total
Multiply total by 3
Subtract Num2 by 3 13
ENDDO
Requirement
Three Requirements
1. Sequence is:
a. Precise
b. Consists of a limited number of steps
2. Each step is:
a. Unambiguous (Explicit, Definite, Clear-cut, Clear)
b. Executable
3. The sequence of steps terminates in the form of
a solution
14
Example1 of Algorithm
Compute the average marks of students obtained in a TEST of
"Computer Programming"
Human Language Specification: (In English)
Read the no of students
Read the marks of each students
Sum the marks of all students as total marks.
Divide the total marks by number of students.
Report the result of division as average marks of students.
Pseudocode Specification:
Input N
Input N marks: m1, m2, m3,..... mN
T = (m1+ m2 + m3 +... ... ....+ mN) / N
A=T/N
Output N. 15
Example2 of Algorithm
Design an algorithm to add two numbers and display
the result
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP 16
Example3 of Algorithm
Write an algorithm to find the square of a number.
Before developing the algorithm, let us first identify the
input, process and output:
Input: Number whose square is required
Process: Multiply the number by itself to get its square
Output: Square of the number
Algorithm to find square of a number.
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square 17
There are two common methods of representing an
algorithm. It may be expressed in either:
flowchart (Graphical representation) and
Pseudocode (Human language (e.g. English)).
Either of the methods can be used to represent an
algorithm while keeping in mind the following:
it showcases the logic of the problem solution,
excluding any implementation details
it clearly reveals the flow of control during execution
of the program
18
Flowchart
A flowchart is the graphical representation of the algorithm
for any problem.
It is useful for developing and representing algorithms.
Flow charts are drawn using certain special-purpose
symbols connected by arrows called flow lines.
The symbols indicate the actions to be performed, and
flow lines indicate the order in which actions are to be
performed.
Flow lines have arrows (direction of flow).
19
Reasons for Flowchart
Major reasons:
is easier to read
more closely follows a standard
easier to understand
20
Flowchart Symbols
Here is a chart for some of the common symbols used in
drawing flowcharts.
21
Flowchart Symbols
22
Flowchart Symbols
23
Example4 of Algorithm
Write an algorithm and draw flowchart for finding
the sum of any two numbers.
Algorithm
Step1: Start
Step2: Display “Enter two numbers”.
Step3: Read A and B
Step4: C= A+B
Step5: Display “C as sum of two numbers”
Step6: Stop 24
Flowchart
25
Average of two numbers
26
Guidelines for Developing Flowcharts
These are some points to keep in mind while developing a
flowchart:
Flowchart should have only one start and one stop
symbol
On-page connectors are referenced using numbers
Off-page connectors are referenced using alphabets
General flow of processes is top to bottom or left to
right. Only one flow line should enter a decision symbol,
but two or three flow lines can leave the decision symbol.
Arrows should not cross each other.
27
Pseudocode
The word “pseudo” means “not real,” so “pseudocode” means “not
real code”.
A Pseudocode is another way of representing an algorithm.
It is a non-formal language that helps programmers to develop
algorithm. It is a cross between human language and a
programming language.
It is a detailed description of instructions that a computer must
follow in a particular order.
It is intended for human reading and cannot be executed directly by
the computer.
Not an actual programming language
Not actually executed on computers
28
of the algorithm.
32
Solution to Example3
Pseudocode for the sum of two
numbers will be:
input num1
input num2
COMPUTE Result = num1 +
num2
PRINT Result
33
Solution to Example3
Flowchart to display sum of two numbers
34
Benefits of Pseudocode
It helps in representing the basic functionality of the
intended program.
(for non-programmers). 35
It shows the flow of events as represented in the flow chart.
Components of an algorithm:
Values and Variables
Instruction
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions) 36
Flowchart
Pseudocode
37
Next Lecture: Week5
Fundamentals of programming (1):
variable/variable naming convention/data
types in Python
38
Fundamentals of Programming
(1)
variable/variable naming convention/data types in Python
39