Array of arrays C++

Array in C++ is the containers having the same data type elements like integer, character, float, etc. Each number in an array stored at a specific index can be accessed through these index numbers. In the C++ programming language, an array is implemented and declared in 3 ways: by mentioning the array size, the second one is by initializing elements of the array directly, and the third one is by specifying the size of the array with the elements. This article will contain an array of array descriptions using some elementary examples.

To understand the concept of the array, the user should be aware of the basics of the C++ language. We implemented all source codes on the Ubuntu terminal by using a text editor to write the C++ programs. And for the resultant values, we have used the Ubuntu terminal here.

Working of arrays in C++

An array contains more than one variable of the same data type, and it works in the same way as the single variables do. When we create an array in C++ or any other language, we mention the number of elements we want to enter into an array. One thing should be kept in mind that the size of an array is fixed throughout the execution of a program, unlike the vector array.  And we have to save the same number of values in the array. Also, some limitations are there depending on the data type. If the data type of array lets us suppose integer, it will not accept the value of any other data type.

Array Creation in C++

The array creation process is the same as creating a variable. The first step is the array declaration. After that, we initialize the array at the same time or maybe later. The syntax is written as:

Datatype array name [size];

Example:  int array1 [10];
In this example, array1 is the name of an array with an integer data type, having 10 elements. If someone tries to put more than 10 values, it will throw an error.

Types of arrays in C++

In C++, there are mainly two arrays: one is a single-dimensional array, and the second is a multidimensional array. The single-dimensional array stores the values in the form of a list. Whereas the multidimensional array contains the values in the form of the matrix. A multidimensional array is also known to be an array of arrays and can be divided into sub-parts like two-dimensional arrays and three-dimensional arrays.

Now let us mention some examples covering the given article.

Example 1
Writing a C++ program in the text editor has easy use of a single-dimensional array. This array has a data type integer with the name “arr”. The size is defined as ‘5’. All the values are assigned when the array is declared. We always use a loop to display and insert the values in an array. As in this example, the values are assigned at the time of declaration, so no need to insert the values. To display the entered data, we use a “For” loop.

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

The output can be obtained by using a G++ compiler. You can observe that on execution, the output is obtained in the same line.

Example 2
This example contains a two-dimensional array.

Here we create an array of the array, also known to be a multidimensional array. The basic syntax is defined as:

Data type Arrayname[row][column];

As we know that a multidimensional array is created in the form of a matrix. In a declaration of a multidimensional array, first comes row and then column number. For example:

int array1 [5] [3];

This example contains an array with name array1 of integer data type having 5 rows and 3 columns.

Now consider an example that contains an array of 3 rows and 2 columns. All the values are assigned at the time of declaration.

int array [3][2] = { {9, -5}, {7, 0}, {4,1} };

In the previous example, we have used a loop to display the values. Similarly, to display the elements of a multidimensional array, we use a nested a “For” loop. The rows of the matrix are accessed by the outer loop, and the columns are accessed through the inner loop of the nested for loop. The index number is displayed with the values inside it.

Execute the code with the help of a compiler. Both the index numbers are displayed with the values inside them, either with positive or negative numbers.

Example 3
This is an example of a three-dimensional array that can store elements up to 12 values. ‘test’ is the name of an array with the integer data type and with the sequence of a matrix that contains one portion of a row, a second portion of the column and then the third portion diagonally. This example will also contain nested loops. But these are three for loops. Starting with the 0 indexes and lasting for 2, 3, and again 2 indexes. All the index numbers with the values are obtained by using nested loops.

Test [i] [j] [k];

Here “i” variable is for rows, “j” is for the column and “z” is for the third parameter.

You can see the resultant value from the Ubuntu terminal by executing the file.

Example 4
This example relates to the initialization of two constant variables first. These variables represent the row and the column of a multidimensional array. One is a city, and the other is a week. Both these variables are globally created. And can be used in the main program or any other function as well. This example has user involvement as the values are taken by the user.

int temp [city] [week];

We have taken the temperature for the city on any special day. For the two-dimensional array, we have used a nested loop. Both the loops have a terminating point as the constant variables. Temperature values are taken so that two values are taken for a single city at different points. So the outer loop will execute two times; for the first execution, the temperature is taken for one city for one day. In the second execution, the temperature is taken for the same city on a different day.

After the data is entered once, it is accessed by the nested for loops. This time the index numbers are not directly displayed but both the values of city and week are displayed as the loop executes.

You can see the resultant values from the Ubuntu terminal from the compiler. The inserted values are abruptly displayed after the user has stopped entering values.


The topic under discussion, “array of arrays in C++,” contains the explanation of arrays, including array creation, working, and declaration of the array. Also, this article contains examples of three types of arrays, single arrays and multidimensional arrays, including two-dimensional and three-dimensional arrays. All these types are further explained with the help of examples. Each part is discussed to increase the knowledge of the user in this aspect of the C++ array of arrays concept.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.