Why doesn't my ArrayResize resize my 2D Array?

 

I have attached the code below for your reference.

int arr[][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

void OnInit()
{
   int arrColSize = ArraySize(arr)/3;
   Print("arrColSize: " + arrColSize);
   int theSize = 2;
   int arrSize = ArraySize(arr);
   Print("Row remove: " + ArrayRemove(arr, 1, 1));
   Print("Arr Size is: " + ArraySize(arr));
   for(int i=0;i<arrColSize;i++){
      arr[2][i] = 0;
   }
   
   Print("Arr Resize is: " + ArrayResize(arr, theSize));
   Print("Arr Size is: " + ArraySize(arr));
   ArrayPrint(arr);
}

I am trying to remove the second row. 

{5,6,7,8}

But I only got the following output:

As shown in the documentation linked https://www.mql5.com/en/docs/array/arrayresize, it should return the size if it is resized, but in this case, i checked ArraySize again and it isn't resized (4x3=12) and when printed, it still shows 3 rows.

How do I remove the 3rd row? Basically I want create a function that removes the rows with all zero's in a 2D array and size it down. 

 
razlin:

I have attached the code below for your reference.

I am trying to remove the second row. 

But I only got the following output:

As shown in the documentation linked https://docs.mql4.com/array/arrayresize, it should return the size if it is resized, but in this case, i checked ArraySize again and it isn't resized (4x3=12) and when printed, it still shows 3 rows.

How do I remove the 3rd row? Basically I want create a function that removes the rows with all zero's in a 2D array and size it down. 

What output ? nothing posted.

 
Alain Verleyen #:

What output ? nothing posted.

My bad

 
razlin #:

My bad

Didn't check your code closely, but for multi-dimension array, you need to use ArrayRange(). Please check it.
 
Alain Verleyen #:
Didn't check your code closely, but for multi-dimension array, you need to use ArrayRange(). Please check it.

Thank you for this, I will use ArrayRange() from now on, but perhaps I am not doing it correctly. I have adjusted the above code below.

int arr[][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

void OnInit()
{
   int arrRowsSize = ArrayRange(arr,0); // will return 3
   int arrColSize = ArraySize(arr)/3;
   Print("arrColSize: " + arrColSize);
   Print("arrRowsSize: " + arrRowsSize);
   Print("Row remove: " + ArrayRemove(arr, 1, 1));
   Print("Arr Size is: " + ArraySize(arr));
   for(int i=0;i<arrColSize;i++){
      arr[2][i] = 0;
   }
   
   Print("Arr Resize is: " + ArrayResize(arr, arrRowsSize-1));
   Print("Arr Size is: " + ArraySize(arr));
   ArrayPrint(arr);
}

Output is attached. It still did not remove the last row, despite using ArrayResize().

 
razlin #:

Thank you for this, I will use ArrayRange() from now on, but perhaps I am not doing it correctly. I have adjusted the above code below.

Output is attached. It still did not remove the last row, despite using ArrayResize().

Seems like an mql5 bug. I will check later with a fresh mind.
 
int arrColSize = ArraySize(arr)/3;

ArraySize is only for single dimensioned arrays. You need to use ArrayRange - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5 to get the size of the first dimension (3).

Your array is ArraySize(arr)/4. The second dimension is four.

 
Alain Verleyen #:
Seems like an mql5 bug. I will check later with a fresh mind.

ArrayResize: cannot be used for static allocated array.

If ArrayResize() is applied to a static array, a timeseries or an indicator buffer, the array size remains the same – these arrays will not be reallocated. In this case, if new_size<=ArraySize(array), the function will only return new_size; otherwise a value of -1 will be returned.

If ArrayRemove() is used for a fixed-size array, the array size does not change: the remaining "tail" is physically copied to the start position.

It is clear in the documentation. https://www.mql5.com/en/docs/array/arrayresize and https://www.mql5.com/en/docs/array/arrayremove

Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
  • www.mql5.com
Dynamic Array Object - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
amrali #:

ArrayResize: cannot be used for static allocated array.

If ArrayResize() is applied to a static array, a timeseries or an indicator buffer, the array size remains the same – these arrays will not be reallocated. In this case, if new_size<=ArraySize(array), the function will only return new_size; otherwise a value of -1 will be returned.

It is clear in the documentation. https://www.mql5.com/en/docs/array/arrayresize

Yes you are right, I missed the point that the array is static because it's initialized (and that is not clear in the documentation).

So if you want a dynamic array and initialize it, you need 2 arrays and copy the values.

int INIT[][4]= {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int arr[][4];

void OnInit() {
   ArrayCopy(arr,INIT);
   int arrRowsSize = ArrayRange(arr,0); // will return 3
   int arrColSize = ArraySize(arr)/3;
   Print("arrColSize: " + arrColSize);
   Print("arrRowsSize: " + arrRowsSize);
   Print("Row remove: " + ArrayRemove(arr, 1, 1));
   Print("Arr Size is: " + ArraySize(arr));
   //for(int i=0;i<arrColSize;i++){
   //   arr[2][i] = 0;
   //}

   Print("Arr Resize is: " + ArrayResize(arr, arrRowsSize-1));
   Print("Arr Size is: " + ArraySize(arr));
   ArrayPrint(arr);
}

Output:


 
Thank you, all of you for the input, I see where my mistake is now! I appreciate this :) May you guys have a blessed weekend ahead