3 - Lectures Note Week4
3 - Lectures Note Week4
Solving
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
Solving
1. Understand the Problem
2. Formulate a Model
3. Develop an Algorithm
5
n
In our day-to-day life we perform activities by
called an algorithm.
problem
Better Definition:
Processing or computation to be
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
Assign Values
NOTE
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
nt
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
14
of a solution
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 17
tion
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
27
decision symbol.
e
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
No specific standard for writing a pseudocode exists.
e
Why do you need it?
Helps to “think out” a program before writing
it
May be converted easily into a corresponding
programming language code.
29
Similar to a programming language, but not as rigid
The method of expression most suitable for a given situation is used:
Pseudocode
Construct
Following are some of the frequently used keywords
while writing pseudocode:
INPUT
COMPUTE
PRINT
INCREMENT
DECREMENT
IF/ELSE
WHILE
TRUE/FALSE
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.
chart.
times.
Components of an algorithm:
Values and Variables
Instruction
Sequence (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
Python
Python is a Scripting programming language known for both
its simplicity and wide breadth of applications. For this
reason it is considered one of the best languages for
beginners.
Python is a powerful and flexible programming language.
It uses concise and easy-to-learn syntax which enables
programmers to write more codes and develop more
complex programs in a much shorter time.
Used for everything from Web Development to Scientific
Computing
Python is referred to as a “general purpose” language by
40
41