An array in CPP is a data structure for storing related information of the same data type. Each storage location in an array is called an array element and is identified by an array index or key. The lowest index address corresponds to the first element and the highest index address to the last element.
Declare an array
The declaration statement for an array is similar to the one used to declare any other variable. An array is declared by writing the data type and array name followed by the number of elements the array holds inside square brackets –
int arraydata[50];
The arraydata array (above) holds 50 sequential int integers. This declaration causes the compiler to set aside enough memory to hold all 50 elements. If each integer requires 4 bytes, the array declaration will reserve 200 contiguous bytes of memory. Array elements are numbered from 0 to the largest element, or 0 to 49 in the case of the array arraydata. Any attempt to initialise more elements than you’ve declared for the array generates a compiler error.
Initialising Arrays
An array can be initialised by using a list of comma-separated values enclosed in braces:
int arraydata[10] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };
If the size of the array is omitted, an array just big enough to hold the initialisation values is created.
int arraydata[] = { 10, 20, 30, 40, 50 };
The built-in C++ function sizeof() can be used to count the number of elements in an array:
const int size = sizeof(arraydata) / sizeof(arraydata[0]);
Accessing Data Stored in an Array
An element is accessed by indexing the array name. This is done by placing the element index within square brackets after the array name. These indexes are called zero-based because the first element in an array is at index 0. The first integer value stored in an array arraydata[10] is arraydata[0], the second is arraydata[1], and so on. The index of the last element in an array is always (Length of Array – 1) or arraydata[9].
Multidimensional Arrays
Although, in theory, an array can have any number of dimensions, it is rare to have more than two dimensions. When an array is declared, each dimension is represented as a subscript in the array.
If an array can be considered a single row of data then a two-dimensional array could be pictured as a table of data consisting of rows and columns or a list of rows. A three-dimensional array could be a cube, with one dimension representing width, a second dimension representing height, and a third dimension representing depth.
A two-dimensional array has two subscripts:
int array[5, 13];
A three-dimensional array has three subscripts:
int cube[5, 13, 8];
Initialising Multidimensional Arrays
Multidimensional arrays can be initialised with values just like single-dimension arrays. Values can be assigned using nested braces, with each set of numbers representing a row as below –
3 different ways to initialise a two-dimensional array.
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
Initialisation of a three-dimensional array.
int test[2][3][4] = {{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }}