成人快手

SequencingSequencing in practice: Drawing a square

When designing algorithms, it is important to make sure that all the steps are presented in the correct order. This is known as sequencing, and can be displayed in pseudocode or flowcharts.

Part of Computer ScienceAlgorithms

Sequencing in practice: Drawing a square

An to get a computer to draw a square on the screen might consist of these steps:

  1. draw a 3 cm line
  2. turn left 90 degrees
  3. draw a 3 cm line
  4. turn left 90 degrees
  5. draw a 3 cm line
  6. turn left 90 degrees
  7. draw a 3 cm line

The steps in this algorithm are in the correct . This algorithm would result in a perfect square:

Following a correct algorithm when completing a task such as instructions on how to draw a square will ensure that desired result is obtained.

If there was a mistake when designing the algorithm, and the steps in this sequence were placed like this:

  1. draw a 3 cm line
  2. turn left 90 degrees
  3. draw a 3 cm line
  4. turn left 90 degrees
  5. draw a 3 cm line
  6. draw a 3 cm line
  7. turn left 90 degrees

This algorithm would create this shape, rather than a perfect square:

Following an incorrect algorithm when completing a task such as instructions on how to draw a square will produce unexpected results.

Because step 6 is in the wrong sequence (it should switch with step 7), this algorithm failed. However, fixing the algorithm - and the square - is easy, because there are only seven steps to look through for the error.

Complex algorithms may have hundreds, if not thousands, of steps. It is critical to make sure all steps in the algorithm are in the correct sequence before programming begins. Once programmed, trying to find an instruction in the wrong sequence can be extremely difficult.