3GL Step-Form Algorithm - Exercise 1  Your first step should be the investigation step:
  1. Identify the processes
  2. Identify the major decisions
  3. Identify the loops
  4. Identify the variables
What processes are implied in the problem? At the first attempt I came up with: What decisions need to be made? What loops are implied in the problem? What variables are implied? The next step is the preliminary step:
  1. Devise a "high level" algorithm
  2. Step through the algorithm. Does this "walk-through" reveal any major problems? If it does correct the problems.
Here is my first attempt:
  1. Get book
  2. Find first page
  3. Go to end of page
  4. Increment counter
  5. If not last page then go to step 2
If you walk through this attempt you can see that it is a bit clumsy. The last step states that if we are not at the end of the book then go back to step 2 so that the page counting can continue. But step 2 points us at the first page and what we really want to do is to get to the next page and count that.

The third step in the strategy we are using is the refinement step:

  1. Incorporate any refinements indicated in step 2.
  2. Group together processes where appropriate
  3. Group together variables where appropriate
  4. Test the algorithm again by stepping through it
Here is a simple change as a first refinement:
  1. Get book
  2. Find next page
  3. Go to end of page
  4. Increment counter
  5. If not last page then go to step 2
Now when step 5 terminates we automatically get the next page. This algorithm implies that at the start of the algorithm the book is open just before the first page but it is not very clear. What about making this a bit clearer?
  1. Get book
  2. Open at first page
  3. Go to end of page
  4. Increment counter
  5. Find next page
  6. If not last page then go to step 3
It is better but does it have any problems? What happens if in step 6, after doing a Find next page step the page we have is the last page?

The last page won't be counted. Perhaps we could fix it this way:

  1. Get book
  2. Open at first page
  3. Go to end of page
  4. Increment counter
  5. Find next page
  6. If not last page then go to step 3
  7. Increment counter
It seems OK but what if we have a book which has no pages? or a book which has only 1 page?

Here is another attempt!

  1. Get book
  2. Open book
  3. Find next page
  4. If there is no next page go to step 7
  5. Increment count
  6. Go to step 3
  7. Finish

Return to lesson


and may not be reproduced by any means without the written
 

 
 
 
 

 

This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia


db@codelearn.com