CSC161 2010F Imperative Problem Solving

Lab: Multi-Dimensional Arrays

Summary: In today's laboratory, we explore issues pertaining to mutli-dimensional arrays.



a. Create a directory for this lab.

b. Add the standard Makefile to that directory. (Note that the -Wall flag will give you warnings for some of the code we provide. You can safely ignore some of the warnings.)


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 row zero contains 8, 16, 32, and 64; row one contains 5, 7, 9, and 11; and row two contains 0, 1, 2, 3.

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

Exercise 2: Multi-Dimensional Arrays as Single-Dimensional Arrays

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 < 12; i++) {
    printf("cow[%d]: %d\n", i, cow[i]);
  } // for 

b. Verify your results experimentally.

Exercise 3: Bounds Violations in Multi-Dimensional Arrays

Consider again the declaration of bat above.

a. What values do you expect to get for the following?

  printf ("bat[0][4] = %d\n", bat[0][4]);
  printf ("bat[0][7] = %d\n", bat[0][7]);
  printf ("bat[1][7] = %d\n", bat[1][7]);
  printf ("bat[2][4] = %d\n", bat[2][4]);

b. Check your answer experimentally.

Exercise 4: Declarations, Revisited

Consider the following code.

  int rabbit[2][3] = { 1, 2, 3, 4, 5, 6 };
  int r, c;

  for (r = 0; r < 2; r++)
    for (c = 0; c < 3; c++)
      printf ("rabbit[%d,%d] = %d\n", r, c, rabbit[r][c]);

a. What do you expect to happen when you try to compile this code?

b. Check your answer experimentally.

c. What do you expect to happen when you try to run this code?

d. Check your answer experimentally.

Exercise 5: Printing Arrays

a. Suppose a is a NxM array. Write instructions for printing a as a grid. For example, for rabbit above, we might print

  1  2  3
  4  5  6

b. Rewrite your code to work with rabbit.

c. Rewrite your code to work with bat.

Exercise 6: 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. Check your answer with sizeof.

c. Can one initialize chincilla while declaring it?

d. Check your answer experimentally.

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

f. How would you verify your previous answer?

For Those with Extra Time

Write a function, printIntMatrix (int rows, int cols, int matrix[rows][cols]), that prints a matrix as in problem 5.



Monday, 17 February 2003 [Samuel A. Rebelsky]

Tuesday, 2 November 2010 [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 Tue Nov 2 10:48:01 2010.
The source to the document was last modified on Tue Nov 2 10:48:00 2010.
This document may be found at

Samuel A. Rebelsky,