lecture 1 course intro

Upload: juni-b

Post on 07-Aug-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 Lecture 1 Course Intro

    1/24

    Lecture 1

    Programming

    Fundamental

  • 8/21/2019 Lecture 1 Course Intro

    2/24

    Software

    The intermediary between you (the user)and the hardware

    Operating system (OS) see the next page

    Windows, OS X, LinuxApplication programs

    End-user applications Word processor, etc.

    Matlab, etc. Application development software

    (programming languages)

    C, Matlab (sort of), Python, Java, FORTRAN, etc.

  • 8/21/2019 Lecture 1 Course Intro

    3/24

    Operating System (OS)

    A program that: Acts as an intermediarybetween

    hardware and applicationsoftware

    Provides a consistent, stable wayfor applications to interact withhardware APIs, so you dont have to do it all

    yourself

    Examples: Windows XP/Vista

    Linux

    http://en.wikipedia.org/wiki/File:Operating_system_placement.svg

  • 8/21/2019 Lecture 1 Course Intro

    4/24

    Not correct

    Correct

    Create/Edit

    source files(your program!)

    Program Development

    Compile

    source files

    Link

    compiled files

    Load

    executable file

    Run

    your program!

    Test

    Repeat process

  • 8/21/2019 Lecture 1 Course Intro

    5/24

    Method for Developing a Program

    1. Define the problem:

    State the problem you are trying to solve in clear andconcise terms.

    2. List the inputs and the outputs Inputs: information needed to solve the problem

    Outputs: what the algorithm will produce as a result

    3. Describe the steps needed to convert or manipulate theinputs to produce the outputs (develop the algorithm)

    Begin at a high-level first

    Refine (subdivide the high-level) steps until they areeffectively computable operations.

    4. Test the algorithm:

    choose data sets, and verify that your algorithm works!

    Note: these steps are to be done BEFOREyou write any program code!

  • 8/21/2019 Lecture 1 Course Intro

    6/24

    Structured Programming

    Sequence

    Selection

    IF

    IFELSE SWITCH

    Repetition

    WHILE DOWHILE

    FOR

    Flowchart constructs

  • 8/21/2019 Lecture 1 Course Intro

    7/24

    Algorithm

    What is an algorithm?

    A recipe

    A procedure

    Definition:

    well-ordered collection of unambiguous and

    effectively computable operations, that when

    executed, produces a result and halts in a finite

    amount of time.

  • 8/21/2019 Lecture 1 Course Intro

    8/24

    Characteristics of an Algorithm

    Well-ordered:

    the steps are in a clear order

    Unambiguous:

    the operations described are understood by a

    computing agent without further simplification

    Effectively computable:

    the computing agent can actually carry out

    the operation

  • 8/21/2019 Lecture 1 Course Intro

    9/24

    Pseudocode

    natural language-like statements that preciselydescribe the steps of an algorithm

    Statements that describe actions

    Focuses on the logicof the algorithm Avoids language-specific elements

    Written at a level so that code can be generatedalmost automatically from each statement

    Steps are numbered Subordinate numbers and/or indentation are used for

    dependent statements in selection and repetitionstructures

  • 8/21/2019 Lecture 1 Course Intro

    10/24

    Flowcharts - 1

    Flowcharts

    A graphical tool that diagrammaticallydepicts

    the steps and structure of an algorithm or

    programSymbol Name/Meaning Symbol Meaning

    ProcessAny type of internal

    operation: data transformation,

    data movement, logic operation,

    etc.

    Connectorconnects sections

    of the flowchart, so that the

    diagram can maintain a smooth,

    linear flow

    Input/Output

    input or outputof data

    Terminal

    indicates start orend of the program or algorithm

    Decisionevaluates a condition

    or statement and branches

    depending on whether the

    evaluation is true or false

    Flow linesarrowsthat

    indicate the direction of the

    progression of the program

  • 8/21/2019 Lecture 1 Course Intro

    11/24

    Flowchart Constructs - Sequence and Selection

    from Deitel & Deitel, 6th ed., p. 122Control Structures

  • 8/21/2019 Lecture 1 Course Intro

    12/24

    Flowchart Constructs - Repetition

    from Deitel & Deitel, 6th ed., p. 122

    Control Structures

  • 8/21/2019 Lecture 1 Course Intro

    13/24

    Example:

    Obtain a series of positive numbers from the keyboard, and

    determine and display their sum. Assume that the user types the

    sentinel value -1 to indicate "end of data entry"

    Define the problem

    Statement pretty well defines the problem

    List inputs and outputs

    inputs: number entered from keyboard

    outputs: sum of number

  • 8/21/2019 Lecture 1 Course Intro

    14/24

    Example:

    Obtain a series of positive numbers from the keyboard, and

    determine and display their sum. Assume that the user types the

    sentinel value -1 to indicate "end of data entry"

    Develop the algorithm

    High-level first, then refine:

    Does this work?

    1. Start

    2. Declare variables: ________

    3. Repeat while number not equal to -1

    3.1. get number3.2. add to sum

    4. Display sum

  • 8/21/2019 Lecture 1 Course Intro

    15/24

    Example:

    1. Start

    2. Declare variables: num, sum

    3. while numnot equal to -1, continue doing:

    3.1. Display prompt Enter positive number

    3.2. Read number from the keyboard3.3. Display number entered

    3.4. add to sum

    4. Display sum

    1. Start

    2. Declare variables: ________3. Repeat while number not equal to -1

    3.1. get number

    3.2. add to sum

    4. Display sum

    Develop the algorithm,cont.

    Refine

    Are we there yet?

  • 8/21/2019 Lecture 1 Course Intro

    16/24

    Example:

    1. Start

    2. Declare variables: num, sum3. while numnot equal to -1, continue doing:

    3.1. Display prompt Enter positive number

    3.2. Read number from the keyboard

    3.3. Display number entered

    3.4. if num less than zero, then3.4.1 continue

    3.5. add to sum

    4. Display sum

    Develop the algorithm,cont. Add a test to exclude negative numbers

    Are we there now?

  • 8/21/2019 Lecture 1 Course Intro

    17/24

    Flowchart StartDeclare variables: num, sum

    Intialize variables: num = 0, sum = 0

    Display "Enter

    positive integer"if num ! = -1

    if num < 0

    Read num from

    keyboard

    Display num

    Yes

    Yes

    sum = sum + num

    Display sum

    Stop

    No

    No

    1. Start

    2. Declare variables: num, sum

    3. while numnot equal to -1, continue doing:

    3.1. Display prompt Enter positive number

    3.2. Read number from the keyboard

    3.3. Display number entered

    3.4. if num less than zero, then

    3.4.1 continue

    3.5. add to sum

    4. Display sum

    Test the algorithm!

  • 8/21/2019 Lecture 1 Course Intro

    18/24

    Structure of a C Program

    A formal letter has a

    structure

    So does a program in C

    Burford Furman

    Professor

    Dept. of Mech. and Aero. Eng

    San Jos State University

    San Jose, CA 95192-0087

    July 20, 2009

    Dear Prof. Furman,

    Im writing you to see if I can get into ME 30

    Sincerely,

    Jane Student

    Title block

    Date

    Salutation

    Body

    Closing

    Signature

  • 8/21/2019 Lecture 1 Course Intro

    19/24

    C Code

    Programmers block

    Pre-processor directive

    Declare and initialize

    variables

    While loop

    (repetition structure)

    Main function (statements go between { } )

    return statement

  • 8/21/2019 Lecture 1 Course Intro

    20/24

    Programmers Block

    Include important information (comments)to documentthe program: Title

    Date

    Author

    Description

    Inputs/Outputs

    Algorithm

    Revision history

    Add comments using one of two methods:1./*put comment between */ (note: traditional C)

    2.//comment (note: single line only)

  • 8/21/2019 Lecture 1 Course Intro

    21/24

    # include (pre-processor directive)

    Includes a library file for standard io

    functions for things like printing, etc.

  • 8/21/2019 Lecture 1 Course Intro

    22/24

    main() function

    Your program needs amain() function

    Statements go between

    the braces { }

    main() ends with thereturnkeyword and

    usually the value zero

    If main() runs

    successfully, it returns

    a value of zero

  • 8/21/2019 Lecture 1 Course Intro

    23/24

    Declare and initialize variables

    Variables must be declared beforeyou can use them

  • 8/21/2019 Lecture 1 Course Intro

    24/24

    while() Repetition Structure

    Full program

    while (condi t ion

    )repeat statements

    between { }