Lec03 Algorithm 20160224 -...

44
演算法 Algorithm 彭旭霞 助理教授 國立清華大學 生醫工程與環境科學系 1

Transcript of Lec03 Algorithm 20160224 -...

  • Algorithm

    1

  • Contents

    Definition of Program and Algorithm Syntax and Semantics Pseudocode Operator

    2

  • Definition Program: developed to help perform tasks (to meet

    your needs)- a collection of statements or steps

    - solve a problem - converted into a language the computer understands to perform tasks

    Algorithm: - an ordered set of unambiguous, executablesteps that defines a terminating process

    - logically ordered set of statements: used tosolve a problem

    3

  • What is a Program?

    Interpreter (; )- Translates programs statements into a language

    the computer understands- ;

    Compiler ()- Application reading all the programs statements

    - Converts them into computer language- Produces an executable file running

    independently of an interpreter- =>=>

    4

  • Art of problem solving

    The creativity of art can not be taught.()

    We will talk about the representation of the art.

    5

  • Example: Taipei=>Kaohsiung

    =>

    =>=>

    =>=>

    6

    ?

    ?

  • Contents

    Definition of Program and Algorithm Syntax and Semantics Pseudocode Operator

    7

  • Different types of programming languages

    I Speak Computer

    8

  • I Speak Computer

    Low-level language- Use binary code for instructions- machine language, assembly language

    Machine language - Lowest-level programming language- Consist of binary bit patterns

    Assembly language ()- One step up from machine language- Assign letter codes to each machine-languageinstruction

    9

  • I Speak Computer Assembler ()

    - Program that reads assembly language code andconverts it into machine language

    High-level language- Written in a more natural language that humans can

    read and understand- eg.: Java, C++, Delphi, Fortran

    10

  • Syntax and Semantics

    Syntax ()- Symbolic representation- Rules for how a programming languagesstatements must be constructed

    Semantics ()- Meaning

    11

  • How to fold a bird?????? ?

    12

  • Syntax and semantics of folding paper

    13

  • Structure of a Program

    Before writing a program in any language: Know how the program should work Know the language syntax

    Formal definition of how statements must be constructed in the programming language

    Learning a programming language is similar to learning a foreign language

    14

  • How to start an algorithm or a program?(, !!)

    Help describe the method used to solve a problem- Break down each task in the plan into smallersubtasks

    - For many tasks, plan a series of logicalsteps to accomplish them

    - Provide a logical solution to a problem- Consist of steps to follow to solve the problem- Convert algorithm into programming statements by representing the steps in some format- Pseudocode is often used ()

    15

  • Flow chart

    Start / end

    Process

    Decision

    Path

    Input/output

    16

  • Take out all coins in your pocket

    Start

    Any coin in pocket?

    Take one coin from the pocket

    End

    yes

    no

    17

  • Contents

    Definition of Program and Algorithm Syntax and Semantics Pseudocode Operator

    18

  • Pseudocode

    flow chart: sometimes too complex Pseudocode: a notational system:

    less formal and intuitive ()

    19

  • Pseudocode

    Readable description of an algorithm written in human language- Template describing what needs converting into programming language syntax

    - No formal rules for writing pseudocode- Information should explain the process to someone with little experience in solving this typeof problem

    - Practice provides necessary skill

    20

  • Pseudocode: temperature conversion chart

    21

  • Pseudocode Start with the formulas needed in the algorithm:

    - Fahrenheit to Celsius: Celsius temp = (5/9) * (Fahrenheit temp 32)- Celsius to Fahrenheit: Fahrenheit temp = ((9/5) * Celsius temp) + 32

    After formulas are proved correct, begin outlining steps to write a program - Input from the user- Calculates the conversions- Displays results to the user

    22

  • Pseudocode

    23

  • Pseudocode

    Assignmentname expression

    Conditional selection- if condition then action

    - if condition then action1 else action2

    24

  • Contents

    Definition of Program and Algorithm Syntax and Semantics Pseudocode Operator

    25

  • Syntax of a Programming Language

    After defining an algorithm and testing the logic thoroughly, begin translating the algorithm

    May have many different ingredients:- Variables- Operators- Control structures- Objects

    26

  • Variables ()

    A name used to identify a certain location and value in the computers memory- Program type determines variable types needed- When a variable is defined, the data type is specified

    Advantages: - Access memory locations content

    - Use its value in a program- Easy way to access computer memory

    - No need to know actual hardware address Identifier: name of a variable

    27

  • Identifiers and Naming Conventions

    Identifier: access memory contents associated with a variable

    Items to consider when deciding on an identifier:- Name should describe data being stored

    - Use variable-naming standards- Can use more than one word for a variables identifier:eg.: Sun standard

    - Use meaningful names: XX? YY?

    28

  • Operators

    Symbols used to indicate data-manipulation operations- Manipulate data stored in variables- Classified by data type- One may work on numbers, and another oncharacters (depending on definition)

    29

  • Standard mathematical operators(may depend on Programming Language)

    Math Operators

    30

  • Relational Operators Main purpose is to compare values

    Standard relational operators

    31

    == = ?

  • Logical Operators

    Main function is to build a truth table when comparing expressions- Expression: programming statement returning a value when its executed

    - Usually use relational operators to compare variables

    Standard logical operators32

  • Precedence and Operators

    Precedence (,): order in which something is executed

    Symbols with a higher precedence executed before those with a lower precedence- Have a level of hierarchy

    Example: 2 + 3 * 4- Output = 14 (not 20)

    33

  • Order of relational and mathematical precedence

    Precedence and Operators

    34

  • Module form

    if (not raining)then ( if (temperature = hot)

    then (go swimming)else (play golf)

    )else (watch television)

    35

  • Module form

    Repeated executionwhile condition do activity

    Procedureprocedure name (generic names)

    36

  • Repeat 3 times

    37

  • Iterative Structures

    Pretest loop:while (condition) do

    (loop body)

    Posttest loop:repeat (loop body)

    until(condition)

    38

  • loop structure: while (pretest)

    39

  • loop structure: repeat (posttest)

    40

  • Repetition number: unknown

    Loop structure: while- unknown repetition number

    eg. While (the pH level is greater than 4) do

    (add a drop of sulfuric acid)

    We do not know how many drops are needed.

    41

  • Better algorithm or program

    Correctness Efficiency

    42

  • 43

  • :

    44