Physics 3730/6720 Example of Pseudocode

When developing a computer algorithm it is sometimes useful to begin with a detailed description in English before coding it in a structured language, such as C++. That way one may concentrate on the logic rather than on the syntax of the language. There is no special syntax for pseudocode in our course. The only rule is that it must be complete, and the logic clear and unambiguous.

Here is an example, taken from our second assignment. The problem is to find the roots of a quadratic equation.

Step 1   Prompt for and read the coefficients a, b, and c
Step 2   If a = 0, do steps 3,4
         Step 3  If b = 0 also, Error message: "no roots" and exit
         Step 4  Write single root -c/b and exit
Step 5   Set d = b^2 - 4*a*c and if d < 0 Error message: "not real" and exit
Step 6   Set s = -(b  + sqrt(d)*sign(b))/2
Step 7   Set x1 = s/a.  Write this first (robust) root
Step 8   Set x2 = c/s.  Write this second (robust) root.
Step 9   Set s =  -(b  - sqrt(d)*sign(b))/2.
Step 10  Set x1 = s/a.  Write this third (risky) root.
Step 11  Set x2 = c/s.  Write this fourth (risky) root.
Setp 12  Exit

Physics 3730/6720 Home Page
Physics Department Home Page

This page is maintained by:

Carleton DeTar Mail Form
Last modified 22 September 2002