Part 1a
Start by reading in the four parameters, and printing them to the screen.
Some students find the
while
loop easier to deal with than the
for
loop. So you can start by performing the iterates using a
while
loop. After you get this working, then redo it with a
for
loop.
Part 1b
Review the
printf
formatting options (especially King p. 34) to allocate 4 spaces for printing each integer.
Use a variable, say
i
to count the number of iterations. Print a newline character if
i
is a multiple of 20. Use the
%
operator to do this.
If you are using DOS, we recommend testing your program out by printing 15 integers per line instead of 20 so you aren't sandbagged by the fact that some DOS prompt's auto line-wrap after 80 columns.
Part 2
Write the code for finding a point on the cycle. Consider using a
do-while
loop. You will want to maintain two variables, one for the slow generator (say
x
as before), and one for the fast generator (say
xx
).
Once you have a point on the cycle, think about how to compute its length. You will need to use one additional variable to keep track of the length. Don't neglect to give it an appropriate name.
Warning: the length of the cycle is not necessarily the same as the number of iterates until the fast and slow generators arrive at the same point. As described in the assignment, you need to go around the cycle one more time to compute its length.
Kevin Wayne