Step by Step Algorithms


Straight-line algorithms

The simplest kind of algorithm, called a straight-line algorithm, is just a sequence of one or more steps, to be done one after the other. Each step is a statement, or command. Straight-line algorithms are like instructions for assembling a piece of furniture by following a fixed sequence of steps.


Case study: avoiding recomputation

Suppose that you want to compute (12345.0/9.1)2, and call the result z. A direct way to do it is to write

  Let z = (12345.0/9.1) * (12345.0/9.1).
But that requires you to write the same thing, (12345.0/9.1), twice, and also requires the program to compute it twice. A better way to do that is to use two steps. First, you give a name to (12345.0/9.1), and then you multiply that named value by itself.
  Let w = 12345.0 / 9.1.
  Let z = w*w.
The value of w is only computed once and remembered.


Case study: Using several steps to aid readability

Sometimes you break an algorithm down into a few steps not only to avoid computing the same thing more than once but also to make the program easier to read and write. Here is an example.

Quadratic equation ax2 + bx + c = 0 can have two solutions, given by

x = (-b ± sqrt(b2 - 4ac))/(2a)
Suppose that you want to find both solutions for the case where a = 20.0, b = -50.0 and c = 1.0. The following straight-line algorithm computes both solutions and calls the two answers x1 and x2.
  Let a = 20.0.
  Let b = -50.0.
  Let c = 1.0.
  Let d = sqrt(b*b - 4*a*c).
  Let twoA = 2*a.
  Let x1 = (-b + d)/twoA.
  Let x2 = (-b - d)/twoA.


Hand simulation

You would hope that the people at the furniture factory had somebody try out the assembly instructions to see whether they really work and make sense. You want to do a similar thing for an algorithm or program. Trying out a program by hand is called a hand simulation of the program.

Let's check out the program that solves a quadratic equation. But the numbers a = 20, b = -50 and c = 1 are awkward to compute with (for a person). So instead, let's try it with simpler numbers. We know from algebra that expression (x - 2)(x - 3) is equal to 0 just when x = 2 or x = 3. Since (x - 2)(x - 3) = x2 - 5x + 6, it should be very simple to solve equation x2 - 5x + 6 = 0. So let's just modify the program by putting in new values for a, b and c. (Notice how helpful it is that their definitions have been made. The job would be much harder if the numbers 20, -50 and 1 had been used in their places throughout.)

  Let a = 1.0.
  Let b = -5.0.
  Let c = 6.0.
  Let d = sqrt(b*b - 4*a*c).
  Let twoA = 2*a.
  Let x1 = (-b + d)/twoA.
  Let x2 = (-b - d)/twoA.
To do a hand simulation, just perform the steps, showing the value of each thing.
   a = 1
   b = -5
   c = 6
   d = sqrt(5*5 - 4*6 = sqrt(25 - 24) = 1
   twoA = 2
   x1 = (-(-5) + 1)/2 = 6/2 = 3
   x2 = (-(-5) - 1)/2 = 4/2 = 2
As you can see, the results appear to be correct. That gives you confidence that they will also be correct when you put in the desired values a = 20, b = -50 and c = 1.


Problems

  1. [solve] Write a straight-line program that defines two real numbers a = 7.0 and b = -1.0, then defines solution to be the solution for x to equation ax + b = 0.

    Write your answer in the box. When it is ready, push the Compute button to see whether it works.

  2. [solve] Modify your answer to the preceding question so that a = 10.0 and b = -5.0. You should only need to change the definitions of a and b.

  3. [solve] Suppose that triangle A has corner points p1 = (x1, y1), p2 = (x2, y2) and p3 = (x3, y3). Then the area of triangle A is |d1 + d2 + d3|/2 where

    d1 = x1y2 - x2y1,
    d2 = x2y3 - x3y2
    d3 = x3y1 - x1y3.

    Write a straight-line program that defines x1 = 0.0, y1 = 0.0, x2 = 4.0, y2 = 6.0, x3 = 8.0 and y3 = 0.0, defines d1, d2 and d3, then defines area to be the area of the triangle. Use names x1 for x1, y1 for y1, etc. Remember that abs(x) is the absolute value of x.

  4. [solve] Choose different coordinates for the corners of a triangle. Modify your answer to the preceding question so that it gives you the area of the triangle with your new coordinates.


Summary

A straight-line algorithm is just a sequence of steps. The steps can be Let-statements. Later we will see other kinds of statements, which can also be used as steps in a program.

One use of a name is to avoid recomputation of the same thing several times.

Perform a hand simulation to check that an algorithm is working correctly.


Review

A name can contains letters and digits, provided it starts with a letter. A name cannot have any spaces in it. In Cinnameg, it is important to choose names that start with lower case letters.

A statement is a command. It tells the program to do something.

An algorithm is a clear and unambiguous set of instructions for solving a certain kind of problem.