revision - algorithm testing

Upload: daniel-budd

Post on 07-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Revision - Algorithm Testing

    1/9

    Algorithm Testing

    Objective

    This document has been put together to assist you in studying Programming, Pseudocodeand Flow Charts. The document contains the following:

    1. Theory behind Testing Algorithms

    2. Activities on Testing Algorithms

    Materials

    To complete these activities you will only require this document and something to writewith:

    No Computers are needed No other reference material is required

    students may use pens, pencils, erasers and rulers if they wish.

    Procedures

    3. Attempt all the questions to the best of your ability

    4. Swap your work with peers and compare your solutions

    5. Justify why you have modeled your system the way you have.

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 1 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    2/9

    Trace TablesWhat are trace tables?

    A trace table is a technique used to test algorithms, in order to make sure that no logicalerrors occur whilst the algorithm is being processed. The table usually takes the form of amulti-column, multi-row table; with each column showing a variable, and each row showingeach number input into the algorithm and the subsequent values of the variables.

    Trace tables are typically used in schools and colleges when teaching students how toprogram, they can be essential tool in teaching students how a certain algorithm worksand the systematic process that is occurring when the algorithm is executed.

    The can also be useful for debugging applications, using a tract table can help aprogrammer easily detect what error is occurring, and why it may be occurring.

    Common Errors

    There are two common errors in programming; Logical and Syntax.

    Logical Errors often have correct syntax but item are placed in the wrong sequential orderto correctly run, or declarations are the wrong way around. They are errors concerned withsemantics. Syntax errors are when a period is in the wrong place, integer is speltincorrectly. It is the incorrect usage of the language rather then the way the language isused.

    Desk Checks vs. Test Plans

    A Desk Check concentrates on the value of variables and the logic i.e. what is the value ofvariable x after statement n; what is the next statement to be executed? A Test Planfocuses on the values of inputs and outputs required to test a program without concern forthe internal working i.e. are the results (outputs) correct for the inputs?

    What are Test Plans?

    Test plans are the most common form of trace tables. Test Plans shows the systematicprocess that takes place whilst the algorithm is processed, as the value of variable 1,variable 2and variable n, their new values are recorded in the trace table. Trace tables

    are useful for debugging in education, they make following an algorithmic process easy tofollow and understand.

    What are Desk Checks?

    Desk checking is a manual (non-computerised) technique for checking the logic of analgorithm. The person performing the desk check effectively acts as the computer, usingpen and paper to record results. The desk checker carefullyfollows the algorithm beingcareful to rigidly adhere to the logic specified. The desk check can expose problems withthe algorithm.

    Desk checks are useful to check an algorithm (before coding) thereby confirming that the

    algorithm works as expected and saves time possibly writing a program that doesnt dowhat it was intended to. Another benefit of a desk check is that it confirms to theprogrammer/designer that the algorithm performs as intended.

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 2 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    3/9

    A desk check is normally done as a table with columns for:

    1. Line Number - Pseudocode line number column (Pseudocode doesnt normally haveline numbers, but these are necessary in a desk check to specify the line(s) beingexecuted).

    2. One Column per Variables - The columns should be in alphabetical order on variable

    name with the variable name at the top of the column. As the algorithm is executed,the new value of the variables are put in the appropriate column.

    3. Condition - The result of the condition will be True (T) or False (F). As the algorithmis executed, conditions are evaluated and the details are recorded in the column.Show working when evaluating the conditions. This is used whenever a condition isevaluated - IF WHILE or FOR statements all have explicit or implicit conditions.

    4. Input/Output - Column is used to show what is input by the user and displayed by theprogram.

    Normally portrait page layout would be used for the desk check, however if the table is toowide, landscape layout may be used.

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 3 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    4/9

    Trace Table ExamplesProblem Description:

    Calculate the discounted price of an item purchased. (Note: a problem description is notnormally specified in a desk check, it is only included to assist in understanding theproblem.)

    The following example shows desk checking involving sequence, executing instruction oneafter the other. A sequence involves executing all instructions once, from top to bottom,one after the other.

    Algorithm (with line numbers added for the Desk Check)

    1. calcDiscountPrice( )

    2. Input price, discountPercent

    3. discount = price * discountPercent / 100

    4. discountPrice = price - discount

    5. Display discountPrice

    Examples of Test Plans

    Algorithm Price discountPercent Result

    discount = Price *

    discountPercent / 100

    $200 0.10 (10%) $20

    discountPrice = price -discount

    $200 $20 $180

    Example of Desk Checks

    # Price discountPercent discount discountPrice Input/Output

    1

    2 200 0.10

    3 20

    4 180

    5 180

    6

    *NB: Lines 1,6 have no algorithmic inputs or outputs so there is no entry made

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 4 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    5/9

    Example QuestionsProblem Description:

    The Computer Science Stage 2 and 3 course uses the preferred Desk Check method forTrace Tables. Answer the following questions using the desk check method shown above.

    Question 1) Inputs are 2 and 2

    1. BEGIN

    2. Input (Hours)

    3. Input (Rate)

    4. Output (Hours * Rate)

    5. END

    #

    1

    2

    3

    4

    5

    6

    Question 2) Inputs are John and Smith

    1. BEGIN

    2. Input (FirstName)

    3. Input (LastName)

    4. Output (FirstName + LastName)5. END

    #

    1

    2

    3

    4

    5

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 5 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    6/9

    Question 3) Inputs are 10 and 20

    1. BEGIN

    2. Input (num1)

    3. Input (num2)

    4. subTotal = (num1 * 0.10) + 50

    5. Total = subTotal - num2

    6. Output(Total)

    7. END

    #

    1

    2

    3

    4

    5

    6

    7

    Question 3) Inputs are 10 and 201. BEGIN

    2. Input (num1)

    3. Input (num2)

    4. subTotal = (num1 + num2) + 50

    5. Total = subTotal - num2

    6. Output(Total)

    7. END

    #

    1

    2

    3

    4

    5

    6

    7

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 6 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    7/9

    Question 4) The input value is 15

    1. BEGIN

    2. Total = 0

    3. REPEAT

    4. Read input

    5. IF input >= 0 then

    6. output = Input DIV 2

    7. ELSE

    8. output = Input

    9. ENDIF

    10. total = total + output

    11.

    UNTIL input = 212. Write(Total)

    # Input Output Total

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 7 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    8/9

    Question 4) The Input value is 2

    1. BEGIN

    2. Total = 0

    3. Read (Input)

    4. WHILE Input 2

    5. IF input >= 10 then

    6. Output = Input DIV 2

    7. ELSE

    8. Output = Input

    9. ENDIF

    10. Total = Total + output

    11.

    END WHILE12. Write(Total)

    # Input Output Total

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 8 OF 9

  • 8/3/2019 Revision - Algorithm Testing

    9/9

    Credit

    These activities have been put together from various Curriculum Council resources andpast assessment materials.

    SUBJECT: COMPUTER SCIENCE STAGE 2 & 3

    PAGE 9 OF 9