How can I populate a dynamic two dimensional array in a for loop
Code:
//+------------------------------------------------------------------+ //| Script 1.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //--- //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { int array_body[][2]; bool is_body=true; CheckBody(is_body,array_body,10); } //+------------------------------------------------------------------+ //| Check Body | //+------------------------------------------------------------------+ void CheckBody(bool body,int &array[][],int size) { if(!body) return; int result=ArrayResize(array,size); if(result!=size) { Print("ERROR: size(",IntegerToString(size),") != result(",IntegerToString(result),")"); return; } for(int i=0; i<size; i++) { array[i][0]=i+1; array[i][1]=i-3*i; } //--- ArrayPrint(array); } //+------------------------------------------------------------------+
Result:
[,0][,1] [0,] 1 0 [1,] 2 -2 [2,] 3 -4 [3,] 4 -6 [4,] 5 -8 [5,] 6 -10 [6,] 7 -12 [7,] 8 -14 [8,] 9 -16 [9,] 10 -18
Files:
Script_1.mq5
3 kb
Vladimir Karputov:
Code:
Result:
Thanks, this really helps. I would really like to know if there is a way to populate the array in the code I provided without having to restructure it.
This might seem trivial to many but I'm quite new to MQL5 and haven't been able to figure out how to use ArrayFill() to populate a two-dimensional array.
In the code below I want to populate ar[][2] when isBody is true. How can I achieve this.