UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Debugging techniques

The kinds of errors, finding them with test data, tracing, print statements, compiler checks and a debugger.

Our notes

2 min read

Kinds of errors

KindWhen foundExample
Syntax (compile-time)by the compilermissing end if, misspelt keyword, undeclared variable with implicit none
Run-timewhile runningdivision by zero, square root of a negative, reading past end of file, array out of bounds
Logiconly by checking resultswrong formula, integer division, wrong units, wrong loop limits

Logic errors are the most dangerous: the program runs and prints confident wrong numbers.

Techniques

  1. Read compiler messages from the first one. One missing bracket can cause many later messages.
  2. Test with known answers: hand calculations, textbook examples, simple cases (zero load gives zero stress).
  3. Print intermediate values (temporary print statements) to see where values go wrong.
  4. Trace (dry run) a small case by hand.
  5. Divide and conquer: test each procedure on its own.
  6. Check units and orders of magnitude: a beam stress of 10⁹ MPa is a units error.
  7. Use compiler checks while developing: with gfortran, -Wall (warnings), -fcheck=all (bounds and other run-time checks), -g (debug information).
  8. Use a debugger (such as gdb or an IDE): set breakpoints, step line by line, watch variables.
  9. Rubber duck: explain the code line by line to someone (or something); errors often appear.

Worked example: find the bug

real :: d, t, g
g = 9.81
t = 3.0
d = (1/2) * g * t**2
print *, d
Prints 0.0. The bug: 1/2 is integer division = 0. Fix: d = 0.5 * g * t**2, which prints 44.145.

Worked example: off-by-one

A loop meant to average 10 readings is written do i = 1, 9. It averages 9 values but divides by 10: a result 10% low. Test with ten readings of 1.0: the answer should be 1.0 but prints 0.9.

Testing plan

Choose test data that covers: normal values, boundary values (exactly at a limit such as Re = 2300), zero, negative numbers and invalid input. Record expected and actual results.

Exams usually ask

  • Distinguish syntax, run-time and logic errors with examples.
  • Describe debugging techniques; find the error in a given program.

Common mistake

Changing code at random until the output "looks right". Find the cause first, then fix it, then re-run all the tests.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.