| CSC 161 | Grinnell College | Fall, 2011 |
| Imperative Problem Solving and Data Structures | ||
Make a file named hoedown.c and set it up in the standard way.
Copy the following function into your program and describe what it does:
int
yoyo (int reps)
{
int i;
for (i = 0; i < reps; i++)
{
rForward (1, .5);
rBackward (1, .5);
}
sleep (3);
reps *= 3;
return reps;
} // yoyo
Make an int variable, repetitions, and set it to 2.
What does calling the following call do?
yoyo (repetitions);
Does making this call twice change how many times the robot yoyos in each call?
What does the following call do?
repetitions = yoyo (repetitions);
Does making this call twice change how many times the robot yoyos in each call?
What is the difference between the two calls?
Make a prediction of what will happen if you nest calls to yoyo in the following manner:
yoyo (yoyo (repetitions));
Test out your nested call and explain what happened.
Write another function, swings, with the following signature:
void swing (double speeds[], double durations[], int args)
This function should have the following properties:
The Scribbler should turn for each argument 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.
Now test out your function to make sure it works. Be sure to use at least four different durations and speeds.
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_swingsPredict 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);Now test it out and explain what happened and why this is possible.
When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.