ENG 213.1 · Computer Programming for Engineers
Our notes
2 min readKinds of errors
| Kind | When found | Example |
|---|---|---|
| Syntax (compile-time) | by the compiler | missing end if, misspelt keyword, undeclared variable with implicit none |
| Run-time | while running | division by zero, square root of a negative, reading past end of file, array out of bounds |
| Logic | only by checking results | wrong formula, integer division, wrong units, wrong loop limits |
Logic errors are the most dangerous: the program runs and prints confident wrong numbers.
Techniques
- Read compiler messages from the first one. One missing bracket can cause many later messages.
- Test with known answers: hand calculations, textbook examples, simple cases (zero load gives zero stress).
- Print intermediate values (temporary print statements) to see where values go wrong.
- Trace (dry run) a small case by hand.
- Divide and conquer: test each procedure on its own.
- Check units and orders of magnitude: a beam stress of 10⁹ MPa is a units error.
- Use compiler checks while developing: with gfortran, -Wall (warnings), -fcheck=all (bounds and other run-time checks), -g (debug information).
- Use a debugger (such as gdb or an IDE): set breakpoints, step line by line, watch variables.
- 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
-
Debugging
(opens en.wikipedia.org in a new tab)
Wikipedia, Debugging, CC BY-SA 4.0 · licence
-
Software bug
(opens en.wikipedia.org in a new tab)
Wikipedia, Software bug, CC BY-SA 4.0 · licence
Watch
-
Best pick How to Use a Debugger - Debugger Tutorial
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
how to never write bug
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
Debugging JavaScript - Are you doing it wrong?
4 min · ≈ 18 MB · about ₦10
(opens youtube.com in a new tab)
-
Debugging in Python | Debugging Tips | Python Tutorials for Beginners #lec81
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
Debugging Like A Pro
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
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.