# Mini-Lab: Keep On Moving

Using For Loops

This set of Mini-Lab Exercises is the third in a series in which students build a small program with several fish moving around in an aquarium. The set includes the following exercises:

Each section contains an Introduction to a problem or task, (usually) abridged versions of one or more Patterns that will be useful in solving the problem or completing the task, and an Exercise.

In the exercises that precede this one, students will have created three fish, moved them forward one space (changing the fish's direction when it is about to hit a wall), and displayed them graphically. Students should be familiar with constructing objects, invoking methods, and using conditional statements and logical expressions.

Students should read over the patterns that appear in this document before the lab.

## Keep on Moving

### Introduction

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.

### Pattern: Counted Repetition (Repetition) -- Abridged

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.

WARNING: It is important to note that your loop will have an off-by-one boundary error if you mix the two idioms. If, for example, you write
``````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
`(i = 0; i < N; i++)`
pattern for counting as well as for accessing indexable structures.

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 Forward

Modify 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.

## Put the User in the Driver's Seat

### Introduction

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.

### Pattern: Prompted Input

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 Steps

Modify 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?)

## Random Behavior

### Introduction

Our three fish are moving in lock-step with one another, more like a marching band than fish. It would be more interesting if, in each time step, a fish randomly decides whether to change direction before moving forward (unless it is at a wall, in which case it must change direction). We can use the RandGen class to simulate a coin toss (or other random selection between two values) by specifying that we want a random integer in the range 0 to 1.

#### Exercise

Research 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.

#### Stop and Think

Does it matter which expression comes first in your complex condition?