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

# Laboratory Exercise on Arrays

### Goals

The goal of this lab is to introduce arrays to students.

### Preparation

Be sure you have read the notes on arrays before you start work on this lab.

### Introduction

An array is used to arrange data in memory. A real-life example to this kind of data structure can be the dining hall cubbies. There are boxes next to each with limited amounts of space in each.

### Array Indices

1. Copy the program, array-index.c. Write a paragraph explaining what this program does.
2. What happens when you enter inappropriate index values to the array `speed`? You may want to try reasonably small errors (say index = 8) as well as ridiculous indices (say index = 123456789). Explain the results that you get.

### Array Bounds

1. Moving in Different Speeds

The following program, array-move.c, makes the Scribbler move with certain speeds for certain times, which are in the corresponding indices of their arrays. Explain how this program is similar to and different from the program `array-index.c` from the previous question.

2. Change the loop in the program so that it counts down, instead of up.
3. 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.

### Musical Arrays

1. Play a Song with the Help of an Array

Copy the program fragment array-notes.c, and use it to write a program which plays the song with 0.5 second notes.

2. What is the resulting song?

### 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;`
• This is how you take and store a Picture:

`pic = rTakePicture();`
• This is how you show a Picture

`rShowPicture (pic);`

Note, however, that `rShowPicture` is a blocking call; that is, when you call `rShowPicture`, the picture is displayed, and subsequent processing is suspended until you close the picture's window.

• In contrast, this is how you show a Picture without blocking:
`rDisplayPicure()`

When you use `rDisplayPicture`, the picture is displayed, and subsequent processing continues.

Example program photographer.c illustrates the difference of how blocking and non-blocking commands work.

1. photographer.c will take a picture, turn then show it to you, then repeat this twice more. However you will need to uncomment the first commented out part , then run the program and observe. Next, comment back out rShowPicture 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 char and int, but it is for storing Scribbler pictures.

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

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