CS Behind the Curtain (CS195 2003S)

Lab: Arrays, Revisited

Summary: In today's laboratory, we explore different issues pertaining to arrays.

Contents:

Exercises

Exercise 1: Initializing Multi-Dimensional Arrays

Here is a sample initialization of a one-dimensional array of integers.

int ant[5] = { 5, 2, 7, 3, 4 };

Figure out how to initialize the two-dimensional array bat so that the first row contains 8, 16, and 32 and the second row contains 5, 7, and 9.

int bat[2][3] = figure-this-out;

Exercise 2: Changing Dimensionality

Suppose we've declared bat as above and cow and i as follows:

int *cow;
int i;

a. What do you expect the effect of the following code to be?

  cow = (int *) bat;
  for (i = 0; i < 6; i++) {
    printf("cow[%d]: %d\n", i, cow[i]);
  } /* for */

b. Verify your results experimentally.

Exercise 3: Three-Dimensional Arrays

Consider the following declaration of a three-dimensional array:

int chinchilla[2][3][4];

a. How many elements does chinchilla have?

b. Can one initialize chincilla while declaring it?

c. Where in memory is chinchilla[i][j][k]?

d. How would you verify your previous answer?

Exercise 4: Binary Search

Implement iterative binary search of an array of integers in C.

Exercise 5: Quicksort

Implement quicksort of an array of integers in C.

 

History

Monday, 17 February 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 2 14:19:41 2003.
The source to the document was last modified on Mon Feb 17 13:54:16 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/arrays.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu