Fig. 6.23. BASIC programs for solving the Fishermen's Problem - часть 2



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.

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