# Class 36: Multi-dimensional arrays

Back to Pause for Breath. On to Sorting.

This outline is also available in PDF.

Held: Wednesday, 3 November 2010

Summary: We consider multi-dimensional arrays. That is, we consider arrays of arrays. We examine how one declares them, why one might use them, and some complexities in their use.

Related Pages:

Notes:

• I'm trying to reserve most of today for lab.
• There is no reading for Friday.
• I hope you enjoyed yesterday's digressions. I do think that it is important that prospective computer scientists know about these "big ideas" from the discipline.
• As part of PragProWriMo, I'm trying to write extended pieces on the more complicated parts of K&R and C. If you observe things that you'd like a short piece (2-4 pages) on, please let me know.
• We'll use a few minutes at the start of class for the final set of questions on Exam 2.

Overview:

• Multi-dimensional arrays.
• Why?
• Arrays of pointers vs. Arrays of arrays.
• K&R example, revisited. (Reading forthcoming.)
• Lab.

## Multi-Dimensional Arrays

• You can make arrays of anything.
• Even arrays of pointers and arrays of other arrays.
• What's the syntax for declaring?
• An array of pointers
```TYPE *NAME[SIZE];
```
• An array of arrays
```TYPE NAME[ROWS][COLS];
```
• You can initialize them when declaring.
```char *strings[3] = { "foo", "bar", "baz" };
int stuff[2][3] = { { 1, 2, 3},
{ 4, 5, 6} };
```

## Why Do We Need Multi-Dimensional Arrays?

• When we have arrays of strings.
• When we want to represent matrices for computation (e.g., for those physics types)
• When we want to represent compound data, one way to represent compound data is an array. (E.g., we might represent a student in this class by a five element array of strings: Last name, First name, Student ID, Major, and Grade.)
• A collection of compound data can then be represented as an array of arrays. (Or, more precisely, an array of arrays of arrays.)
• As you've no doubt determined, tables of numbers are often useful. Two dimensional arrays simultate tables.

## Arrays vs Pointers, Revisited

• We've seen that arrays and pointers are similar in some ways, but different in others.
• What's the difference between an array of pointers and an array of arrays?

## Selected Issues in the Example

This section of the class is being moved to a forthcoming reading that I will distribute after we discuss sorting.

• K&R talk about sorting, but raise other interesting issues.
• When you're reading strings, you need to read them into somewhere.
• They previously wrote `alloc` to give you a way to deal with that issue.

## Lab

Back to Pause for Breath. On to Sorting.

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 Wed Dec 8 10:57:11 2010.
The source to the document was last modified on Fri Aug 27 07:12:23 2010.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Outlines/outline.36.html`.

Samuel A. Rebelsky, rebelsky@grinnell.edu