NavigationUser loginWho's new
Who's onlineThere are currently 0 users and 54 guests online.
|
ArraysHi folks, 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. 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. 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] ; 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}; 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 }; 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; Hope that the Arrays is understandable subject now. And see you in other article. Coders Guru |