1 / 23

Chapter 5 Algorithms

Chapter 5 Algorithms. Introduction to computer, 2nd semester, 2009/2010 Mr.Nael Aburas nras@iugaza.edu.ps Faculty of Information Technology Islamic University of Gaza. Introduction .

Télécharger la présentation

Chapter 5 Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Algorithms Introduction to computer, 2nd semester, 2009/2010 Mr.Nael Aburas nras@iugaza.edu.ps Faculty of Information Technology Islamic University of Gaza

  2. 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 unambiguous, 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.

  3. Introduction • Machine cycle: • As long as the halt instruction not executed • Continue to execute the following steps: • Fetch an instruction • Decode the instruction • Execute the instruction

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

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

  6. Process, algorithm, and program • Program is a representation of an algorithm • Process is activity of executing a program “algorithm”.

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

  8. 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 along 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

  9. Algorithm representation

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

  11. 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+B • Grade mid+final • So that the grade can be used on future to refer to that sum.

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

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

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

  15. Figure 5.4 The procedure Greetings in pseudocode

  16. Iterative Structures • Pretest loop: while (condition) do (loop body) • Posttest loop: repeat (loop body) until(condition)

  17. Figure 5.7 Components of repetitive control

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

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

  20. Flowchart symbols

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

  22. Example 1 • Draw a flowchart • to find the sum • of first 50 natural numbers

  23. Start Read A, B,C IS B>C? IS A>C? IS A>B? NO YES YES NO YES NO Print B Print A Print C END Example 2 • Draw a flowchart to find the largest of three numbers A, B, and C.

More Related