471,850 Members | 1,064 Online

could use help on a simple problem

I am taking a beginners class in programming and I am very lost. I need
to design a program that models the worm's behavior in the following
scenario: A worm is moving toward an apple. Each time it moves the worm
cuts the distance between itself and the apple by its own body length
until the worm is close enough to enter the apple. The worm can enter
the apple when it is within a body length of the apple. I now I need to
use an iteration control structure but have no idea where to start or
what to do in this one. Can some one help?

Aug 16 '06 #1
2 5629
"porky008" writes:
>I am taking a beginners class in programming and I am very lost. I need
to design a program that models the worm's behavior in the following
scenario: A worm is moving toward an apple. Each time it moves the worm
cuts the distance between itself and the apple by its own body length
until the worm is close enough to enter the apple. The worm can enter
the apple when it is within a body length of the apple. I now I need to
use an iteration control structure but have no idea where to start or
what to do in this one. Can some one help?
You could use either a for loop or a while loop here, but since you are
having problems in this area, a while loop is perhaps easier to understand.
Everything is out in plain sight and nothing "under the covers", you might
say. Besides its easier to write pseudocode for a while loop. How about
translating this to C?

while distance>= worm length
distance = distance - worm length

Where distance is the distance remaining to the apple.
Aug 16 '06 #2

porky008 wrote:
I am taking a beginners class in programming and I am very lost. I need
to design a program that models the worm's behavior in the following
scenario: A worm is moving toward an apple. Each time it moves the worm
cuts the distance between itself and the apple by its own body length
until the worm is close enough to enter the apple. The worm can enter
the apple when it is within a body length of the apple. I now I need to
use an iteration control structure but have no idea where to start or
what to do in this one. Can some one help?
Start by breaking your problem down into smaller problems, eg, write
down with pencil and paper "Move worm:"
position = position + 1

etc

Then translate it into the language you are using.

Aug 16 '06 #3