Is it not possible to initalize an array using a variable?

 

I'm simply trying this:

int Lo=Bid*10000;

int Hi=Ask*10000;


int Range=Hi-Lo;


int Field[2][Range];

getting this error message:

'Range' - integer number expected

if I can't initialize an array with another integer variable, is there a workaround?

 

Hi!

Both Bid and Ask prices are in double.


try using MathCeil ..... https://docs.mql4.com/math/MathCeil

ie.

Lo = MathCeil(Bid*10000);


 

Ok, will try. Doesn't initializing as an integer automatically round off any number to an integer?

ex:

double d = 1.2536;
int i = d;
Print(i);
the above would print out "1," or maybe "2" right? (not sure how it rounds, up or down or nearest)

I though most languages did this.

 

Yes, sorry you'r right.


you cannot use an variable to initialize an array.

you have to set it statical.


why do you want it to be dynamical?

maybe you can use a counter instead?

 

I was thinking about initializing the array in any size and then using ArrayResize() to change the dimensions? Does ArrayResize() work that way?

 
inkexit:
the above would print out "1," or maybe "2" right? (not sure how it rounds, up or down or nearest)


It would be rounded down to 1. typecasting a double into an integer always results in the integer value being the largest integer that is smaller than the double value.

inkexit:

I was thinking about initializing the array in any size and then using ArrayResize() to change the dimensions? Does ArrayResize() work that way?


ArrayResize does allow you to change the array size but for the first dimension, not the 2nd, 3rd, or 4th dimensions.

If you think of a 2-dimensional array in row-column form then what your array looks like is Array[row#][column#]. MQL4 requires you to explicity define the number of columns in your 2D array, but you can change the number of rows dynamically.
Reason: