chapter 5 algorithms

22
Chapter 5 Algorithms Chapter 5 Algorithms Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas [email protected] Faculty of Information Technology Islamic University of Gaza

Upload: joel-mays

Post on 04-Jan-2016

18 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: Chapter 5 Algorithms

Chapter 5 AlgorithmsChapter 5 Algorithms

Introduction to computer, 2nd semester, 2010/2011Mr.Nael Aburas [email protected] of Information TechnologyIslamic University of Gaza

Page 2: Chapter 5 Algorithms

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.

Page 3: Chapter 5 Algorithms

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

Page 4: Chapter 5 Algorithms

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.

Page 5: Chapter 5 Algorithms

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.

Page 6: Chapter 5 Algorithms

Process, algorithm, and Process, algorithm, and programprogramProgram is a presentation of an

algorithmProcess is activity of executing a

program “algorithm”.

Page 7: Chapter 5 Algorithms

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).

Page 8: Chapter 5 Algorithms

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

Page 9: Chapter 5 Algorithms

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.

Page 10: Chapter 5 Algorithms

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.

Page 11: Chapter 5 Algorithms

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)

Page 12: Chapter 5 Algorithms

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)

Page 13: Chapter 5 Algorithms

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

Page 14: Chapter 5 Algorithms

Figure 5.4 The procedure Figure 5.4 The procedure Greetings in pseudocodeGreetings in pseudocode

5-14

Page 15: Chapter 5 Algorithms

Iterative StructuresIterative StructuresPretest loop:

while (condition) do (loop body)

Posttest loop:repeat (loop body) until(condition)

5-15

Page 16: Chapter 5 Algorithms

Figure 5.7 Components of Figure 5.7 Components of repetitive controlrepetitive control

5-16

Page 17: Chapter 5 Algorithms

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

Page 18: Chapter 5 Algorithms

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.

Page 19: Chapter 5 Algorithms

Flowchart symbols Flowchart symbols

Page 20: Chapter 5 Algorithms

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

Page 21: Chapter 5 Algorithms

Example 1Example 1Draw a flowchart to find the sum

of first 50 natural numbers

Page 22: Chapter 5 Algorithms

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