CSC 161 Grinnell College Fall, 2011 Imperative Problem Solving and Data Structures

# Laboratory Exercise on Arrays

### Goals

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

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

### Array Order

1. Making a Reverse Movie

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 seperate loops. Below are some things you need to know in order to write this program:

• 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 can use `rDisplayPicture`, the picture is displayed, and subseqent processing continues.
• You can make an array of Picture just like any other variable type.

## Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/161.fa11/modules/module-arrays-functions/arraylab.shtml
```