Last Updated:

Arrays in C++ Programming 

 

The array in the table below has 8 elements, each of which stores a number of real type. The elements in the array are numbered. This kind of array, which is a simple list of data of the same type, is called a one-dimensional array. To access data stored in a specific element of an array, you must specify the array name and sequence number of that element, called an index. Note that the array in the table has a maximum index of 7, although the array size is 8 cells because it starts at zero.

12.70.13-1.5021.9-3.75.0121.7
0th element1st element2nd elementElement 3Element 4Element 5Element 6Element 7

When there is a need to store data in the form of tables, in the format of rows and columns, you will have to use two-dimensional arrays. The following table shows an example of an array consisting of four rows and four columns. Rows in it are considered the first dimension, and columns are considered the second. To access the data stored in this array, you must specify the name of the array and two indexes, the first must match the number and the second the number of the column in which the desired element is stored.

 1 column2 column3 column4 column
1 line3.57.81.30.6
Line 2-1.40.3012.1
Line 3-5.7-0.785.06.9
Line 445.1124.0-24.70.96

Description of arrays

You can describe an array in C++ as follows:

Type array_name [dimension];

Dimension is the number of elements in an array. For example:

The dimension of an array and the type of its elements determine the amount of memory that is needed to store an array, so dimensionality is an integer positive constant expression.

As I said, array elements in C++ are numbered from zero. The first element always has a number of zero, and the number of the last element is one less than the dimension specified when describing it:

Each element of the array is accessed using an index, the sequence number of the element. To refer to an array element, specify its name, and then in square brackets the index

Array_name [index]

For example:

An array, like any other variable, can be assigned an initial value. To do this, the values of the array elements must be listed in curly braces separated by commas:

variable_name type [dimension] = {element_0, element_1, ...};

For example:

The input and output of the elements of the array is carried out element by element. For example, this:

In the following lessons, we'll cover basic operations on arrays, such as sorting the elements in an array, finding the maximum element, the bubble method, and so on. As for two-dimensional arrays, they will be described in detail in the lessons about matrices.