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[1], the second value is selected by the expression a[2], the third value a[3], 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[1], a[2], a[3], 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[2] = 3;
a[3] = 4*7-1;
a[4] = a[2] + 4;
a[2] = a[4] + a[1];
a[2] = 3*a[2];

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[0]. All the elements of the array a are:

a[0], a[1], a[2], a[3], a[4].

The last element is a[4], and there are 5 elements of array a in total. Therefore, in the C++ language, the first element of an array a is a[0], the second element is a[1], the third is a[2], and so on. In the last example, the element a[0] equals 2, a[1] equals -1, a[2] equals 4, a[3] equals 0.3, a[4] 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[2] is: " << a[2] << endl;
    cout << "a[4] is: " << a[4] << endl;

    a[2] = 3;
    cout << "a[2] is: " << a[2] << endl;

    a[4] = a[2] + 4;
    cout << "a[4] is: " << a[4] << endl;

    a[2] = a[4] + a[1];
    cout << "a[2] is: " << a[2] << endl;

    a[2] = 3*a[2];
    cout << "a[2] is: " << a[2] << endl;
}

The result of this program is:

a[2] is: 4
a[4] is: 11.7
a[2] is: 3
a[4] is: 7
a[2] is: 6
a[2] 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] = 3+2;

The next example assigns the value 9 to the element a[4]:

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