## Arrays

If you remember, two papers were required to carry out the process described by the minimum value algorithm. It was obvious that the contents of the two papers were substantially different. The first paper contained many values, while the other contained just a single value. This other paper ended up being represented by a single variable. Can the first paper, having many values, also be represented by a single variable? The answer is – yes.

Let us study the following two problems:

• Write a program that reads in 10 values, then writes them out in the reverse order, that is from the last to the first one.
• Write a program that first reads in a natural number n. Then it should read in n values. It should write out those n values in the reverse order.

In the first problem, in order to be able to output ten values in the reverse order the program needs to somehow store all ten of them. That could be accomplished by using ten variables, like this:

```double a1;
cin >> a1;
double a2;
cin >> a2;
double a3;
cin >> a3;
...
```

This is how first problem could be solved, but what if the program was required to process 100 values, or a million?

It is impossible to solve the second problem in this manner. The only way to solve it would be to have some kind of variable that could store as many values as required. It is possible to create variables of that kind. A fundamental kind of variable that can store multiple values is called an array.

A integer called an index is used to select a particular value from an array. To select the first value in an array named `a` we would write `a`, the second value is selected by the expression `a`, the third value `a`, and so on. When not using a programming language, we would write the indexes (i.e. indices) in a subscript, as in: a1, a2, a3. To denote indices the C++ language uses the square brackets instead.

We also say that `a`, `a`, `a`, etc. are elements of the array `a`. Each element on its own acts the same way as any other variable does. A value can be assigned to an element, or the element can be used in an expression, for example:

```a = 3;
a = 4*7-1;
a = a + 4;
a = a + a;
a = 3*a;
```

We still have to explain how to introduce variables of this kind. To introduce a variable named `a` that can store five elements, the following statement can be used:

```vector<double> a = {2, -1, 4, 0.3, 11.7};
```

The best way to create an array in the C++ language is by using vectors. C and C++ also have another feature for arrays built in, and this feature is commonly referred to as ”C-style arrays” or just ”arrays”. The arrays we present here are commonly called ”vectors”, to avoid confusion.

This statement introduces a new variable named `a` containing 5 elements of type `double`. We also say that the variable `a` is an array, or an array of 'doubles', or an array of 5 values of type `double`.

However, we did not say the whole truth. In the C++ programming language, the indices of arrays actually start from the number 0. What does that mean? It means that the first element of the given array `a` is `a`. All the elements of the array `a` are:

`a`, `a`, `a`, `a`, `a`.

The last element is `a`, and there are 5 elements of array `a` in total. Therefore, in the C++ language, the first element of an array `a` is `a`, the second element is `a`, the third is `a`, and so on. In the last example, the element `a` equals 2, `a` equals -1, `a` equals 4, `a` equals 0.3, `a` equals 11.7.

At first, this might be a bit confusing, but it quickly gets easier with practice. Many other programming languages also follow this convention of starting from the number 0, and all the C++ programmers have learned to start from the number zero when counting array elements.

To use vectors with all the required features in Code::Blocks IDE, you will need to enable the latest version of C++ in Code::Blocks. From the menu, select Settings->Compiler, and the Global compiler settings dialog will open. In the Compiler Flags tab (it is opened by default) check the option titled Have g++ follow the C++11 ISO C++ language standard [-std=c++11]. Visual Studio 2013/2015 enables the required features by default.

Before the arrays can be used in a program, the following lines have to be added to the beginning of the program's source code:

```#include <vector>
using namespace std;
```

The second line does not need to be repeated twice in the same source code file.

Let us give you an example of a program that includes everything said so far:

```#include <vector>
#include <iostream>
using namespace std;

int main()
{
vector<double> a = {2, -1, 4, 0.3, 11.7};

cout << "a is: " << a << endl;
cout << "a is: " << a << endl;

a = 3;
cout << "a is: " << a << endl;

a = a + 4;
cout << "a is: " << a << endl;

a = a + a;
cout << "a is: " << a << endl;

a = 3*a;
cout << "a is: " << a << endl;
}
```

The result of this program is:

```a is: 4
a is: 11.7
a is: 3
a is: 7
a is: 6
a is: 18
```

Furthermore, an index of an array can be an expression. For example, the statement:

```a[4-1] = 3+2;
```

has exactly the same meaning as:

```a = 3+2;
```

The next example assigns the value 9 to the element `a`:

```int i=3;
a[i+1] = 9;
```