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

Laboratory Exercise on Functions

Robot Hoedown

Passing Primitive Variables

  1. Make a file named hoedown.c and set it up in the standard way.

  2. 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
          
  3. 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;
      yoyo (repetitions);
      printf ("repetitions = %d\n", repetitions);
                
      • Does making this call twice change how many times the robot yoyos in each call?

    • Now try the following variation of the above code:

      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?

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

Passing Arrays to Functions

  1. Write another 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. Write a program which uses the rGetObstacleAll() function and then displays each obstacle value. Then, your program should call a function which modifies each element in your obstacle array and turns it into the average of the three values. Then, your program should display the averaged obstacle values. All of your functions should be of void return type, so you will have to make your functions take arrays in by reference.

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.