Skip to main content

Section 13.2 How to write algorithms people (or computers) can actually follow

Part of learning how to be a professional engineer is learning how to do things efficiently, in a way that is repeatable by others. It is important that if you solve a unique problem, that others don’t need to solve it all over again.
Now, if I wanted to describe to someone my solution to the 8 AM attendance problem, I could write it out as I did above (see Professor’s Solution). However, there are some language flourishes that are unnecessary, take up space, and do not contribute to your understanding of how I arrive at the solution. If we strip those away, it then becomes clear that there are a lot of details that are missing. For example, if you wanted to copy my solution exactly, you may wonder what types of clothes I wear and when I wear them. Also, you may wonder if I ride my bike to work in all weather conditions. Do I ever drive? Etc.
For your algorithms to be usable by other people, the algorithm needs to be as specific as possible. You need to write algorithms down in such a way that a computer (sometimes, but less often, a human) can follow the steps to complete the solution. It is important to write your algorithms so that nothing is implied! To make sure I am writing my algorithms appropriately, I pretend that I am writing them for a really, really, dumb person. Even though we say things with computers are “smart” (i.e. smartphones), in reality, computers are shockingly dumb. They are just really fast which we can confuse as smart. For now, when crafting algorithms, remember to be as specific as you possibly can.
It is important to write your algorithms so that nothing is implied!
Let’s break down the Professor’s Solution to the problem as an ordered list of instructions that should be followed step-by-step:
Professor’s Solution- Algorithm
Below, is the Professor’s Solution to the 8 AM attendance problem.
Instructions to ensure success when following the algorithm that solves the 8 AM attendance problem:
  • Steps should be read from the top to bottom (line numbers are displayed in yellow to the left)
  • Follow the instructions on each line to their logical conclusion
  • Either continue one line down OR follow the instruction on line designating skip location
Figure 13.2.1. Professor’s solution to the 8 AM problem.
Let’s take a look at line 4:
If forecast >= 70 continue, else go to line 7
Translated from Algorithm-ese into English, that is equivalent to saying:
If the temperature is forecast to be greater than or equal to 70 degrees F, continue to line 8 which tells you to wear a t-shirt and shorts. Or else you should consider other clothing options.
Let’s say the forecast indicates it will be 61 degrees today. Therefore when I evaluate my algorithm, the statement If forecast >= 70 continue, evaluates to false. It isn’t true, so I shouldn’t follow the instruction. This is where the algorithm following rule. Follow the instructions on each line to their logical conclusion comes into play. Since it is not true that it is 61 degrees, I should follow the else go to line 7 bit. So I skip to line 7 and am on my merry way.

Checkpoint 13.2.2. What Should You Wear?

Bugs
Notice how the steps are written in such a way, that you can not mess them up if you follow the instructions. If you start from the top and follow the instructions, you can’t both ride the bike AND drive the car at the same time. You will not be instructed to wear both a t-shirt and a long sleeve, etc. You may think the way of writing go to line 7 is unnecessary because you are smart and can understand that some things are implied. However, it is crucial to ensure that someone following the algorithm does not wear multiple items of clothing if they are dumb.
A bug in your algorithm is an erroneous instruction(s) included in your steps that causes your algorithm to behave in a way you didn’t anticipate
Figure 13.2.3. Always check for bugs in your code.
Remember, it might be useful to you to think about writing algorithms for a very, very, very dumb person. For example, consider the following Professor’s Solution - Algorithm with the following, small, yet critical change (figure 13.2.4 below). Now, read it and pretend that you are exceptionally dumb and just follow the instructions blindly. Furthermore, let’s say that the forecast is 61 degrees F. Before continuing reading the text, look at the picture below and see if you can find the bug.
Figure 13.2.4. Can you find the bug?
The dumb person MIGHT start at the top of the list, notice that the forecast is NOT greater than or equal to 70, deduce that the tabs imply that you should only follow the instructions if the preceding statement is true, then skip to line 6 (doubtful, but let’s say that this particular dumb person is having a good day). When the dumb person gets to line 6, they notice that hey, If forecast >= 50 is true so they continue and put on a long sleeve shirt and jeans. When they get to line 8, they notice that If forecast >= 30 is ALSO true! So they put on a jacket and jeans over the long sleeve shirt and jeans they already have on. That is the bug! We didn’t intend for someone to remember that an algorithm is a step-by-step process that should be written with nothing implied. Wear two pairs of jeans or two tops!

Checkpoint 13.2.5. Finding the Bug.

For the next question, consider the following algorithm for determining where to go out to eat with a boyfriend or girlfriend.
Find the bug in the algorithm and select the line number that contains the error.
  • Line 1
  • Incorrect. The bug is not on line 1.
  • Line 3
  • Incorrect. The bug is not on line 3.
  • Line 5
  • Incorrect. The bug is not on line 5.
  • Line 7
  • Correct. The bug is located on line 7.
  • Line 9
  • Incorrect. The bug is not on line 9.
You have attempted of activities on this page.