l02 - introduction to algorithms - جامعة نزوى 151 –introduction to algorithms 1...

Download L02 - Introduction To Algorithms - جامعة نزوى 151 –Introduction to Algorithms 1 Introduction to Algorithms Dr. Saleh Al-Hatali COMP 151 Introduction to Algorithms COMP 151

Post on 07-Apr-2018

217 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • COMP 151 Introduction to Algorithms 1

    Introduction to Algorithms

    Dr. Saleh Al-Hatali

    COMP 151

    Introduction to Algorithms

    COMP 151 - Introduction to Algorithms 1

    What is an Algorithm?

    A typical programming task can be divided into two phases:

    Problem solving phase

    Produces an ordered sequence of steps that describes the solution of problem

    This sequence of steps is called an algorithm

    Implementation phase

    This phase implements the program in some programming language

    COMP 151 - Introduction to Algorithms 2

  • COMP 151 Introduction to Algorithms 2

    What is an Algorithm?

    Algorithm

    A sequence of unambiguous instructions to solve a problem

    A well-defined procedure that takes some value(s) as input, process it and produces some value(s), as output

    COMP 151 - Introduction to Algorithms 3

    What is an Algorithm?

    Computer

    Problem

    Algorithm

    Input Output

    COMP 151 - Introduction to Algorithms 4

  • COMP 151 Introduction to Algorithms 3

    What is an Algorithm?

    Hence algorithm has three parts:

    Input

    Processing or transformation

    Output

    COMP 151 - Introduction to Algorithms 5

    Properties of an Algorithm Finiteness

    Terminates in finite number of steps

    Definiteness Each step must be clearly and carefully

    specified

    Input Valid inputs clearly specified

    Output Can be proved to produce the correct output

    given a valid input

    Effectiveness Steps must be simple and atomic

    COMP 151 - Introduction to Algorithms 6

  • COMP 151 Introduction to Algorithms 4

    Steps in Problem Solving

    First produce a general algorithm (one can use pseudocode)

    Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language.

    COMP 151 - Introduction to Algorithms 7

    What is Pseudocode?

    Pseudocode

    Artificial, informal language used to develop algorithms

    Similar to everyday English

    Understood by programmers

    Not executed on computers

    Used to think out a program before coding

    Easy to convert into C++ program

    COMP 151 - Introduction to Algorithms 8

  • COMP 151 Introduction to Algorithms 5

    Assignment

    name = expression

    set name = expression

    name expression

    Example

    RemainingFunds = CheckingBalance +

    SavingsBalance

    Pseudocode Primitives

    COMP 151 - Introduction to Algorithms 9

    Conditional selection

    if (condition) then

    activity

    Example

    if (sales have decreased) then

    lower the price by 5%

    Pseudocode Primitives (continued)

    COMP 151 - Introduction to Algorithms 10

  • COMP 151 Introduction to Algorithms 6

    Conditional selection

    if (condition) then

    activity

    else

    activity

    end if

    Example

    if (year is leap year) then

    daily total = total / 366

    else

    daily total = total / 365

    end if

    Pseudocode Primitives (continued)

    COMP 151 - Introduction to Algorithms 11

    Repeated execution

    while (condition)

    body

    Examplewhile (tickets remain to be sold)

    sell a ticket

    Pseudocode Primitives (continued)

    COMP 151 - Introduction to Algorithms 12

  • COMP 151 Introduction to Algorithms 7

    Indentation shows nested conditions

    if (not raining) then

    if (temperature == hot) then

    go swimming

    else

    play golf

    else

    watch television

    Pseudocode Primitives (continued)

    COMP 151 - Introduction to Algorithms 13

    Pseudocode Example

    Write an algorithm to read the

    final mark of a certain course

    and determine if the student

    passes or fails the course.

    COMP 151 - Introduction to Algorithms 14

  • COMP 151 Introduction to Algorithms 8

    Pseudocode Example

    Pseudocode:

    Input the final mark

    if mark is below 60

    Print FAIL

    else

    Print PASS

    COMP 151 - Introduction to Algorithms 15

    Pseudocode Example

    Detailed Algorithm:

    Step 1: Input MARK

    Step 2: if (MARK < 60) then

    Print FAIL

    else

    Print PASS

    endif

    COMP 151 - Introduction to Algorithms 16

  • COMP 151 Introduction to Algorithms 9

    What is a Flowchart?

    A graphical representation of the sequence of operations in an information system or program

    A graphical way to express an algorithm

    A picture is worth a thousand words

    COMP 151 - Introduction to Algorithms 17

    Flowchart Example

    PRINT

    PASS

    Step 1: Input MARK

    Step 2: if (MARK < 60) then

    Print FAIL

    else

    Print PASS

    endif

    START

    Input

    MARK

    IS

    MARK

  • COMP 151 Introduction to Algorithms 10

    Flow Chart Symbols

    There are no one definition for flowcharting symbols

    There are some guidelines for commonly used symbols

    Just be sure that when using these symbols you are consistent with their meaning

    COMP 151 - Introduction to Algorithms 19

    Basic Flowchart Shapes and Definitions

    Start / End

    The start or end of a workflow

    Process

    Process or action

    Connector

    Used to connect one part

    of a flowchart to another

    Decision

    Decision point in a process or

    workflow

    Input / Output

    Data: Inputs to, and outputs

    from, a process

    COMP 151 - Introduction to Algorithms 20

  • COMP 151 Introduction to Algorithms 11

    Flow Chart Symbols

    The Start/Stop Symbol is used at the beginning and end of a flowchart

    Start

    COMP 151 - Introduction to Algorithms 21

    Stop

    Flow Chart Symbols

    The Process Symbol represents any process, function, or action and is the

    most frequently used symbol in

    flowcharting

    C A + B

    COMP 151 - Introduction to Algorithms 22

  • COMP 151 Introduction to Algorithms 12

    Flow Chart Symbols

    The Input/Output Symbol represents data that is available for input or resulting from

    processing (i.e. customer database

    records)

    Display SUM

    COMP 151 - Introduction to Algorithms 23

    Flow Chart Symbols

    The Decision Symbol is a junction where a decision must be made. A single entry

    may have any number of alternative

    solutions, but only one can be chosen

    A > B?

    COMP 151 - Introduction to Algorithms 24

    NoYes

  • COMP 151 Introduction to Algorithms 13

    Flow Chart Symbols

    The Connector Symbol represents the exit to, or entry from, another part of the same

    flow chart. It is usually used to break a

    flow line that will be continued elsewhere.

    COMP 151 - Introduction to Algorithms 25

    General Rules for Flowcharts

    All boxes of the flowchart are connected with Arrows (Not lines).

    Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit point for all flowchart symbols is on the bottom except for the Decision symbol.

    The Decision symbol has two exit points; these can be on the sides or the bottom and one side.

    COMP 151 - Introduction to Algorithms 26

  • COMP 151 Introduction to Algorithms 14

    Various Structures of Algorithm

    Sequence (also known as Order or Process)

    Decision (also known as Selection)

    Repetition (also known Iteration or Looping)

    COMP 151 - Introduction to Algorithms 27

    The Sequence Structure

    This is the default structure of programs. Flow will transfer from start to end in sequence unless encountered by selection or repetitionstructures.

    Is also known as Process.

    COMP 151 - Introduction to Algorithms 28

  • COMP 151 Introduction to Algorithms 15

    The Sequence Structure

    Example: Write an algorithm to add two numbers and display their sum

    Start

    Step 1: Read num1

    Step 2: Read num2

    Step 3: sum num1 + num2

    Step 4: Display sum

    Stop

    COMP 151 - Introduction to Algorithms 29

    The Sequence Structure

    COMP 151 - Introduction to Algorithms 30

    STOP

    START

    Inputnum1, num2

    sum num1 + num2

    Displaysum

  • COMP 151 Introduction to Algorithms 16

    The Sequence Structure

    Start

    Read

    Num1

    Sum Num1 + Num2

    Read

    Num2

    Display

    Sum

    Stop

    Another Way:

    1. begin

    2. get num1

    3. get num2

    4. set sum num1 + num2

    5. display sum

    6. end

    COMP 151 - Introduction to Algorithms 31

    Exercises

    Write the pseudocode and draw the

    flowchart to find and display the

    following:

    1.The difference of two numbers.

    2.Both the sum and difference of two

    numbers.

    3.To read the first and last names and display

    a greeting, such as Hello Ahmed Al-

    Rashdi

    COMP 151 - Introduction to Algorithms 32

  • COMP 151 Introduction to Algorithms 17

    The Selection Structure

    Decision (Selection)

    Instructions are written based on some conditions. If the particular condition or conditions are satisfied then the program flow is in one route. Otherwise, it follows another route. ie, based on conditions the selection of program flow is jumped into some ways.

    COMP 151 - Introduction to Algorithms 33

    The Selection Structure

    This structure is also known as if-else structure

    Example:If A > B then

    print A

    else

    print B

    endif

    COMP 151 - Introduction to