CSC 161 Grinnell College Spring, 2013 Imperative Problem Solving and Data Structures

# Laboratory Exercise on Functions with Array and Function Parameters

## Robot Hoedown

### Passing Arrays to Functions

1. Write a function, swings, with the following signature:

```void swing (double speeds[], double durations[], int sizeOfArrays)
```

This function should have the following properties:

• sizeOfArrays identifies the number of elements stored in the speeds array and in the durations array.

• For each speeds[i], durations[i] pair, the Scribbler should turn with the associated speed and duration

• If the current index of the command is an even number, it should turn right

• If the current index of the command is an odd number, it should turn left

• Hint: Consider using the % operator to determine whether the index is even or odd.

2. Now test out your function to make sure it works. Be sure to use at least four different durations and speeds.

3. Copy the following function into your program, and make sure you understand what it does:

```void
divide_swings (double times[], int args)
{
int i;

for (i = 0; i < args; i++)
times[i] /= 3;
} // divide_swings
```
4. Predict what will happen if you make the following calls in your main method, in this order:

```swing (speeds, times, num_moves);
divide_swings (times, num_moves);
sleep (3);
swing (speeds, times, num_moves);
```
5. Now test it out and explain what happened and why this is possible.

### More Passing of Arrays

1. MyroC's rGetObstacleAll() function queries the three obstacle sensors within the Scribbler 2 robot and collects the results in an array, the parameter int irsensors[3].

Write a program which calls the rGetObstacleAll() function and uses the results as follows:

• Call rGetObstacleAll() to obtain the three sensor values in a single array.
• Display each obstacle value.
• Call a function which modifies each element in your obstacle array and turns it into the average of the three values.
• Display the averaged obstacle values.

All of your functions should be of void return type, so you will have to make your functions take utilize array parameters appropriately.

### Function Parameters

1. Write a function with the following signature:

```void boxMove (void movement (double, double))
```

The function should move the Scribbler 2 robot 5 times using the following sequence:

• move forward for 1 second
• call the movement function with values 0.6, 1.0.

Write a program that calls boxMove using the MyroC rTurnLeft function as parameter and then call boxMove again using rTurnRight.

Why do you think the name boxMove was chosen? Is there a better name?

### Arrays of Functions

1. Write 3 functions that cause the Scribbler 2 robot to react in different ways (e.g., move/turn, beep, spin).

Write a program that uses these three functions as follows.

• Within main, make an array that reference each of these robots.
• Execute these functions by having a loop that calls each function in turn by accessing an array element.
• Use a random number generator to select and execute a function randomly through 5 iterations. (That is, for 5 iterations, the program will select a function at random and move the robot accordingly.)

## Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.