Arrays in C++

An array can be defined as a sequence of data in memory, wherein all the data are of the same type, and are placed in physically adjacent locations.

An array is a group of elements that share a common name, and that are differentiated from one another by their positions within the array. For example, if we have five numbers, all of which are named as x, they may be listed as:

Arrays in C++

The position of each of these elements can be indicated by means of a subscript as:
x1 = 65
x2 = 33
x3 = 64
x4 = 88
x5 = 5

In mathematics, a subscript is a number written to the right of the variable name, slightly below the line, and usually in small type. The subscript indicates the position of the particular elements.

In C++, square brackets are used for this purpose.

The method of subscripting arrays in C++ is different from that used in many other programming languages. The elements of a five-element array in C++ are numbered starting with 0, not with 1. Therefore, the assignment statements for this above example actually should be written as:
x[0] = 65;
x[1] = 33;
x[2] = 64;
x[3] = 88;
x[4] = 5;

Also, an array must be declared, since it is a type of variable. An array containing five elements, all of which are integers, can be declared as follows:

int x[5];

Defining of 1-Dimensional Arrays in C++:
In general terms, a 1-Dimensional array definition may be represented as:

storage-class data-type array[expression];

Where storage-class refers to the storage class of the array,
data-type is the data type,
array is the array name, and
expression is a positive-valued integer expression which indicates the number of array elements.

The storage-class is optional; default values are automatic for arrays that are defined within a function or a block, and external for arrays that are defined outside of a function.

Defining of Multi-Dimensional Arrays in C++:
In general terms, Multi-Dimensional array definition may be represented as:

storage-class data-type array[expression 1][expression 2]...[expression n];

Where storage-class refers to the storage class of the array,
data-type is the data type,
array is the array name, and
expression 1, expression 2,..., expression n are positive-valued integer expression that indicates the number of array elements associated with each subscript.

Remember that the storage-class is optional; default values are automatic for arrays that are defined within a function or a block, and external for arrays that are defined outside of a function.

Also, you can refer the below links to get more details about arrays.
1-Dimensional Array
2-Dimensional Array

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *