MINI-LAB: KEEP ON MOVING
Using For Loops
This set of Mini-Lab Exercises includes the following:
Our program would be more interesting if we had the fish move more than just the one time. We can use the Counted Repetition pattern to allow the fish to move for several time slices.
You are in a situation in which some action should be repeated a known number (N) of times, where N might be a constant number or a value in a variable. The number of repetitions does not depend on any aspect of the repeated action, i.e., you know the value of N before you start the repetition.
Therefore, use a FOR statement that creates a variable to count the number of times the action has been repeated. The counting variable is created and initialized and then, so long as the action has not been repeated too many times (the "test"), the action is done and the count is incremented (the "step"). Express the test as a Positive Condition if possible. The structure of a FOR loop is:
for (<initialization> ; <condition> ; <step>) <repeated action>
There are two common idioms for controlling the loop. The first is to count from 0 to N-1, which is the idiom used to ensure a Valid Index in a Linear Indexed Traversal (see the Repetition pattern for detailed information on Linear Indexed Traversal). For example, at the beginning of a card game each player is dealt a hand of cards:
for (int i = 0; i < nbrPlayers; i++)
{ dealHand();
}
The second common idiom for controlling the loop is to count from 1 to N. For example,
for (int i = 1; i <= nbrPlayers; i++)
{ dealHand();
}
You might choose to use the second alternative because it corresponds more closely with counting. On the other hand, the Linear Indexed Traversal pattern is a very common, and important, pattern because it is used to step through one of the most common data structures.
for (int i = 0; i <= nbrPlayers; i++) INCORRECT!
{ dealHand();
}
your action will be repeated nbrPlayers+1 times, not nbrPlayers times. If,
on the other hand, you write
for (int i = 1; i < nbrPlayers; i++) INCORRECT!
{ dealHand();
}
your action will be repeated nbrPlayers-1 times.
To avoid these kinds
of mistakes many people find it easiest to consistently use the
Linear Indexed
Traversal
Another alternative is to count down rather than counting up. This alternative and other types of Repetition are described in the complete Repetition Pattern.
Exercise: Simulate Fish Moving ForwardModify your main program to become a simulation of three fish moving in the aquarium over time. Initially, set the number of time steps in the simulation to 10. Choose one of the loop control idioms above and use it correctly. |
Our program would be more flexible if we allowed the user to specify how many times they want the simulation to run. We can use a Prompted Input to ask the user to provide the desired number.
In the Prompted Input pattern, we print a prompt that describes the type of input required from the user and then read in the desired value. A well-written and robust version of this pattern verifies that the input actually provided by the user matches the expectations of the program. For example, if we prompt the user to provide an integer, we should verify that the input is, in fact, a valid integer and not a string, floating point number, or other type. We will return to this full-fledged version of Prompted Input when we discuss writing functions.
In the meantime, we will use a simpler, weaker version of Prompted Input that does not verify the user's input. The structure of this Prompted Input pattern is:
cout << "String to prompt user: "; cin >> inputVariable;The prompt may be on its own line, in which case the cout statement will end with an endl or \n new-line escape sequence. Alternatively, you may wish the user to enter their data on the same line as the prompt, in which case the prompt should end with one or more spaces.
For example,
int numPlayers; cout << "How many players will participate? " cin >> numPlayers;
Exercise: Allow User to Control Simulation StepsModify your program to ask the user how many times they want the simulation to run, and then use that number to control your loop. (Should this prompt appear before or after your message welcoming the user to the aquarium program?) |
ExerciseResearch the RandGen interface to discover how to randomly generate a 0 or 1. Modify your simulation function to have each fish randomly decide whether to change direction before moving forward (unless it is facing a wall, in which case it should always change direction before moving forward).In a previous exercise you wrote the selection statement to change direction when a fish is facing a wall. You do not need to add any new selection statements to your code in order to implement the random behavior for this exercise. Just modify your selection statement to test for a more complex condition. Don't forget to include the header file for RandGen at the top of your file.
|