|
Let's go through an example of program logic development. The problem is simple and can be solved a number of ways. Here is the statement: Rodney is a tree snail who fell down a well of depth "H." Each day, Rodney crawls up a distance "U" and each night he slips back a distance "D." He persists, however, as he knows Rowena, a female tree snail, is waiting at the top of the well. For any given H, U, and D, how long will Rowena have to wait?
The first step in solving any problem is to eliminate the extraneous stuff and state all assumptions. For the Rodney/Rowena problem which of the following is extraneous?
That's right, "A" has no bearing on the solution to the problem. The second step is to determine what sort of mathematical steps are necessary to find the answer to the problem. In this case there are two possible ways of approaching the problem:
The iteration approach entails setting up a process that simulates what the snail is going through. Step one is to move the snail up "U" units and test to see if "H" has been reached. If "H" has not been reached, step two moves the snail down "D" units. Step three moves the snail up another "U" units and then the test for "H" is repeated. If the test fails, steps two and three are repeated over and over until successful. One might consider this approach the "brute force" approach. The calculation approach requires some thought about the problem. The time the snail spends moving up is the distance he has to travel divided by the net movement per day, or (H-U)/(U-D). The "H-U" term defines the point where the next day's movement will exactly equal "H" and the snail escapes. Note that you have to add one to account for the last day's movement. If there is any fraction, simply ignore it. The formula is: 1 + (H-U)/(U-D). If this is too complicated for you, simply use the "brute force" method. The third step is to go through a sample calculation with the particular solution technique you have found. We'll use the iteration method for illustration. Assume that H = 5, U = 3 and D = 2.
For comparison, using the prior formula: 1+(H-U)/(U-D) |
Please use the contact
form for questions or comments about this web site.
Copyright © 2001
Computer Knowledge, All Rights Reserved
Pray the Rosary for peace.