20070905-ch1-intro

Upload: samuel-chan

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 20070905-ch1-intro

    1/24

    Programming Fundamentals

    and Methodology

    COMP104: Introduction

    Dr. Brian MakDr. Pedro Sander

    Dr. Zhen Zhou

    Department of Computer Science & Engineering

    The Hong Kong University of Science and TechnologyHong Kong SAR, China

    Fall 2007

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.1

  • 8/3/2019 20070905-ch1-intro

    2/24

    Part I

    Computer, Computer Program, and ProgrammingLanguages

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.2

  • 8/3/2019 20070905-ch1-intro

    3/24

    Course Objectives

    To learn how to solve problems by writing computer programs.

    To learn how to design a computer program.

    To learn how to program in C++.

    To learn how to debug a computer program.To learn object-oriented programming.

    To prepare you for COMP151, COMP171, etc.

    Question: computer science = programming?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.3

  • 8/3/2019 20070905-ch1-intro

    4/24

    Whats a Computer Program?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.4

  • 8/3/2019 20070905-ch1-intro

    5/24

    Whats a Computer Program? ..

    Human work model

    Computer work model

    WORKERwork output

    HUMANINSTRUCTIONS

    work outputCOMPUTER

    PROGRAM

    A computer program is a set of machine-readable instructionsthat tells a computer how to perform a specific task.(During the execution of the program, it may interact withthe users and its environment.)

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.5

  • 8/3/2019 20070905-ch1-intro

    6/24

    von Neumann Computer Architecture

    ControlUnit

    Memory

    I/O device

    results of operations instructions/data

    CPU

    ALU

    Designed by John von Neumann, a mathematician, in 1945.

    It is still todays dominant computer architecture.

    For efficiency, many programming languages, including C++,are designed to take advantage of the architecture.

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.6

  • 8/3/2019 20070905-ch1-intro

    7/24

    Is This a Computer?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.7

  • 8/3/2019 20070905-ch1-intro

    8/24

    Whats a Programming Language for?

    Bjarne Stroustrup (C++ designer, 1994):tool for instructing machines?

    means for communicating between programmers?

    vehicle for expressing high level designs?

    notation for algorithms?way of expressing relationships between concepts?

    tool for experimentation?

    means for controlling computerized devices?

    collection of neat features?

    His answer: All of the above except

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.8

    C

  • 8/3/2019 20070905-ch1-intro

    9/24

    Can You Understand This?

    0000100100101110011001100110100101101100011001010000100100100010011011000110010101100011011101000111010101110010011001010011000100101110011000110010001000001010011001110110001101100011001100100101111101100011011011110110110101110000011010010110110001100101011001000010111000111010000010100010111001110011011001010110001101110100011010010110111101101110000010010010001000101110011101000110010101111000

    0111010000100010000010100000100100101110011000010110110001101001011001110110111000100000001101000000101000001001001011100110011101101100011011110110001001100001011011000010000001101101011000010110100101101110000010100000100100101110011101000111100101110000011001010000100100100000011011010110000101101001011011100010110000100011011001100111010101101110011000110111010001101001011011110110111000001010000010010010111001110000011100100110111101100011000010010011000000110100000010100110110101100001011010010110111000111010000010100000100100100001001000110101000001010010010011110100110001001111010001110101010101000101001000110010000000110000000010100000100101110011011000010111011001100101001000000010010101110011011100000010110000101101001100010011001000111000001011000010010101110011011100000000101000001010000010010010000100100011010100000101001001001111010011000100111101000111010101010100010100100011001000000011000100001010000010010110110101101111011101100010000000110001001011000010010101101111001100000000101000001001011100110111010000100000001001010110111100110000001011000101101100100101011001100111000000101101001100100011000001011101000010100000100101101101011011110111011000100000001100100010110000100101011011110011000000001010000010010111001101110100001000000010010101101111001100000010110001011011001001010110011001110000001011010011001000110100010111010000101000001001011011000110010000100000010110110010010101100110011100000010110100110010001100000101110100101100001001010110111100110000000010100000100101101100011001000010000001011011001001010110011001110000001011010011001000110100010111010010110000100101011011110011000100001010000010010110000101100100011001000010000000100101011011110011000000101100001001010110111100110001001011000010010101101111001100000000101000001001011100110111010000100000001001010110111100110000001011000101101100100101011001100111000000101101001100100011

    100001011101000010100000100101101101011011110111011000100000001100000010110000100101011010010011000000001010000010010110001000100000001011100100110001001100001100010000101000001001011011100110111101110000000010100010111001001100010011000011000100111010000010100000100101110010011001010111010000001010000010010111001001100101011100110111010001101111011100100110010100001010001011100100110001001100011001100110010100110001001110100000101000001001001011100111001101101001011110100110010100001001001000000110110101100001011010010110111000101100001011100100110001001100011001100110010100110001001011010110110101100001011010010110111000001010000010010010111001101001011001000110010101101110011101000000100100100010010001110100001101000011001110100010000000101000010001110100111001010101001010010010000000110010001011100011100000101110001100010010001000001010

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.9

    H Ab Thi ?

  • 8/3/2019 20070905-ch1-intro

    10/24

    How About This?

    main:

    !#PROLOGUE# 0save %sp,-128,%sp

    !#PROLOGUE# 1

    mov 1,%o0

    st %o0,[%fp-20] mov 2,%o0

    st %o0,[%fp-24]

    ld [%fp-20],%o0

    ld [%fp-24],%o1

    add %o0,%o1,%o0

    st %o0,[%fp-28]

    mov 0,%i0

    nop

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.10

    I Thi B N ?

  • 8/3/2019 20070905-ch1-intro

    11/24

    Is This Better Now?

    #include

    int main()

    {

    int x, y, z;

    x = 1 ;

    y = 2 ;

    z = x+y;

    return 0;

    }

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.11

    L l f P i L

  • 8/3/2019 20070905-ch1-intro

    12/24

    Levels of Programming Languages

    machine (binary) language is unintelligible

    assembly language is low level

    mnemonic names for machine operationsexplicit manipulation of memory addresses/contentsmachine-dependent

    high level languagereadable

    instructions are easy to rememberfaster coding

    less error-prone (fewer bugs?)easier to maintain

    no mention of memory locationsmachine-independent = portable

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.12

    G l f C P i L

  • 8/3/2019 20070905-ch1-intro

    13/24

    Genealogy of Common Programming Languages

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.13

    C il ti F S t R bl P

  • 8/3/2019 20070905-ch1-intro

    14/24

    Compilation: From Source to Runnable Program

    A compiler translates source

    programs into machine codesthat run directly on the targetcomputer.

    e.g. a.cpp a.out.

    static codes

    compile once, run many

    optimized codes more efficient

    examples: FORTRAN,Pascal, C++

    machine A compiler

    source code

    machine A code

    output

    run

    compile

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.14

  • 8/3/2019 20070905-ch1-intro

    15/24

    Part II

    Problem Solving and Computer Programming

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.15

    Example: Find the Best Way to Travel from HKUST to

  • 8/3/2019 20070905-ch1-intro

    16/24

    Example: Find the Best Way to Travel from HKUST to

    the Peak Tram Station in Central

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.16

    Step 1: Define the Problem

  • 8/3/2019 20070905-ch1-intro

    17/24

    Step 1: Define the Problem

    What do we mean by the best way?

    fastest?cheapest?most comfortable?

    What types of transportation means are allowed?

    How many transits are allowed?

    How to obtain the departure location and destination fromthe users?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.17

    Step 2: Develop a Solution

  • 8/3/2019 20070905-ch1-intro

    18/24

    Step 2: Develop a Solution

    Find out all possible bus/minibus/MTRroutes and their fares.

    Evaluate all possible combination of their

    routes.CS people will ask: how fast can weevaluate all the possible routes?

    Select the route that meets our goal

    (fastest/cheapest etc.)

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.18

    Steps 3 5: Testing Documentation Maintenance

  • 8/3/2019 20070905-ch1-intro

    19/24

    Steps 3 5: Testing, Documentation, Maintenance

    Testing

    Test the best route found by your solution.Make sure the conditions have not changed.

    has the transport fare been increased?any road construction?

    Documentation: Describe and explain the solution.

    Maintenance: Test and revise the routes as needed.

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.19

    One Solution: efinder ust hk

  • 8/3/2019 20070905-ch1-intro

    20/24

    One Solution: efinder.ust.hk

    Best means: cheapest, fastest, a tradeoff calledefinder-route

    how much are you willing to pay to save X minute of traveltime?

    Transport vehicles: bus, minibus, MTR.

    Allow inputs by typing, mouse selection, voice input.

    Get the fare and route information from the companies.

    Put all information into a database.

    Compute all best routes between any two locations by theFloyd-Warshall algorithm one solution that solves the

    well-known all-pairs shortest path problem.(c.f. traveling salesman problem)

    Update the database and re-compute all the best routes whenthe route and fare information changes.

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.20

    Shortest Path Problem

  • 8/3/2019 20070905-ch1-intro

    21/24

    Shortest Path Problem

    A

    B

    D

    E

    1

    2

    3

    4

    5

    8

    6

    C

    Question: Which is the shortest path from A to E?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.21

    Traveling Salesman Problem

  • 8/3/2019 20070905-ch1-intro

    22/24

    Traveling Salesman Problem

    A

    B

    D

    E

    1

    2

    3

    4

    5

    8

    6

    C

    7

    9

    Question: A salesman wants to travel all cites {A, B, C, D, E }starting from A, visiting each other city once and only once.Which is the shortest round-trip path?

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.22

    Problem Solving

  • 8/3/2019 20070905-ch1-intro

    23/24

    Problem Solving

    Define the problem.Be specific

    What are the input(s) and output(s)?Any constraints?Which information is essential?

    Can it be done at all?May need refining the problem definition.

    Develop a solution.

    Outline the solution first.Write down the details step-by-step.

    Test the solution.Revise the solution if necessary, based on the test results.

    Repeat the develop and test steps until the solution works.

    Document the solution.Actually, you should document your solution while you aredeveloping it, not after you have developed it.

    Maintain the solution.revise the solution if conditions are changed.

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.23

    Programming as Problem Solving

  • 8/3/2019 20070905-ch1-intro

    24/24

    og a g as ob e So g

    Define the problem.

    What are the input(s) and output(s)?Any constraints?Which information is essential?

    Develop a solution.

    Construct an algorithm.

    Implement the algorithm by writing a program.Compile the program.

    Test the program.

    Debug the program.

    Document the program as you write the program.Maintain the program

    modify the codes when conditions change.enhance the codes to improve the solution.

    { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.24