All about ARRAY in C++

An array is a collection of items of the same data type stored at contiguous memory locations.

Array

Declaring Arrays

To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows −

type arrayName [ arraySize ];

This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement −

double balance[10];

Initializing Arrays

You can initialize C++ array elements either one by one or using a single statement as follows −

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create the same array as you did in the previous example.

#include <iostream>
using namespace std;

int main() {
    int n[10];

    // Initialize elements of array n
    for (int i = 0; i < 10; i++) {
        n[i] = i + 100;
    }

    cout << "Element  Value" << endl;

    // Output each array element's value
    for (int j = 0; j < 10; j++) {
        cout << "   " << j << "     " << n[j] << endl;
    }

    return 0;
}

Output:
Element  Value
   0     100
   1     101
   2     102
   3     103
   4     104
   5     105
   6     106
   7     107
   8     108
   9     109

Advantages of Array

  • Array provides a single name for the group of variables of the same type. Therefore, it is easy to remember the names of all the elements of an array.

  • Traversing an array is a very simple process; we just need to increment the base address of the array in order to visit each element one by one.

  • Any element in the array can be directly accessed by using the index.

Disadvantages of Array

  • Array is homogenous. It means that the elements with similar data type can be stored in it.

  • In array, there is static memory allocation that is size of an array cannot be altered.

  • There will be wastage of memory if we store less number of elements than the declared size.

Multi-dimensional arrays

C++ allows multidimensional arrays. Here is the general form of a multidimensional array declaration −

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three-dimensional integer array −

int threedim[5][10][4];

Two-Dimensional Arrays

The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.

A two-dimensional array can be thought of as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown below −

Two Dimensional Arrays

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

Initializing Two-Dimensional Arrays

Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example −

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

Accessing Two-Dimensional Array Elements

An element in the 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example −

int val = a[2][3];

The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram.

#include <iostream>
using namespace std;

int main () {
   // an array with 5 rows and 2 columns.
   int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

   // output each array element's value                      
   for ( int i = 0; i < 5; i++ )
      for ( int j = 0; j < 2; j++ ) {

         cout << "a[" << i << "][" << j << "]: ";
         cout << a[i][j]<< endl;
      }

   return 0;
}

Output:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8