Laboratory Exercise on Arrays
Goals
This lab provides practice with arrays as data structures and as parameters to functions.
Work Started in Class
An array is used to arrange data in memory. A real-life example to this kind of data structure lockers in a locker room or storage rooms within a long-term storage facility. There are boxes next to each with limited amounts of space in each. The following exercises illustrate the use of arrays within C.
Array Indices
-
Program max-min.c is discussed in today's reading on arrays. The program computes the maximum, minimum, and average of an array of numbers and also prints values in main memory in or near an array.
- Copy the program max-min.c and compile and run it. Provide your own explanation of what the program does.
- Change #define n 10 to #define n 12, but do not change any other parts of the program. Compile and run the program, and explain what happens.
- Change #define n 10 to #define n 7, but do not change any other parts of the program. Again, try to compile and run the program, and explain what happens.
Consider the following declaration within the C program array-scale.c
int notes[8] = {523, 587, 659, 698, 783, 880, 987, 1048};
C uses this declaration in at least two ways:
- The machine allocates space for 8 int values within main memory..
- The identifier notes is used to reference the location of the first element in main memory.
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.
- Use of the variable notes specifies an address to a function. For arrays, all parameter passage is by reference, as the address is given. (Practically, this means that the machine only has to pass the reference to the array, not copy the entire array, when passing arrays to functions.)
- The variable notes specifies a starting address, but not an ending address or a size for the array. If one wants to use an array in processing, one must keep track of size separately.
-
Copy the program, array-scale.c to your account.
- Write a paragraph explaining what this program does.
- 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.
Multiple Arrays
-
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.
-
Explain how this program is similar to and different from the
program
array-scale.c
from the previous question. - Change the loop in the program so that it counts down, instead of up.
-
Try changing the initialization
numMoves
, 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.
-
Explain how this program is similar to and different from the
program
A Robot Journey
-
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 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.
-
This is how you declare a Picture:
Picture pic;
-
The Picture type is defined in the MyroC.h header.
-
This is how you take and store a Picture:
pic = rTakePicture();
-
This is how you display a Picture
rDisplayPicture (&pic, duration, window-name);
Notes:
-
The first parameter to
rDisplayPicture
is the address of a Picture, not the Picture variable itself.- As you might expect, a Picture contains a substantial amount of information — for example, color information for each element or pixel in the Picture. If the Picture itself were passed to the rDisplayPicture, then all of image data would have to be copied into the function's parameter when the function was called.
- For rDisplayPicture, however, only the address of the variable pic is transmitted, and rDisplayPicture can work directly with the original picture, not a copy.
- Since rDisplayPicture does not change the picture, this function is reasonably safe — there is little advantage in making a copy of the original image.
-
rDisplayPicture
has both blocking and non-blocking functionality, which is dependent on the duration. Recall that a blocking call stops the program from continuing, and a non-blocking call allows for the program to keep going. So with a non-blocking call, you could have a photo displayed and have the robot execute new commands.- If duration is positive, then it is a blocking call.
- If duration is negative, then it is a non-blocking call.
- duration 0, then non-blocking and non-terminating (you will have to close the window manually).
-
Here,
-
pic is the variable of type Picture described above, so &pic is the address of a Picture.
-
duration is a double that indicates how long the picture should be displayed.
-
window-name is a string of characters. In C, this often is given as a sequence of characters within double quotes.
-
Putting these pieces together, a typical call to rDisplayPicture might be
rDisplayPicture (&pic, 2.5, "picture from robot");
Example program photographer.c illustrates the difference of how blocking and non-blocking commands work.
-
photographer.c will take a picture, turn then show it to you, then repeat this twice more.
- First uncomment the first commented out part , then run the program and observe.
- Next, comment back out rDisplayPicture part and uncomment the second part of the code.
What is the difference of the functions for these two commands?
Making a Reverse Movie
You can make an array of Pictures just like any other variable type. This is because Picture is a "type" just like double and int, but it is for storing Scribbler pictures.
-
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
-
Program max-array.c computes the maximum of an array of numbers using a separate function. Copy this program and be sure you know how they work.
- Initialization in find_max uses the statement max = arr[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?
- Using max-array.c as an example, add separate functions, find_min and find_avg, that compute the minimum and the average of the values in an array.
-
Write an additional procedure find_max_min_avg 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 procedure
signature should be:
void find_max_min_avg (double arr [], int sizeOfArray, double * max, double * min, double * avg);
Homework
Robot Hoedown
-
In program max-array.c from Step 7, 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 < sizeOfArray; j++) { if (arr[j] > max) max = arr[j]; arr[j] += 3.0; }
Explain what happens when the array is printed in the main program and why.
-
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.
Be sure to test your function for correctness. It is suggested that you 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_swings
-
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);
-
Now test it out and explain what happened and why this is possible.
-
created 19 July 2011 by Dilan Ustek revised 18 September 2011 by Henry Walker revised 15 October 2011 by Dilan Ustek modest editing 16 October 2011 by Henry Walker minor editing 22 October 2013 by Henry Walker reorganized with module 26 January 204 by Henry Walker readings added 19 September 2014 by Henry M. Walker reformatted, expanded, and connected to new readings 9 August 2016 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |