Section 1 - Declaring Arrays

Introduction to Week Eight

We have skipped one of the most useful tools in programming, arrays, because we wanted to move fast and get into some interesting C++.  Now that we have so many tools at our disposal, we can add arrays and really have some fun with them.

With arrays we can quickly declare large numbers of data objects organized in a single group. They can be used more efficiently than if we had declared hundreds of separate, unrelated objects.

Reading

As usual, first study this module completely. Then, if you want more, read the (single dimensional) array material in the text.

8A.1.1 What is an Array?

An array is a group of objects that are positioned in contiguous memory locations that have the same root name, and are distinguished from each other by an index. If the root name of the array was student and we wanted 20 variable objects of type int in this array, we would declare and allocate the array as follows:

int student[20];

meaning that we desire 20 int objects, stored in consecutive memory locations, and the names of the individual elements are to be:

student[0]
student[1]
student[2]
.
.
.
student[19]

Note that the numbering begins with 0, and ends with 19. The elements are stored one after another, starting with student[0] as the first, or smallest, location in memory.

The variable student[0] is called an element of the array, but remember that its value can be controlled individually as if it were a separate object declared individually:

student[0] = 25;
cout << student[0];

and so on.

The integer inside the brackets is the array index. It can be a constant or integer expression. Be careful not to confuse the size of the array, which is a constant inside the brackets in the allocation statement:

int student[20];

with the index of the array, which must be in the range from 0 to [size of array] - 1, or 19 in this case.

Usually, each array element is modified in a way that brings out the group nature of arrays. For example, to set every array element to 7, we could use the following loop:

for (k = 0; k < 20; k++)
student[k] = 7;

By using a k as an index, you can run through all of the array elements in a single for statement.   As simplistic as it sounds, this for loop demonstrates the main characteristics of how arrays are used in programs.  It shows how natural it is to marry arrays with for loops.

8A.1.2 The Pointer Nature of Arrays

tree

Although the above array might consist of individual primitive data elements (ints), the array itself behaves like a lightweight value that "points" to the list of array elements. 

When we pass an array name  to a method as an argument, we are only passing a "pointer" to the elements in the array.  The array is not copied into the method as it would be if it were an ordinary value parameter.  Instead the pointer to the array is passed in, and the array itself stays where it is.  This implies any changes to the array elements inside the method carry back to the main()

 It is not possible to pass arrays back to clients using functional returns..

We will see examples of all of this very shortly.

8A.1.3 The Base Data Type of an Array

Above we used int as the base data type for the array.  However, the base type need not be primitive.  It could be string:

string student[50];

or some class, user-defined, as in an array of Employees:

Employee worker[50];

8A.1.4 Initializing an Array

Besides using the for loop we have seen above, we have a short cut way of initializing an array to some programmer-specified values.  Here is an example:

double myArray[] = {10.2, 56.9, -33, 12, 0, 2,
4.8, 199.9, 73, -91.2};

This list can be as long as you wish, and can go on for several lines.  The important thing is that the values match the base type of the array.  Here is an example with a different base type:

string myArray[] = {"martin", "claudia", "sandra", "samuels",
"terry", "jack", "clark", "palmer", "abraham", "Mike"};