| CSC 161 | Grinnell College | Fall, 2013 |
| Imperative Problem Solving and Data Structures | ||
This laboratory exercise provides practice with procedures and parameters within C programs.
This lab has two parts:
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 count)
{
int i;
int reps = 3*count;
for (i = 0; i < reps; i++)
{
rForward (1, .5);
rBackward (1, .5);
}
sleep (3);
return reps;
} // yoyo
Explore using the yoyo procedure as follows:
Make an int variable, repetitions, set it to 2, and use it to call yoyo. What does calling the following call do?
int repetitions = 2;
int result = yoyo (repetitions);
printf ("repetitions = %d, result = %d\n", repetitions, result);
Does making this call twice change how many times the robot yoyos in each call?
Now replace the above code with the following variation:
int repetitions = 2;
repetitions = yoyo (repetitions);
printf ("repetitions = %d\n", repetitions);
Explain what happens.
Does making this call twice change how many times the robot yoyos in each call?
Explain the difference between the two calls?
Consider the nesting of calls yoyo in the following manner:
yoyo (yoyo (repetitions));
Make a prediction of what will happen with the above nested call.
Test out your nested call and explain what happened.
Look over amp-example.c and write a few sentences explaining what it does.
Within a C program, define and use the following functions:
Function circum that takes a circle's radius as parameter and returns the circumferences of the circle.
Function area that takes a radius as parameter and returns the area of the circle with that radius.
Write a new version of your solution to Step 6, so that the program has just one procedure circleCompute that has three parameters, the radius of a circle, the circumference, and the area. circleCompute has a void return type, but takes the radius as input and returns the circumference and area as changed parameters. (You will need to pass in the addresses of the circumference and area variables from your main procedure.)
Consider the program /home/walker/c/examples/lab2-1.c.
Consider the program /home/walker/c/examples/lab2-2.c.
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.