02 program design

Upload: wakt

Post on 04-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 02 Program Design

    1/32

    Fundamental Programming

    Program DesignBimo Sunarfri Hantono

    Electrical Engineering and Information TechnologyGadjah Mada University

    mailto:[email protected]?subject=tei103%20pseudocodemailto:[email protected]?subject=tei103%20pseudocode

  • 8/13/2019 02 Program Design

    2/32

    Objective• To describe the steps in the program development process

    • To introduce current program design methodology

    • To introduce procedural and object-oriented programming• To introduce algorithms and pseudocode

    • To describe program data

  • 8/13/2019 02 Program Design

    3/32

    Algorithm• Named after: Muhammad ibn Musa al-Khwarizmi of

    Khowarezm (now Khiva in Uzbekistan) Circa 780-850 C.E.(Common Era)

    • Book on arithmetic:

    • Hindu numeration, decimal numbers, use of zero, method for findingsquare root

    • Latin translation (c.1120 CE): “Algoritmi de numero Indorum”

    • Book on algebra• Hisab al-jabr w’al-muqabala

  • 8/13/2019 02 Program Design

    4/32

    Algorithm• A sequence of instructions specifying the steps required to

    accomplish some tasks

    • Examples• A cooking recipe

    • Assembly instructions for a model

    • The rules of how to play a game

    • Directions for driving from A to B

    • A knitting pattern• A car repair manual

    • etc

  • 8/13/2019 02 Program Design

    5/32

    How do we solve problems?• Just Do

    • Guess Work and Luck

    • Trial and Error• Experience

    • Scientifically

  • 8/13/2019 02 Program Design

    6/32

    Components of an Algorithm• Values represent quantities, amounts or measurements. May

    be numerical or alphabetical (or other things).

    • Variables are containers for values, place to store values.Variables may be restricted to contain a specific type of value.

    • Instructions should be simple and unambiguous, the systemknows about and should be able to actually do. Sequences (ofinstruction)

    • Selections (between instructions)• Repetitions (of instructions)

    • Procedures

    • Documentation

  • 8/13/2019 02 Program Design

    7/32

    An Algorithm Conclusion• An algorithm must:

    • simple

    • lucid

    • precise

    • unambiguous

    • give the correct solution

    • eventually end

  • 8/13/2019 02 Program Design

    8/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • Develop the outline into an algorithm• Test the algorithm for correctness

    • Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    9/32

    7 Steps in Program Development• Define the problem

    • The inputs,

    • The outputs,

    • The processing steps to produce the required outputs.

    • Outline the solution

    • Develop the outline into an algorithm

    • Test the algorithm for correctness

    • Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    10/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • The major processing steps involved, The subtask (if any), The userinterface (if any), The major control structures (e.g. Repetitionloops), The major variables and record structures, The mainlinelogic.

    • Develop the outline into an algorithm

    • Test the algorithm for correctness• Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    11/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • Develop the outline into an algorithm• Algorithm, a set of detailed, unambiguous and ordered instructions

    developed to describe the processes necessary to produce thedesired output from a given input.

    • Pseudocode, Flowcharts and Nassi-Schneiderman

    • Test the algorithm for correctness• Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    12/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • Develop the outline into an algorithm• Test the algorithm for correctness

    • This step is one of the most important in the development of aprogram, and yet it is the step most often forgotten.

    • The main purpose of desk checking the algorithm is to identify

    major logic errors early, so that they may be easily corrected.• Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    13/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • Develop the outline into an algorithm• Test the algorithm for correctness

    • Code the algorithm into a specific programming language

    • Run the program on the computer

    • This step uses a program compiler and programmer designed testdata to machine test the code for syntax errors (those detected atcompile time) and logic errors (those detected at run time).

    • This step may need to be performed several times until you aresatisfied that the program is running as required.

    • Document and maintain the program

  • 8/13/2019 02 Program Design

    14/32

    7 Steps in Program Development• Define the problem

    • Outline the solution

    • Develop the outline into an algorithm• Test the algorithm for correctness

    • Code the algorithm into a specific programming language

    • Run the program on the computer

    • Document and maintain the program• External documentation (such as hierarchy charts, the solution

    algorithm and test data results) and internal documentation thatmay have been coded in the program.

    • Program maintenance refers to changes that may need to be made

    to a program throughout its life.

  • 8/13/2019 02 Program Design

    15/32

    Data Validation• Data should always undergo a validation check before it is

    processed by a program.

    • Different types of data require different check, for example:

    • Correct type: the input data should match the data type definitionstated at the beginning of the program.

    • Correct range: the input should be within a required set of values.

    • Correct length: the input data should be the correct length.

    • Completeness: all required fields should be present.• Correct date: an incoming data should be acceptable.

  • 8/13/2019 02 Program Design

    16/32

    Example 1. Add Three Number• A program is required to read three numbers, add them

    together and print their total.

  • 8/13/2019 02 Program Design

    17/32

    Example 1. Add Three Number• A program is required to read three numbers, add them

    together and print their total.

    • Underline the nouns and adjectives used in the specification

    • establish the input, output component and any object that arerequired.

  • 8/13/2019 02 Program Design

    18/32

    Example 1. Add Three Number• A program is required to read three numbers, add them

    together and print their total.

    • Underline the nouns and adjectives used in the specification

    • establish the input, output component and any object that arerequired.

    Input Processing Output

    Number1Number2Number3

    Total

    Defining Diagram:

  • 8/13/2019 02 Program Design

    19/32

    Example 1. Add Three Number• A program is required to read three numbers, add them

    together and print their total.

    • Underline the verbs and adverbs used in the specification

    • establish the action required.

    Input Processing Output

    Number1Number2Number3

    Total

    Defining Diagram:

  • 8/13/2019 02 Program Design

    20/32

    Example 1. Add Three Number• A program is required to read three numbers, add them

    together and print their total.

    • Underline the verbs and adverbs used in the specification

    • establish the action required.

    Input Processing Output

    Number1Number2Number3

    Read three numbers Add numbers togetherPrint total number

    Total

    Defining Diagram:

  • 8/13/2019 02 Program Design

    21/32

    Example 2. Find average temperature• A program is required to prompt the terminal operator for the

    maximum and minimum temperature readings on a particularday, accept those readings as integers, and calculate and

    display to the screen the average temperature, calculated by(maximum temperature + minimum temperature)/2.

  • 8/13/2019 02 Program Design

    22/32

    Example 2. Find average temperature• A program is required to prompt the terminal operator for the

    maximum and minimum temperature readings on a particularday, accept those readings as integers, and calculate and

    display to the screen the average temperature, calculated by(maximum temperature + minimum temperature)/2.

    Input Processing OutputMax_tempMin_temp

     Avg_temp

    Defining Diagram:

  • 8/13/2019 02 Program Design

    23/32

    Example 2. Find average temperature• A program is required to prompt the terminal operator for the

    maximum and minimum temperature readings on a particularday, accept those readings as integers, and calculate and

    display to the screen the average temperature, calculated by(maximum temperature + minimum temperature)/2.

    Input Processing OutputMax_tempMin_temp

    Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature

     Avg_temp

    Defining Diagram:

  • 8/13/2019 02 Program Design

    24/32

    Example 3. Compute mowing time• A program is required to read from the screen the length and

    width of a rectangular house block, and the length and widthof the rectangular house that has been built on the block. The

    algorithm should then compute and display the mowing timerequired to cut the grass around the house, at the rate of twosquare meters per minute.

  • 8/13/2019 02 Program Design

    25/32

    Example 3. Compute mowing time• A program is required to read from the screen the length and

    width of a rectangular house block, and the length and widthof the rectangular house that has been built on the block. The

    algorithm should then compute and display the mowing timerequired to cut the grass around the house, at the rate of twosquare meters per minute.

    Input Processing Output

    Block_lengthBlock_widthHouse_lengthHouse_width

    Mowing_time

  • 8/13/2019 02 Program Design

    26/32

    Example 3. Compute mowing time• A program is required to read from the screen the length and

    width of a rectangular house block, and the length and widthof the rectangular house that has been built on the block. The

    algorithm should then compute and display the mowing timerequired to cut the grass around the house, at the rate of twosquare meters per minute.

    Input Processing Output

    Block_lengthBlock_widthHouse_lengthHouse_width

    Prompt for block measurementsGet block measurementsPrompt for house measurementsGet house measurementsCalculate mowing area

    Calculate mowing time

    Mowing_time

  • 8/13/2019 02 Program Design

    27/32

    Introduction to Program Data• Three main discussion:

    • Variables is the name given to a collection of memory cell, designedto store particular data item. The value stored may change or vary

    as the program executes.• Constants is a data item with a name and a value that remain the

    same during the execution of the program.

    • Literals is a constant whose name is the written representation of itsvalue.

  • 8/13/2019 02 Program Design

    28/32

    Data Types• Should be clearly defined at the beginning of the program.

    • Can be:

    • Elementary data items

    • Data structures

  • 8/13/2019 02 Program Design

    29/32

    Elementary Data Items• Contain single variable that is always treated as a unit.

    • The most common elementary data types:

    • Integer : representing a set of whole numbers, positive, negative orzero. e.g. 3, 576, -5

    • Real : representing a set of numbers, positive or negative, whichmay include values before or after a decimal point. Sometimesreferred to as floating point numbers. e.g. 19.2, 1.92E+01, -0.01

    • Character : representing the set of characters on the keyboard, plussome special characters. e.g. ‘A’, ‘b’, ‘$’

    • Boolean : representing a control flag or switch, which may containone of only two possible values; true or false.

  • 8/13/2019 02 Program Design

    30/32

    Data Structures• Data structure is an aggregate of other data items.

    • Its component could be elementary data items or another datastructure.

    • Data is grouped together in a particular way to reflects thesituation with which the program is concerned.

    • The most common data structures

    • String: a collection of characters that can be fixed or variable.

    •  Array: a data structure that is made up of a number of variables ordata items that all have the same data type and are accessed by thesame name.

    • Record: a collection of data items or fields that all bear somerelationship to one another.

    • File: a collection of records.

  • 8/13/2019 02 Program Design

    31/32

    Files• A popular method to enter and store information.

    • Major advantage of using files are:

    • Several different program can access the same data

    • Data can be entered and reused several times.

    • Data can be easily updated and maintained.

    • The accuracy of the data is easier to enforce.

    • Two different methods of storing data on files:

    • Sequential or text files, where data is stored and retrievedsequentially, may be opened to read or to write, but not bothoperations on the same file.

    • Direct or random-access files, where data is stored and retrievedrandomly, using a key or index, can be opened to read and write on

    the same file.

  • 8/13/2019 02 Program Design

    32/32