Arrays

An array, in its simplest form (one-dimensional), is an ordered list of data items of the same type that are stored in contiguous memory locations. The array has a unique identifier, and individual elements can be referenced using a combination of this identifier and an index number called an array subscript. Array elements in C++ are numbered from 0, so the first element in an array has the subscript 0, while the last element has a subscript equal to one less than the number of items in the array. Like any other type of variable, an array must be declared before it can be used. The declaration takes the general form shown below.

type array_name [number_of_elements];

Note that the value specified in square brackets by number_of_elements must be a constant integer value, since by default an array has a fixed size, and contiguous memory for the array is allocated when the program is loaded into memory. Dynamic arrays are possible, and will be discussed in a later section. An example of a one-dimensional array is shown below.



The Exam Score Array
SubscriptExam Score (%)
098
185
283
380
477
575
670
769
867
963
1059
1154
1252
1349
1442
1537


The table above shows the contents of a one-dimensional array that holds the exam results for a class of sixteen students. Note that the subscript range is from 0-15. In order to access the tenth record in the array, therefore, we would use a subscript of [9].



Initializing arrays

When a local array variable is declared (e.g. within a function) its elements are uninitialised. Static arrays of fundamental data types that have global scope, on the other hand, have their elements initialised to zero by default. In either situation, the array can be initialised to whatever values we specify at the same time they are declared by enclosing the values inside curly braces, as shown in the example below.

int myArray[5] = {1, 2, 3, 4, 5};

The number of values defined within the curly braces must not exceed the number of array elements given inside the square brackets. If the square brackets are left empty, the compiler will assume that the number of elements in the array will be equal to the number of initial values specified within the curly braces.



Accessing array elements

An individual array element can be accessed by the program's code, just like any other type of variable, and the value assigned to it can be read or modified. Each array element of the array myArray, therefore, is essentially a variable of type int. To assign the value 8 to the third element of myArray, we could use the following statement:

myArray[2] = 8;

The following statement assigns the value of the second element of myArray to a variable called x:

x = myArray[1];

Note that square brackets are used both in the declaration of an array variable, to specify the size of the array, and to identify a specific array element, by specifying the array subscript. Note also that array subscripts can be constant integer values or integer variables. An integer variable with an initial value of zero may be incremented by one each time through a loop, for example, to enable a program to access each element of an array in turn, using the variable's value as an array subscript.

In C++ it is possible to use an array subscript that is beyond the actual limits of the array. If we wrote a statement that assigned a value to array element myArray[5], for example, we would be trying to assign a value to an array element that does not exist, since the last element in the array is myArray[4]. Any subscript outside the range specified for an array is said to be out of range. Using an out of range subscript will not usually cause a problem when the program is compiled, but may well cause a problem when the program is loaded and run (e.g. a runtime error). The short program below demonstrates the use of arrays.

// Example Program 1

#include <iostream>
using namespace std;

int main ()
{
  string s;
  int prime[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

  cout << "The first ten prime numbers are: \n\n";
  for (int i=0; i<10; i++)
  {
    cout << prime[i] << "\n";
  }
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 1

The output from example program 1



Multidimensional arrays

In the same way that a one-dimensional array can be compared to a list, multidimensional arrays can be considered to be lists of lists. A two-dimensional array, for example, is essentially a table, in which one two subscripts are used. One subscript is used to reference a table row, and the other is used to reference a column. All elements in the two-dimensional array have the same data type, and each element is referenced using two subscripts. The table below represents a two-dimensional array of integers.



A Multi-dimensional Array
Subscripts0123
01023976428
136899955057
218684119753


The array has three rows indexed from 0 - 2, and four columns indexed from 0 - 3. The array variable would be declared as follows:

int myArray [3][4];

In order to assign the value of the array element stored in the third column of the second row to a variable n, therefore, we would use the following statement:

n = myArray [1][2];   // assigns the value 550 to n



Arrays as function parameters

It may sometimes be necessary to pass an array to a function as a parameter. An array cannot be passed to a function by value, only by reference (i.e. by passing the address of the array to the function). The function declaration must include an argument of the same type as the array, the name of which must be followed by empty square brackets ([ ]). In the code fragment below, the function myFunc() accepts an array of integers as its argument. Subsequent calls to the function will accept any array that has elements of type int.

int myArray[20];
. . .
void myFunc (int intArray[]);
. . .
myFunc (myArray);

The short program below demonstrates the principle of using arrays as function parameters.

// Example Program 2

#include <iostream>
using namespace std;

void printArray (int intArray[], int length)
{
  for (int i=0; i<length; i++) cout << intArray [i] << " " << "\n";
}

int main ()
{
  string s;
  int array01[] = {2, 4, 6};
  int array02[] = {1, 3, 5, 7, 11};

  printArray (array01, 3);
  printArray (array02, 5);
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 2

The output from example program 2