Arrays In C++ Language

In today’s tutorial we will learn on use of arrays in C++ language, as we have been discussing on arrays in other languages. We will look at definition, declaring arrays, initializing arrays and how to access array elements.

Arrays– refers to the collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. They are used to store multiple values in a single variable, instead of declaring a separate variables for each value.

For instance, instead of declaring individual variables, such a car0, car1, car2,…car29. You can just declare one array such as cars and use cars[0], cars[1],cars[2],…,cars[29] which represents individual variables. Whereby a certain element is accessed by an index. There are various types of arrays in C++ language which may include; one-dimensional arrays, multi-dimensional arrays.

In the following section we will look at how you can declare, initialize and access elements in arrays giving out the relevant examples.

How To Declare Arrays

For one to declare an array in C++, you should specify the type of elements and the number of the elements the array requires, look at the syntax below;

type arrayName[arraySize];

In the above syntax type represents any valid C++ data type while arraysize represents an integer constant greater than zero. Look at the example here for declaring a 8-element array called cars of the type double.

double cars[8]

How To Initialize Arrays

Array elements in C++ language can be initialized by either using a single statement or one by one, look at the following example here;


double cars[6] = {50.0, 90.0, 100.0, 7.8, 8.2, 150.5};

In the above example the number of values between the braces {} cannot be larger than the number of elements that are declared between the square brackets[]. Look at the example below whereby you are assigning a single element of the array.


double cars[] = {50.0, 90.0, 100.0, 7.8, 8.2, 150.5};

In the example above the array size is omitted but the arrays are just the same with the first example since an array is created which is is big enough to hold the initialization.

How To Access Array Elements

In this part we will discuss how you can access array elements whereby they are accessed by indexing the array name. You places the index between the square brackets after the array name, look at the example here,

double price =cars[5];

In the above example ti will take the 5th element from the array and assign the value to price variable. Let’s have an example to illustrate array declaration, array initialization and how to access array elements. In this example it makes uses of setw() function to format the output as follows;


#include 
using namespace std;
#include 
using std::setw;
int main ()
{
int n[ 6 ]; // n is an array of 6 integers

// initialize elements of array n to 0
for ( int x = 0; x < 6; i++ )
{
n[ x ] = x + 100; // set element at location x to x + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;

// output each array element's value
for ( int y = 0; y < 6; y++ )
{
cout << setw( 7 )<< y << setw( 13 ) << n[ y ] << endl;
}
return 0;
}



It will produce this;
Element Value
0       100
1       101
2       102
3       103
4       104
5       105

In this part we will discuss various concepts as used in C++ arrays which are very vital for anyone who is studying C++ language. They may include;

  • Pointer to an array -You can generate a pointer to the first element of an array by specifying the array name, without any index.
  • Passing arrays to functions -you can also pass a pointer to an array to the function by specifying the arrays name without an index.
  • Multi-dimensional arrays- The simplest form of this arrays is two-dimensional array which are supported by C++ language.
  • Return array from functions- function returns an array in C++.

Multi-dimensional Arrays

As we have discussed above C++ allows multi-dimensional arrays, the simplest form we will discuss in the following section is the two-dimensional arrays. Let's have a general form of a multi-dimensional array declaration as follows;

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

For better understanding let's have an example here;

int threedim[8][9][3];

Two-dimensional Arrays

In this section we will discuss two-dimensional arrays which is the simplest form of the multi-dimensional array. Look at the following syntax on how you write your two-dimensional array, type stands for any valid C++ data type while arrayName should be any valid C++ identifier.

type arrayName[x][y];

Initializing Two-dimensional Arrays

To initialize multi-dimensional arrays you need to specify the values in brackets for each row. For instance, in the example below the array have 3 rows and each have row have 4 columns. Look at it;


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 above example you can also write it as follows;

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

Accessing Two-dimensional Array Elements

To access two-dimensional array you will use subscripts, that is row index and column index of the array. Lets have an example here;

int val=x[3][4];

We may have an example to explain whatever we have discussed above as followers;


#include 
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;
}