Arrays
An array could be a series of components of a similar kind placed in contiguous memory locations that may be severally referenced by adding an index to a novel symbol.
That means that, as an example, we will store five values of kind integer in an array while not having to declare five totally different variables, each with a unique symbol. Rather than that, exploitation an array we will store five totally different values of the same type, integer as an example, with a novel symbol.
For example, an array to contain five whole number values of kind integer known as billy might be defined like this:

where every blank panel represents a part of the array, that during this case are number values of type int. These
elements are numbered from zero to four since in arrays the primary index is usually zero, respectively of its length.
Like a regular variable, associate degree array should be declared before it’s used. A typical declaration for associate degree array in C++ is:
type name [elements];
where
type could be a valid type (like int, float…), name could be a valid symbol and also the parts field (which is usually enclosed in sq. brackets []), specifies what percentage of those parts the array should contain.
Therefore, so as to declare associate degree array referred to as billy because the one shown within the on top of diagram it’s as easy as:
int billy [5];
NOTE: parts|the weather} field inside brackets [] that represents the amount of elements the array goes to carry,
must be a relentless worth, since arrays square measure blocks of non-dynamic memory whose size should be determined before execution. so as to form arrays with a variable length dynamic memory is required, that is explained later in this tutorials.
Initializing arrays.
When declaring an array of local scope (within a function, for example), if we tend to don’t specify otherwise, its
elements won’t be initialized to any value by default, thus their content are undetermined till we tend to store some value in them. In each cases, local and global, once we declare an array, we’ve got the chance to assign initial values to every one of its components by enclosure the values in braces . For example:
int billy [5] = { 16, 2, 77, 40, 12071 };
This declaration would have created an array like this:

The amount of values between braces should not be larger than the quantity of components that we tend to declare for the array between sq. brackets [ ]. for instance, within the example of array billy we’ve declared that it’s five elements and within the list of initial values among braces we’ve nominal five values, one for every component.
When associate data formatting of values is provided for associate array, C++ permits the chance of feat the sq. brackets empty [ ]. during this case, the compiler can assume a size for the array that matches the quantity of values enclosed between braces :
int billy [] = { 16, 2, 77, 40, 12071 };
After this declaration, array billy would be 5 ints long, since we have provided 5 initialization values.
Accessing the values of an array.
In array, we will access the value of any of its parts on an individual basis as if it had been a standard variable, so having the ability to each browse and modify its value. The format is as easy as:
name[index]
Following the previous examples during which billy had five components and every of these components was of kind int, the name that we will use to talk over with every part is that the following:

For example, to store the value 75 in the third element of billy, we could write the following statement:
billy[2] = 75;
and, for example, to pass the value of the third element of billy to a variable called a, we could write:
a = billy[2];
Therefore, the expression billy[2] is for all functions sort of a variable of kind int. Notice that the third part of billy is such as billy[2], since the primary one is billy[0], the second is billy[1], and so, the third one is billy[2]. By this same reason, its last part is billy[4]. Therefore, if we write billy[5], we might be accessing the sixth part of billy and so exceptional the dimensions of the array.
In C++ it’s syntactically correct to exceed the valid vary of indices for Associate in Nursing array. this may produce issues, since accessing out-of-range parts don’t cause compilation errors however will cause runtime errors. the explanation why this is allowed are going to be seen more ahead once we begin to use pointers.
At now it’s vital to be able to clearly distinguish between the 2 uses that brackets [ ] have associated with
arrays. They perform 2 totally different tasks: one is to specify the dimensions of arrays once they ar declared; and therefore the second one is to specify indices for concrete array parts. don’t confuse these 2 doable uses of brackets [ ] with arrays.
Some other valid operations with arrays:
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;
// arrays example
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
Output
12206
PDF tutorial is here