tugas progdas 02 - developing an algorithm

Upload: freztstep

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    1/17

    CHAPTER 3 :

    DEVELOPINGANALGORITHM

    Muh Anung Darmawan 33209

    Muhammad Muzani 33267

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    2/17

    DEFINING THE PROBLEM (1)

    To help with the initial analysis, the problem

    should be divided into three separate

    components:

    1. Input: a list of the source data provided to theproblem.

    2. Output: a list of the outputs required.

    3. Processing: a list of actions needed to produce the

    required outputs.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    3/17

    DEFINING THE PROBLEM (2)

    EXAMPLE : Add three numbers

    1. Tackle this problem in two stages. First, underline the

    nouns and adjectives used in the specification. We get

    2. By looking at the underlined nouns and adjectives,

    you can see that the input for this problem is threenumbers and the output is the total. It is helpful to

    write down these first two components in a simple

    diagram, called a defining diagram.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    4/17

    DEFINING THE PROBLEM (3)

    3. Then, underline the verbs and adverbs used in thespecification. This will establish the actions required. We get:

    4. By looking at the underlined words, we can see that theprocessing verbs are 'read', 'add together' and 'print'. We geta complet defining diagram :

    When we get a complete defining diagram, the problem hasbeen properly defined. That is, we now understand the inputto the problem, the output to be produced, and the processingsteps required to convert the input to the output.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    5/17

    DEFINING THE PROBLEM (4)

    When it comes to writing down the processing steps in

    an algorithm, you should use words that describe thework to be done in terms of single, specific tasks or

    functions. For example:

    There is a pattern in the words chosen to describe these

    steps. Each action is described as a single verb followed

    by a two-word object. Studies have shown that if you

    follow this convention to describe a processing step,

    two benefits will result. First, you are using a disciplinedapproach to defining the problem; and second, the

    processing is being divided into separate tasks or functions.

    This simple operation of dividing a problem into separate

    functions and choosing a proper name for each function is

    extremely important later, when considering algorithm

    modules.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    6/17

    DESIGNING A SOLUTION ALGORITHM (1)

    it is important that we are not too anxious to

    start coding until the necessary steps of

    defining the problem and designing the

    solution algorithm have been completed. Here are solution algorithm for the preceding

    example. It involve sequence control structures

    only; there are no decisions or loops, so the

    solution algorithms are relatively simple.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    7/17

    DESIGNING A SOLUTION ALGORITHM (2)

    Example : Solution algorith for last example

    This diagram shows what is required, and a simple calculation

    will establish how. Using pseudocode, and the sequence controlstructure, establish the solution algorithm as follows:

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    8/17

    DESIGNING A SOLUTION ALGORITHM (3)

    There are a number of points to consider in this solution

    algorithm:

    1. Aname has been given to the algorithm, namely

    Add_three_numbers. Algorithm names should briefly describe the

    function of the algorithm, and are usually expressed as a single

    verb followed by a two-word object.

    2. An END statement at the end of the algorithm indicates that the

    algorithm is complete.

    3. All processing steps between the algorithm name and the END

    statement have been indented for readability.

    4. Each processing step in the defining diagram relates directly to oneor more statements in the algorithm. For instance, 'Read three

    numbers' in the defining diagram becomes 'Read number1,

    number2, number3' in the algorithm; and 'Add number together'

    becomes 'total = number1 + number2 + number3'.

    Now that the algorithm is complete, we should desk check the

    solution and then translate it into a programming language.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    9/17

    DESIGNING A SOLUTION ALGORITHM (4)

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    10/17

    CHECKING THE SOLUTION ALGORITHM

    (1)

    After a solution algorithm has been established,

    it must be tested for correctness. This step is

    necessary because most major logic errors occur

    during the development of the algorithm, and if

    not detected, these errors can be passed on to the

    program

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    11/17

    CHECKING THE SOLUTION ALGORITHM

    (2)

    Selecting test data

    y By doing this, we will still be able to concentrate on

    what the program is supposed to do, not how.

    Steps in desk checking an algorithm1. Choose simple input test cases that are valid.

    Two or three test cases are usually sufficient.

    2. Establish what the expected result should be for

    each test case

    3. Make a table on a piece of paper of the relevantvariable names within the algorithm

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    12/17

    CHECKING THE SOLUTION ALGORITHM

    (3)

    4. Walk the first test case through the algorithm,

    line by line, keeping a step-by-step record of the

    contents of each variable in the table as the data

    passes through the logic.

    5. Repeat the walk-through process using theother test data cases, until the algorithm has

    reached its logical end.

    6. Check that the expected result established in

    Step matches the actual result developed in Step 5.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    13/17

    CHECKING THE SOLUTION ALGORITHM

    (4)

    By desk checking an algorithm, you are

    attempting to detect early errors.

    Desk checking will eliminate most errors, but it

    still cannot prove that the algorithm is 100%correct!

    Bellow is an example of desk checking

    (meskipun hanya 4 langkah, tetapi contoh

    tersebt telah melewati 6 langkah yang telah

    dijelaskan di 2 slide sebelum ini) :

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    14/17

    CHECKING THE SOLUTION ALGORITHM

    (5)

    We have :

    1. We input 2 data set, we get :

    2. We calculate the 2 input data set, we 2 set output :

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    15/17

    CHECKING THE SOLUTION ALGORITHM

    (6)

    3. Set up a table of relevant variable names, and pass

    each test data set through the solution algorithm,

    statement by statement. Line numbers have been

    used to identify each statement within the program.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    16/17

    CHECKING THE SOLUTION ALGORITHM

    (7)

    4. Check that the expected results (60 and 123) match

    the actual results (the total column in the table).

    This desk check, which should take no more than a few

    minutes, indicates that the algorithm is correct. You can

    now proceed to code the algorithm into a programminglanguage.Note that if, at the end of a desk check, the

    actual results do not match the expected results, the

    solution algorithm probably contains a logic error. In this

    case, the programmer needs to go back to the solution

    algorithm, fix the error, then desk check the

    algorithm again.

  • 8/6/2019 TUGAS PROGDAS 02 - Developing an Algorithm

    17/17

    THEEND