13A. Elementary Arrays

Arrays and indexing

Arrays are useful foils for illustrating scan algorithms, and they are introduced now for that reason. This discussion is brief. Arrays are covered in more detail later.

An array is a collection of numbered variables, all having the same type. Each variable is referred to by its index in the array. Notation A[i] means the variable at index i in array A. A[i] is often called "A sub i" for short.

The indices of an array always start at 0. For example, an array A of 5 items has indices 0, 1, 2, 3 and 4, so A[0], A[1], A[2], A[3] and A[4] are the variables in array A.

Array diagrams

When carrying out hand simulations with arrays, it is common practice to draw an array as a column or row of boxes. Number the boxes, starting at 0. For example, an array called B of 4 integers is diagrammed as follows.

B
0    
1    
2    
3    

Notice that the variables in the array are shown empty. They are uninitialized when the array is created. Performing statements

  B[0] = 2;
  B[1] = B[0] + 2;
changes array B as follows.

B
0  2 
1  4 
2    
3    

Declaring arrays

You are required to declare an array before you use it. Declaration

  int A[10];
declares array A of 10 integers. In general, the form is
  T name[size];
where T is the type of each variable in the array, name is the array's name and size is the number of variables. For example,
  double stuff[100];
creates an array called stuff of size 100, where each variable in array stuff has type double.

Passing an array to a function

At an elementary level, C++ arrays look a lot like Java arrays, but there are some important differences. A big difference is that a C++ program cannot ask an array how large it is. Really! To deal with that, when we pass an array to a function, we will also pass its size.

That means that we cannot say that a function returns the smallest value in array A, but instead we must say that it returns the smallest value of A[0], A[1], …, A[n − 1]. That is awkward, so we will use notation A[0, …, n − 1] in comments. That is not C++ notation, and should only occur in comments or descriptions of functions or algorithms.

To indicate in a function heading that a parameter is an array, write [] after the parameter. For example, a function that returns the smallest member of A[0, …, n − 1] might have the following heading.

  int smallest(int A[], int n)

To pass an array to a function in a function call, just write the name of the array. For example,

  int s = smallest(B, 5)
returns the smallest value in B[0, …, 4].

Exercises

  1. Write a statement that defines a constant called maxNumKangaroos, with value 200. Write another statement that creates an array integers called kangaroo of size maxNumKangaroos. Answer

  2. Using the array kangaroo from the preceding question, write a statement that sets the first variable in array kangaroo to 7. Answer

  3. Using array kangaroo from question 1, does the following make sense?

    kangaroo[maxNumKangaroos] = -1;
    
    Answer

  4. Using function smallest discussed above, write a statement that creates variable r of type int and sets it equal to the smallest number in kangaroo[0, …, 10]. Answer