MathCAD



Fig. 6.23. BASIC programs for solving the Fishermen's Problem


Fig. 6.23 shows four BASIC programs to automate the solution of the Fishermen's Problem. In all four, the operator Input requests the first guess (50 fish, for example) and the operator Print gives out the answer: 25 fish.

The first program follows exactly our 'manual' algorithm. Inside the loop for the three fishermen's successive leavings, we watch until we find fractional 'tails': that is, the parameter Catch is not an integer (the built-in BASIC function Int chops off these tails; the Mathcad analogue is the floor

function). Then the guess decrements by one (Answer = Answer - 1) and the program control passes to the labelled statement (Goto label).

Before this program can be translated to Mathcad's programming language, we need to get rid of the label. And not only because in Mathcad's programming arsenal there is no facility to jump to a label, either conditionally or unconditionally, but for other reasons unconnected with Mathcad. In our 'toy' program (item 1 in a Fig. 6.23) the label is quite pertinent and natural, but if a program with labels expands, it becomes difficult to understand and practically impossible to debug and develop. The result, as structural programming wizards rightly emphasise, becomes like spaghetti: try pulling out a block of statements for separate debugging or compilation, and you find adhering strings of Goto links. Besides, such program can't be created by a group of developers ('bottom-up' technology). The first implementations of the Pascal language had no labels at all, as Pascal was developed by Niklaus Wirth for training students in structural programming. The label has appeared only in the late versions. So I think our view on learner programmers and the label must be, "Hide matches from children"!

The first step in structuralizing the BASIC program (see item 2 in Fig. 6.23) is moving the decrement code from the conditional statement to the label. That is:

If Catch > Int (Catch) Then Answer = Answer - 1: Goto




Содержание  Назад  Вперед