Skip to main content
Engineering LibreTexts

7.4: Test/Debug the Program

  • Page ID
    54270
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Once the program is written, testing should be performed to ensure that the program works. The testing will be based on the specific parameters of the program. In this example, each of the three possible values for the discriminant should be tested.

    C:\mydir> period
    Pendulum Period Calculation Program
    
    Enter Length and Angle values:
    120.0  15.0
    
     The Period is:    2.20801973
    C:\mydir>
    

    For this program, the results can be verified with a calculator. A series of different values should be used for testing. If the program does not work, the program comments provide a checklist of steps and can be used to help debug the program.

    Error Terminology

    In case the program does not work, it helps to understand some basic terminology about where or what the error might be.

    Compiler Error

    Compiler errors are generated when the program is compiled. This means that the compiler does not understand the instructions. The compiler will provide a list of errors with the line number of each error. It is recommended to address the errors from the top down. Resolving an error at the top can clear multiple errors further down.

    Typical compiler errors include misspelling a statement and/or omitting a variable declaration. For example, if the correct Fortran statement “write (*,*)” is entered incorrectly as “wrote (*,*)”, an error will be generated.

    In this case, the compiler error displayed will appear as follows:

    c:\mydir> gfortran -o period period.f95
    period.f95:13.1:
    
     wrote (*,*)
    1
    Error: Unclassifiable statement at (1)
    

    The first digit, 13 in this example, represents the line number where the error occurred. Using a text editor that displays line numbers, the statement that caused the error can be quickly found and corrected.

    If the declaration for the variable length is omitted, the error would appear as follows:

    c:\mydir> gfortran -o period period.f95
    period.f95:17.18:
    
     read (*,*) length, angle
                    1
    Error: Symbol 'length' at (1) has no IMPLICIT type
    

    In this case, the error is shown on line 18 (first digit after the “:”). However, the actual error is that the variable length is not declared. Each error should be reviewed and evaluated.

    Run-time Error

    A run-time error is something that causes the program to crash. For example, if a number is requested and a letter is entered, it will cause a run-time error.

    For example, the period program expects two real numbers to be entered. If the user enters letters, x and y, in this example, an error will be generated during the execution of the program as follows:

    c:\mydir> period
    Pendulum Period Calculation Program
    
    Enter Length and Angle values:
    x y
    At line 17 of file period.f95 (unit = 5, file = 'stdin')
    Fortran runtime error: Bad real number in item 1 of list input
    

    The program was expecting numeric values and letters were provided. Since letters are not meaningful in this context, it is an error and the program “crashes” or stops with an error message.

    Later chapters will provide additional information on how to deal with such errors. Until then, providing the correct data type will avoid this kind of error.

    Logic Error

    A logic error is when the program executes, but does not produce the correct result. For example, coding a provided formula incorrectly or attempting to compute the average of a series of numbers before calculating the sum would be considered a logic error.

    For example, the correct formula for the period of a pendulum is as follows:

    pperiod = 2.0 * pi * sqrt(length/gravity) * &
        ( 1.0 + 1.0/4.0 * sin(alpha/2.0)**2 ) 
    

    If the formula is typed incorrectly or incompletely as follows:

    pperiod = 2.0 * pi * sqrt(length/gravity) * &
        ( 1.0 + 1/4 * sin(alpha/2.0)**2 ) 
    

    The 1 over 4 is entered as “1/4” which are interpreted as integers. As integers, “1/4” results in 0. The compiler will accept this, perform the calculations, and provide an incorrect result.

    The program would compile and execute as follows.

    c:\mydir> period
     Pendulum Period Calculation Program
    
     Enter Length and Angle values:
    120.0 15.0
     The period is:   2.19865513
    

    However, an incorrect answer would be generated as shown. This is why testing the program is required. Logic errors can be the most difficult to find.

    One of the best ways to handle logic errors is to avoid them by careful development of the algorithm and writing the code.

    If the program has a logic error, one way to find the error is to display intermediate values. Further information will be provided in later chapters regarding advice on finding logic errors.


    This page titled 7.4: Test/Debug the Program is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?