0% found this document useful (0 votes)
23 views23 pages

Session 06

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

Session 06

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DCIT 104

Programming Fundamentals

SESSION 6 – ALGORITHM

Lecturer: Mr. Joseph Ecklu (DCS)


Contact Information: [email protected]

College of Education
School of Continuing and Distance Education
2014/2015 – 2016/2017
Session Overview
• The design stage of the Program development
framework deals with the step by step instructions
required to solve the defined problem based on the
user requirements and program specification.
Algorithms, flowcharts and pseudo-codes are
discussed as tools for design in the imperative
paradigm. The success of programming effort and its
efficiency is largely due to the work done at the design
stage. Also, flowcharts and pseudo-codes are
introduced as design documentation tools of the
programmer.
Mr. Joseph Ecklu (DCS) Slide 2
Session Objectives
• Identify common algorithms
• Create algorithms for solving simple problems based
on program specification
• Be able to understand and apply algorithms to
problem solving as imperative design method
• Use flowcharts as pictorial representation of the
algorithm
• Write pseudo-codes

Mr. Joseph Ecklu (DCS) Slide 3


Reading List
• Foundation of computer Science by B. Forouzon and
F. Mosharraf (pages 213-240)

Mr. Joseph Ecklu (DCS) Slide 4


Algorithm is a step-by-step method of solving a
problem or doing a task. Algorithm accepts input
data and creates an output data.

Input Data
Algorithm
A step-by-step method of solving a
problem or doing a task

Output Data
Mr. Joseph Ecklu (DCS) Slide 5
Example: Finding the largest integer among
five integers
(12, 8, 13, 9, 11) Input

Largest 12 12 8 13 9 11 List
Largest 12 12 8 13 9 11 List
Largest 13 12 8 13 9 11 List
Largest 13 12 8 13 9 11 List

Largest 13 12 8 13 9 11 List

(13) Output Fig 1


Mr. Joseph Ecklu (DCS) Slide 6
Example: Finding the largest integer among
five integers
Fig 1. does not what should be done in step by step. It
can therefore me modified to show the detailed
process by Defining Actions
(12, 8, 13, 9, 11) Input

Step 1 Set largest to first number


Step 2 If the 2nd number is greater than Largest, set Largest to the 2nd number
Step 3 If the 3rd number is greater than Largest, set Largest to the 3rd number
Step 4 If the 4th number is greater than Largest, set Largest to the 4th number
Step 5 If the 5th number is greater than Largest, set Largest to the 5th number

(13) Output Fig 2


Mr. Joseph Ecklu (DCS) Slide 7
Example: Finding the largest integer among
five integers
In other for Algorithms to be widely accepted in the programming
community, it needs Refinement. In the example below, we
initialize the largest number to minus infinity (∞) and current
number replacing the positions of the numbers
(12, 8, 13, 9, 11) Input

Step 0 Set largest to - ∞

Step 1 If the current integer is greater than Largest, set Largest to current integer

Step 5 If the current integer is greater than Largest, set Largest to current integer

(13) Output Fig 3


Mr. Joseph Ecklu (DCS) Slide 8
Example: Finding the largest integer among
five integers
It is possible to tell the computer repeat steps for a number of
time. This is know as generalization. In the fig 4, the algorithm give
the user the enter integer n time. The number of integers are
therefore not predefined.

Input data (n intergers )

Set largest to - ∞
Repeat the following steps

If the current integer is greater than Largest, set Largest to


current integer

Largest Fig 3
Mr. Joseph Ecklu (DCS) Slide 9
CONSTRUCTS
• In structured programming or
algorithms, a program must be made
up of only three constructs
– Sequence
– Decision
– Repetition

Mr. Joseph Ecklu (DCS) Slide 10


Sequence
An algorithm, and eventually a program is a
sequence of instructions, which one can be simple
instruction or either of the other two constructs

Do action 1
Do action 2

Do action n

Mr. Joseph Ecklu (DCS) Slide 11


Decision
Some sequence cannot be solved with only sequence
of simple instructions. Sometime we need to test a
condition. If the result of testing is true, we follow a
sequence of instructions: if it is false we follow a
different sequence of instructions, this is called
decision (sequence) constructs.
If a condition is true
Do a series of action
else
Do another series of actions
Mr. Joseph Ecklu (DCS) Slide 12
Repetition
In some problems, some sequence of insructions
must be repeated. We handle this with the repetition
or loop construct. Finding the largest integer among
a set of integers can use a construct of this kind.

While a condition is true


Do a series of action

Mr. Joseph Ecklu (DCS) Slide 13


ALGORITHM REPRESENTATION
There are two basic tools for
representing algorithms. These tools
are
• Unified Modeling Language (UML)
• Pseudocode

Mr. Joseph Ecklu (DCS) Slide 14


Unified Modeling Language (UML)
Unified Modeling Language (UML) is a pictorial
representation of an algorithm. It hides all the details
of an algorithm in an attempt to give the “big
picture” and to show how the algorithm flows from
beginning to end.

Mr. Joseph Ecklu (DCS) Slide 15


Unified Modeling Language (UML)

Action 1
FalseAction 1 TrueAction 1

Action 1 TrueAction 1

FalseAction 1 TrueAction 1
TrueAction 1
Action 1

a. Sequence b. Decision c. Repetition


Mr. Joseph Ecklu (DCS) Slide 16
Flowcharts
Used for start and stop
Used for showing flow of control

Used for decisions (Yes/No or True/False

Used for input and output

Used for processing

Used to join two different pages


Mr. Joseph Ecklu (DCS) Slide 17
Flowcharts example
Start

Input a, b, c

Smallest = a

Is Yes
Smallest = b
b<smallest
No
A

Mr. Joseph Ecklu (DCS) Slide 18


Flowcharts example
A

Is Yes Smallest = c
c<smallest
No
Print smallest

Stop

The above is an example flowchart for finding the smallest


number for three sent of numbers
Mr. Joseph Ecklu (DCS) Slide 19
Pseudocode
Pseudocode is an English-language-like representation
of an algorithm. There is no standard for pseudocode
– in pseudocode design, a lot of details and less
details can be used
If (condition)
{
action 1 trueAction(s) While (condition)
} {
action 2 Action(s)

action n
Else
{ }
FalseAction(s) b. Decision
}
a. Sequence
b. Decision
Mr. Joseph Ecklu (DCS) Slide 20
Pseudocode example
Algorithm: FindingSmallest (list)
Purpose: Find the smallest number among a list of numbers
Pre: List of numbers
Post: None
Return: the smallest number in the list
{
smallest …… first number
loop (not to end of list)
{
if (next number < smallest)
{
smallest….. Second number
}
}
Return Value of smallest
}
Mr. Joseph Ecklu (DCS) Slide 21
Pseudocode example
In the pseudocode above, there are some basic characteristics

• Purpose: A short statement about what the algorithm does


• Precondition (Pre): A list of any precursor requirement.
• Postcondition (Post): identifies an effect created by the
algorithm.
• Return: Every algorithm should show what is returned from
the algorithm. It there is nothing to be returned, it is
advisable that Null is specified.
• Statements: they are commands such as assign, input,
output, if-then-else, and loop
Mr. Joseph Ecklu (DCS) Slide 22
Session Summary
Algorithm helps the programmer to derive a step by
step approach of solving a problem. This session has
explained how to use flowcharts and pseudocode as
methods of step by step problem solving.

Mr. Joseph Ecklu (DCS) Slide 23

You might also like