Arrays
Hi folks,
The subject of Arrays in any programming language is a big mystery to any new comer. MQL4 is not exception.
Today we are going to reveal the mystery behind the Array in general and in MQL4 in particular.
What are the Arrays?
In our life we usually group similar objects into units, in the programming we also need to group together the data items of the same type. We use Arrays to do this task.
Arrays are very like the list tables, you group the items in the table and access them by number of the row, but rows in the Arrays called Indexes.
The rows in the Arrays start from 0 not from 1 like anything else. So the index of the first item in an array is 0 and the index of the second item is 1 and the index of the third item is 2 etc.
Note: In some of programming language the Arrays index starts from 1, it called 1 indexed arrays, but in MQL4 and the most of programming languages the Arrays index start from 0 (0 indexed Arrays).

Figure 1 – one dimension array of 10 integers
Array dimensions:
In the most of cases the Array has one dimension like the description above, but in some case the Array has more than one dimension (In MQL4 the maximum dimension is four dimensions).
Multi-dimensional arrays are very like multi-column tables, where the first column is the first dimension of the Array and the second column is the second dimension of the Array etc.
Creating an Array:
The arrays must to be declared (like the variables) before using it in the code. Declaring an array means creating it by giving it a name (identifier), type and size.
In MQL4 we are creating the Arrays like this:
int myarray[50];
In the code above we declared one-dimensional array.
int keyword indicates that the array is an array of integers.
myarray is the name of the array (identifier).
[] telling the compiler that we are declaring an array not an integer variable.
50 is the size of the array.
We have now a one-dimensional array of 50 integers
double myarray[5][40];
The line above is the way to declare two-dimensional of seven arrays each of them consisting of 40 doubles.
Initializing the Array:
Initializing an array (or a variable) means setting its value at the same declaration line.
Look at this code which initializes one- dimensional array of 6 integers:
int myarray [6] = {1,4,9,16,25,36};
The list of array element have to be enclosed between curly braces {} then be assigned to the array.
The number of elements has to be equal to the array size. If you provided number of elements lesser than the array size the last elements of arrays will be filled with zeros. And if you provided number greater than the array size, MQL4 will ignore these elements and set the array to the first elements.
For example the following code will fill the last element of the array with zero because the size of array is 6 elements and we provided it 5 elements:
int myarray [6] = {1,4,9,16,25}; //5 elements only for 6 size array.
Alert ("myarray[0] " + myarray[0]);
Alert ("myarray[1] " +myarray[1]);
Alert ("myarray[2] " +myarray[2]);
Alert ("myarray[3] " +myarray[3]);
Alert ("myarray[4] " +myarray[4]);
Alert ("myarray[5] " +myarray[5]);
The code above will produce will fill the array with the elements you see in figure 2.
Figure 2 - assigned elements lesser than array size
int myarray [6] = {1,4,9,16,25,36,55}; //7 elements for 6 size array.
Alert ("myarray[0] " + myarray[0]);
Alert ("myarray[1] " +myarray[1]);
Alert ("myarray[2] " +myarray[2]);
Alert ("myarray[3] " +myarray[3]);
Alert ("myarray[4] " +myarray[4]);
Alert ("myarray[5] " +myarray[5]);
The code above will produce will fill the array with the elements you see in figure 3.
Figure 3 - assigned elements greater than array size.
Assigning elements to an Array:
You’ve created an array and want to fill it with elements. You can do that in two ways, the first way we have explained above is initializing the array with the elements in the declaration line.
If you didn’t initialize the array yet, don’t worry. You can assign the elements of the array in two ways:
1- Assigning the elements of the array using the element index.
In this method you assign element by element using the index of the element, The first element is 0 index and the second is 1 index etc.
Look at this code:
int myarray [6] ;
myarray[0] = 1;
myarray[1] = 4;
myarray[2] = 9;
myarray[3] = 16;
myarray[4] = 25;
myarray[5] = 36;
Alert ("myarray[0] " + myarray[0]);
Alert ("myarray[1] " + myarray[1]);
Alert ("myarray[2] " + myarray[2]);
Alert ("myarray[3] " + myarray[3]);
Alert ("myarray[4] " + myarray[4]);
Alert ("myarray[5] " + myarray[5]);
In the code above we declared an array without initializing it. Then we set each element with a line of code.
2- Assigning the element of one filled array to an empty array:
In this method you have already filled array and you assign it to another array. The assigned to array must be the same size of the assigned from one.
Look at this code:
int mysourcearray [6] = {1,4,9,16,25,36};
int mycopyarray[6] ;
mycopyarray[0] = mysourcearray [0];
mycopyarray[1] = mysourcearray [1];
mycopyarray[2] = mysourcearray [2];
mycopyarray[3] = mysourcearray [3];
mycopyarray[4] = mysourcearray [4];
mycopyarray[5] = mysourcearray [5];
Alert ("mycopyarray[0] " + mycopyarray[0]);
Alert ("mycopyarray[1] " + mycopyarray[1]);
Alert ("mycopyarray[2] " + mycopyarray[2]);
Alert ("mycopyarray[3] " + mycopyarray[3]);
Alert ("mycopyarray[4] " + mycopyarray[4]);
Alert ("mycopyarray[5] " + mycopyarray[5]);
Note: You can use MQL4 ArrayCopy function which copies one array to another one. But the ArrayCopy is not the scope of our today article and we will discuss the MQL4 Arrays Function in another article.
Accessing the elements of the Array:
When you create an array and fill it with the elements, you can access the elements of the array by the index number of each element.
The first element in the array has the index 0 and the second has the index 1 etc.
Look at this code:
int myarray [6] = {1,4,9,16,25,36 };
Alert ("The first element at myarray is: " + myarray[0]);
Alert ("The second element at myarray is: " + myarray[1]);
Alert ("The third element at myarray is: " + myarray[2]);
Alert ("The fourth element at myarray is: " + myarray[3]);
Alert ("The fifth element at myarray is: " + myarray[4]);
Alert ("The sixth element at myarray is: " + myarray[5]);
In the above code myarray[0] is the first element in the array and myarray[1] is the second element etc. The last element in this array is myarray[5]. (Figure 4).
Figure 4 - accessing the elements or the array
You can type code like this to access the third array element:
int third_element;
third_element = myarray[2];
Alert(third_element); //9
Hope that the Arrays is understandable subject now. And see you in other article.
Coders Guru
