Algorithm1

Chapter 5 Algorithms
Introduction to computer, 2nd semester, 2010/2011
Mr.Nael Aburas [email protected]
Faculty of Information Technology
Islamic University of Gaza
Introduction
We have seen that before a computer can
perform a task, it must be given an algorithm
telling it what to do.
 Algorithm: set of ordered steps that define
how a task is performed.
 Algorithm: an ordered set of executable
steps that defines a terminating process
 We have seen some examples of algorithms
such as converting numeric representation,
as well as algorithm for the machine cycle
that is followed by the CPU.

Introduction

Machine cycle:
◦ As long as the halt instruction not executed
◦ Continue to execute the following steps:
a) Fetch an instruction
b) Decode the instruction
c) Execute the instruction
Introduction

As mentioned in the definition, the steps
required to be ordered, that means the
steps must have a well-established
structure in terms of the order of their
execution.
The abstract nature of algorithm
It is important to differentiate between the
algorithm and its representation.
 It is similar to different between story and
book.
 Story is abstract, book is a physical
representation of a story, is a book is
translated in another language or different
format, it still the representation of the
story.
 A single algorithm can be represented in
many ways.

Process, algorithm, and program
Program is a presentation of an algorithm
 Process is activity of executing a program
“algorithm”.

Algorithm representation
The goal of this section is to introduce the
basic of primitives and Pseudocode
 Primitives:
 The representation of an algorithm requires
some form of language, human language
(English, Arabic), or picture language.
 Such natural channels of communications
“natural language” leads to
misunderstandings (communication
problems).

Algorithm representation





Computer science approaches this problem by
establishing a well-defined set of building blocks from
which algorithm representation can be constructed,
such building block is called a primitive.
A collection of primitives a long with a collection of
rules stating how the primitives can be combined
constitutes a programming language.
Each primitive has its own syntax and semantic.
Syntax: primitive's symbolic representation
Semantic: meaning of the primitive
Pseudocode
Notational system in which ideas can be
expressed informally during the algorithm
development process.
 Pseudocode is a kind of structured
English for describing algorithms.
 Pseudocode generally does not actually
obey the syntax rules of any particular
language.

Pseudocode
Example: saving of a computed value.
If we have computed the sum of two value, and
we want to save the result.
 General syntax:


 Name

expression
We read it as assign name the value of the
expression (assignment statement)
 Result
 Grade
A+B
mid+final
 So that the grade can be used on future to refer to
that sum.
Pseudocode
Another recurring semantic structure is:
 If (condition) then (activity)
else (activity)
 Selection one of two possible activites.

 If (sales have decreased) then (lower the price by 5%)
 If (not raining ) then
(if (temperature = hot)
then ( go swimming)
else (play golf)
)
else (watch TV)
Pseudocode
Another recurring semantic structure
repeated execution of a statement:
 While (condition) do (activity)

◦ While (tickets remain to be sold) do (sell a
ticket)
X  3;
while (X < 5) do
(X  X + 1)
Pseudocode, procedure
 We
want to use out Pseudocode to
describe activities that can be used as
abstract tools in other applications
 Computer science has a variety of terms
for such programs units, including subprogram, procedure, model, and
functions.
 We will use the term procedure
 Syntax:
 Procedure name
Figure 5.4 The procedure Greetings in
pseudocode
514
Iterative Structures
Pretest loop:
while (condition) do
(loop body)
 Posttest loop:
repeat (loop body)
until(condition)

515
Figure 5.7 Components of repetitive
control
516
Recursion
The execution of a procedure leads to
another execution of the procedure.
 Multiple activations of the procedure are
formed, all but one of which are waiting
for other activations to complete.

517
Flow chart
A flowchart is a common type of
diagram, that represents an algorithm or
process.
 A flow chart is a graphical or symbolic
representation of a process. Each step in
the process is represented by a different
symbol.
 The flow chart symbols are linked
together with arrows showing the
process flow direction.

Flowchart symbols
Construct Flowchart

To construct an effective flowchart
 Define the process boundaries with starting and
ending points.
 Complete the big picture before filling in the details.
 Clearly define each step in the process
Example 1

Draw a flowchart to find the sum of first
50 natural numbers
Example 2

Draw a flowchart to find the largest of three numbers
A, B, and C.
Start
Read A, B,C
NO
IS
B>C?
YES
IS
A>C?
IS
A>B?
YES
NO
YES
NO
Print B
Print C
END
Print A