How to read Chapter 7 Home Class Info Links Lectures Newsgroup Assignments

Chapter 7 is primarily about arrays. Arrays are the last big missing piece. With arrays, we can start to do some real programming.

Since almost every programming language has arrays or something very similar, and you already know at least one other programming language, this chapter should be mostly review. The main things you need to know are

• what to pay attention to in the many examples given, and
• how arrays in C/C++ differ from other languages.

## How to Use Arrays

### How to Loop Through an Array

The many examples show the standard way to loop through an array -- with a for loop, using a counter called i, from 0 to N, where N is the size of the array, e.g.,

```for (int i = 0; i < n; ++i)
{
...a[i]...
}```

### How to Pass an Array to a Function

The examples also show how to pass an array to a function. For generality, you pass the array with [], which means the length can be anything, plus the actual number of elements, e,g., ;.

```int foo ( int a[], int length )
{
for (int i = 0; i < length; ++i)
{
...a[i]...
}
}```

### How to Pass an Multi-dimensional Array to a Function

Although multi-dimensional arrays are easy to declare and use, they're fairly tricky to pass to functions. In order to know the distance from the first element of row i (a[i][0]) to the first element of the next row (a[i+1][0], the compiler has to know how many elements (columns) are in each row. So you can't write void func(int a[][], int rows, int cols), you have to write void func(int a[][cols], int rows), and cols has to be a constant integer. This makes the function not very general.

There is a way to do this with pointers, but it's less readable than normal array use.

Fortunately, much of the time, your arrays will be data members of classes, accessed only by member functions, and they will not need to be passed as parameters.

## How C++ Arrays are Different

How C/C++ arrays differ from some other languages:

• Arrays are 1-dimensional. In Pascal and Common Lisp, among others, you can define an array of N dimensions. E.g., A[10, 15] is Pascal is a rectangular 10 x 15 array. In C and C++, int a[10][15] works basically the same, but is really an array of 10 items, each of which is an array of 15 items. Java and Javascript are like C and C++.
• The dimensions in array declarations must be constants. You can declare an array with int a[10];, but you can't read a number N and then say int a[n];. (When we get to memory allocation, we'll see how to get around this.) In Lisp, Java and Javascript, you can use a variable size when declaring arrays. In all of these, you can't change the size of an array after you create it.
• Arrays are not copied when passed to functions. If the function with an array parameter modifies the array, e.g., void fn( int[] a ) { a[0] = 1; }, then it modifies the original array passed to the function. This does not happen when you pass other objects, like instances of classes and structures.
• An array can't be the return type of a function. That is, something like int[] getGrades() { ... } is not valid C++.
• A function can't tell how many elements an array parameter has. If we have void fn ( int [] a ) { ... }, then there's no way to get how big a is. In Java and Javascript, for example, you can say a.length to get how big an array is.
• When passing a two-dimensional array (or more) to a function, only the first dimension can be left unspecified. The compiler needs to know how big each row is, so it needs to know how many columns there are per row.