C++ arrays

An array in C++ is an ordered collections of identical objects that can be accessed via an index.

It is implemented as a contiguous block of memory accessed via pointers to the elements. 

Arrays can store multiple values in a single variable, without creating separate variables for each value. 

 

Array declaration

To declare an array, define the variable type, specify the name of the array followed by square brackets and mention its size:

int data[5];

After the declaration, the size and type of arrays cannot be changed in C++.

 

Array declaration and initialization

We can initialize an array during declaration using the following syntax:

int data[5] = {10, 20, 30, 40, 50};

Another method to initialize array during declaration:

int data[] = {10, 20, 30, 40, 50};

Here, the compiler will automatically compute the size of the array.

 

Accessing array elements

Array elements can be accessed using C notation (an index in square brackets, [n]), where the index must be an integer. Like arrays in C, arrays start with index number 0, not 1. The index can be thought of as an offset from the beginning of the array. The index may not be less than zero or greater than the declared size. If the array is declared size n, the index must be in the range 0 to n-1.

The following program shows how to display the array elements in C++:

#include <iostream>
using namespace std;

int main() {
//array declaration and initialization
int data[5] = {10, 20, 30, 40, 50};

//access each array element's value
for (int i = 0; i < 5; i++) {
cout << data[i] << endl;
}
return 0;
}

Output:

10 20 30 40 50

NOTE: There is no index out of bounds checking in C++ like C.

 

Multidimensional arrays

So far, we’ve looked at simple arrays that hold their data in a list. However, most programming languages also support multidimensional arrays, which are more like tables than lists. For example, take a look at Figure 1. The first array in the figure is a one-dimensional (1D) array, which is like the arrays we’ve used so far in this chapter. The next type of array in the figure is two-dimensional (2D), which works like the typical spreadsheet type of table we’re used to seeing.

Figure 1: Arrays can have more than one dimension

In C/C++, we can define multidimensional arrays simply as array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order).

For example, to create a two-dimensional array of integers like the second array in Figure 1, we might use a line of code like this:

 int table[4][4]; 

This line of C++ code creates a table that can store 16 values-four across and four down. The first subscript selects the row and the second selects the column. To initialize such an array with values, we might use the lines shown in Listing A (see below), which would give us the array shown in Figure 2.

Figure 2: The 2D array as initialized in Listing A

Listing A : Initializing a Two-Dimensional Array.

table[0][0] = 0;   table[1][0] = 4;   table[2][0] = 8;    table[3][0] = 12; 
table[0][1] = 1;   table[1][1] = 5;   table[2][1] = 9;    table[3][1] = 13; 
table[0][2] = 2;   table[1][2] = 6;   table[2][2] = 10;   table[3][2] = 14; 
table[0][3] = 3;   table[1][3] = 7;   table[2][3] = 11;   table[3][3] = 15;

 

Initializing a two-dimensional (2D) array

Suppose that we need a table-like array that can hold 12 integers in 3 rows and 4 columns. First, we would declare and initialize the array like this:

int arr[3][4] = {  
   {1, 2, 3, 4} ,   // initialization for row 0 
   {5, 6, 7, 8} ,   // initialization for row 1 
   {9, 10, 11, 12}  // initialization for row 2 
};

The nesting of braces, which denote the destined row, are not mandatory. The following initialization is basically equivalent to the previous example −

int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

 

Accessing elements of a two-dimensional (2D) array

Suppose that a 2D array of int values is initialized, and all the elements of the array are accessed using nested for loops. Basically. an element in 2D array is accessed by using the subscripts, i.e., row index and column index of the array.

Thus, every element in array arr is identified by an element name of the form arr[i][j], where arr is the name of the array, and i and j are the subscripts that uniquely identify each element in arr

See the coding example below:

#include <iostream>
using namespace std;

int main() {
int k = 1;
//2D array declaration
int arr[3][4];

//2D array initialization
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = k;
k++;
}
}

//print each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << "arr[" << i << "][" << j << "]: ";
cout << arr[i][j]<< endl;
}
}

return 0;
}
Output:
arr[0][0]: 1
arr[0][1]: 2
arr[0][2]: 3
arr[0][3]: 4
arr[1][0]: 5
arr[1][1]: 6
arr[1][2]: 7
arr[1][3]: 8
arr[2][0]: 9
arr[2][1]: 10
arr[2][2]: 11
arr[2][3]: 12