Although many general things can be said about problem solving, the focus of this book is on the programming of computers so as to employ them as tools in the process. Of course, as they are manufactured, computing machines have only very limited capabilities. Computers do not pose problems, analyse them, or find solutions. They only run the programs that embody the solutions discovered by some human programmer, and for this, the hardware alone is insufficient. The addition of a programming notation (or language) such as Modula-2 produces a system capable of using the programmer's instructions to solve problems. All the intelligent planning to take use of this system once it has been assembled is entirely a human responsibility.
Computers can do only a few simple things, but they do them very quickly, so it is possible to employ them for rather sophisticated tasks. The programmer must set the task for the computer to perform with each step carefully detailed in some such notation--the complete sequence of which steps is termed a program. All the work this requires is worthwhile because the machine can execute the steps so quickly that one is still ahead of the game--despite the many hours it may take to plan and write down the necessary instructions.
It cannot be over-emphasized that the key to success in such an endeavour is careful planning. The problem to be solved or the task to be performed must be completely analyzed and fully understood by the programmer before any instructions to the machine can be written. What is the best method for doing this? There are two strategies for approaching the solution of a problem, however complex it may be.
The first is to focus on the details, tackle each one separately, then gradually weld the partial answers into a coherent whole that provides a solution to the whole problem. This is known as a "bottom up" design, and there are times when variations of this method can be quite useful.
The second is to take an overview of problem as a whole, systematically break it into its parts, with a well-defined notion at each stage as to how the solution to the parts will fit into the whole. The actual solution is built by erecting the foundation and framework first and filling in the details later. From the general overview, one sketches what has to be done in broad terms, then refines the solution a little at a time until it is sufficiently detailed to write the steps in the chosen programming notation.
The process of solving a problem by breaking it down into successively smaller steps whose interrelationship is clearly defined and then solving each of the steps, is known as the top down method of design.
Modula-2 is an excellent language for this kind of structured approach to problem solving, for its facilities can be used to achieve this designed approach in a natural way.
For this technique to work, however, it is essential to clearly determine at the planning stage how the parts will fit together in the final program. The lines of communication between the tasks that each part of the solution is to accomplish must be clearly spelled out in advance. During the construction of the individual parts of a program, each one of them can be broken down in the same manner. Only when the problem at hand (the whole project or a selected portion of it) has been reduced to a series of very detailed steps should the actual code-writing begin. At this point, there is (ideally) a one-to-one correspondence between the steps of the solution and the commands available in the programming notation being used. Rendering the solution in a correct program is then a simple matter.
An even more sophisticated programmer will use the top-down method for general design purposes, but will occasionally set aside this work to tackle some small or more obvious part of the solution in bottom-up style. It is not easy to give definite rules for when this can be done. Professional programmers have considerably more independence to make such a decision for themselves than do apprentices, because there is more about the practice of problem solving that is obvious or intuitive to them. Most of the problems in this text will be solved in top-down style; the places where a combination top-down/bottom-up style is used will be noted.