The goal of this lab is to introduce arrays as data structures and as parameters to functions.

An array is used to arrange data in memory. The cubbies just outside the dining hall represent a real-life example of this data structure; the fixed-size boxes are arrayed in a grid. The following steps illustrate the use of arrays within C.

Array Indices

  1. Copy the program, array-scale.c to your account.
    1. Explain what this program does.
    2. Near the end of the program, the index variable is set to 5. What happens if this index value is set to an inappropriate value? You may want to try reasonably small errors (e.g., index = 8, or index = -1) as well as ridiculous indices (e.g., index = 123456789). Explain the results that you get.
    3. Change the loop in the program so that it counts down, instead of up.

Multiple Arrays

  1. Program array-move.c makes the Scribbler move at specified speeds for certain times, using one array for speed and a second array for time.
    1. Explain how this program is similar to and different from the program array-scale.c from the previous question.
    2. Try changing the definition of NUM_MOVES, which tells the number of moves (it is now 8) to different numbers and see what happens.
      • Try a number smaller than 8.
      • Try a number greater than 8.

A Robot Journey

  1. One might describe a journey by the robot as having a series of steps. For each step,
    • the robot moves forward at a specified speed for a certain time
    • the robot turns at a given speed for a specified time

    As you know, speeds for the Scribbler 2 are numbers between -1.0 and 1.0 (or between 0.0 and 1.0, if the robot only moves forward). Also, speed parameters for either rTurnRight or rTurnLeft are between -1.0 and 1.0.

    Set up an appropriate set of four arrays to guide the robot through a journey of at least 10 steps. (The main loop should go through array values, first calling rForward and then one of the turn commands.) (See the MyroC.h header for details of these robot commands.)

Taking Pictures

The following notes outline the steps needed to utilize the Scribbler 2 to take a picture.

  1. Example program photographer.c illustrates the difference of how blocking and non-blocking commands work; it will take a picture, turn and show it to you, then repeat this twice more.

    1. First uncomment the first commented out part , then run the program and observe.
    2. Next, comment back out rDisplayPicture part and uncomment the second part of the code.
    What is the difference between the functionality for these two commands?
  2. Write a program that tells the Scribbler to take and store 6 photos by turning in a circle, and showing them in reverse order. Taking the pictures and showing them should be done with two separate loops.

Arrays and Functions

Consider the following declaration within a C program:

int notes[8] = {523, 587, 659, 698, 783, 880, 987, 1048};

C uses this declaration in at least two ways:

To clarify, the variable notes identifies the start of the the array. Thus, notes[0] specifies the first value in the block of memory, notes[1] specifies the second value in the block of memory, etc. This interpretation of the array has two consequences when using functions.

  1. Program max-min.c is a simple C program that computes the maximum, minimum, and average of an array of numbers; and program max-array.c computes the maximum of an array of numbers using a separate function. Copy both of these programs and be sure you know how they work.
    1. Initialization in find_max uses the statement max = array[0]. Why do you think this is done, rather than setting max to a large number (e.g., 1500) and starting the loop index at 0 rather than 1?
    2. Using max-array.c as an example, add separate functions, find_min and find_average, that compute the minimum and the average of the values in an array.
    3. Write an additional procedure find_stats that computes the maximum, minimum, and average values within one function. Since multiple values are to be computed and used by the main program, this function must pass values back using reference parameters. The function declaration should be:
      find_stats (double array[], int array_size,
                  double * p_max, double * p_min, double * p_average)
  1. In program max-array.c from Step 6, modify find_max so that it adds 3 to each array element after computing the maximum. That is, the loop in find_max should be changed to
    for (j = 1; j <array_size; j++)
      if (array[j] > max)
        max = array[j];
      array[j] += 3.0;
    Explain what happens when the array is printed in the main program and why.
  2. Robot Square Dancing

  3. Write a function, swings, with the following signature:
    swings (double speeds[], double durations[], int array_size)
    This function should have the following properties:
    • array_size 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.
    Be sure to test your function for correctness using at least four different durations and speeds.
  4. Copy the following function into your program, and make sure you understand what it does:
    divide_swings (double times[], int times_length)
      for (int i = 0; i < times_length; i++)
        times[i] /= 3;
    } // divide_swings
    1. Predict what will happen if you make the following calls in your main method, in this order:
      swings (speeds, times, num_moves); 
      divide_swings (times, num_moves);
      sleep (3); 
      swings (speeds, times, num_moves);
    2. Now test it out and explain what happened and why this is possible.