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


label

becomes

label: Answer = Answer - 1

...

If Catch > Int(Catch) Goto label

There is another necessary change. Rather like an athlete taking a step back before a jump, we must insert Answer = Answer + 1, because when the program is run, label begins by decrementing Answer. Structuralizing generally complicates the algorithm a little: remember that "There's no such thing as a free lunch", "Beauty is only achieved by suffering", and so on.

The second step in structuralizing (see item 3) is extracting the conditional jump from the body of the For loop. For this purpose, we introduce an auxiliary binary variable, Divided, indicating successful or unsuccessful division. Inside the loop, the conditional jump to label is replaced by an "alternative with one shoulder" (that is, Divided stores the result of the binary choice, carrying it to a single exit from the loop – one of the basic structural managing designs). The conditional jump 'slips' down and outside the loop.

After this (see item 4) the program can be modified with one more basic of structural design: a loop with checking on exit, which performs the same function as label and If ... Goto label in item 3. This finally enables the program to completely dispense with labels: the outer loop with exit checking, containing an inner For loop with its 'alternative with one shoulder'.

After all these manipulations, the fourth variant of the BASIC program is the one possible to copy to Mathcad (see Fig. 6.24). It's necessary to define it as a user function, as there are no Input and Print operators in the Mathcad language. Their analogues (the operators [ ]:= [ ] and [ ]=) work, alas, only outside of programs.


Содержание раздела