chapter 5 algorithms
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Chapter 5 AlgorithmsChapter 5 Algorithms
Introduction to computer, 2nd semester, 2010/2011Mr.Nael Aburas [email protected] of Information TechnologyIslamic University of Gaza
Introduction 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 Introduction Machine cycle:
◦As long as the halt instruction not executed
◦Continue to execute the following steps:a) Fetch an instructionb) Decode the instructionc) Execute the instruction
Introduction 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 The abstract nature of algorithmalgorithm 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 Process, algorithm, and programprogramProgram is a presentation of an
algorithmProcess is activity of executing a
program “algorithm”.
Algorithm representation 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 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 representationSemantic: meaning of the primitive
Pseudocode 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 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 A+BGrade mid+final
So that the grade can be used on future to refer to that sum.
Pseudocode 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 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 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 sub-program, procedure, model, and functions.
We will use the term procedure Syntax:
Procedure name
Figure 5.4 The procedure Figure 5.4 The procedure Greetings in pseudocodeGreetings in pseudocode
5-14
Iterative StructuresIterative StructuresPretest loop:
while (condition) do (loop body)
Posttest loop:repeat (loop body) until(condition)
5-15
Figure 5.7 Components of Figure 5.7 Components of repetitive controlrepetitive control
5-16
RecursionRecursionThe 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.
5-17
Flow chartFlow chartA 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 Flowchart symbols
Construct Flowchart 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 1Example 1Draw a flowchart to find the sum
of first 50 natural numbers
Example 2 Example 2 Draw a flowchart to find the largest of three
numbers A, B, and C.
NOYES
Start
Read A, B,C
IS A>B?
IS B>C?
IS A>C?
Print APrint B Print C
YESNO
YES
NO
END