Why Are Array Values Printing Out Zeroes

 

I've coded my first array in an EA and I'm having troubles correctly do so.


The array entitled MAS1, and is declared as a double variable.


The intent is to put Moving Average values in the array. However, for some reason when I query the array for a value all I get in return is "0" (zero)


Here is my code:


int start()
{
m1n=Minute();

if(m1n==m1o)
{
return(0);
}
m1o=m1n;
MAS1[1,2]=iMA(NULL,1,50,0,MODE_EMA,PRICE_CLOSE,1);
test1=iMA(NULL,1,50,0,MODE_EMA,PRICE_CLOSE,1);

Print("Minute = ",m1n);
Print("TEST EMA50 = ",test1);
Print("MAS[1,2] = ",MAS1[1,2]);
Print("MAS[3,1] = ",MAS1[3,1]);
Print("MAS[3,2] = ",MAS1[3,2]);

return(0);
}

========================

=======================

The variable "test1" correctly prints the MA value, however, MAS1[1,2] prints a zero


Any thoughts on what I have done incorrectly here?

 
FXtrader2008 wrote >>

I've coded my first array in an EA and I'm having troubles correctly do so.

The array entitled MAS1, and is declared as a double variable.

The intent is to put Moving Average values in the array. However, for some reason when I query the array for a value all I get in return is "0" (zero)

Here is my code:

========================

=======================

The variable "test1" correctly prints the MA value, however, MAS1[1,2] prints a zero

Any thoughts on what I have done incorrectly here?

you should do this MAS1[1][2] not MAS1[1,2]. Please have a look on Array declaration

 
brother3th:

you should do this MAS1[1][2] not MAS1[1,2]. Please have a look on Array declaration

Thank you brother for the feedback. I continued tinkering with the coding after I posted this and have discovered my error. The array was declared as follows:


double MAS1[3][2]


which means I cannot have a data point address of MAS1[3,1] (the largest address in the first dimension is MAS1[2,1]. Even though that address is non-existant because it is outside the bounds of the array, the code compiled okay, but when you try to run the program is causes all the values in the array to report as zeroes.

 
FXtrader2008 wrote >>

Thank you brother for the feedback. I continued tinkering with the coding after I posted this and have discovered my error. The array was declared as follows:

double MAS1[3][2]

which means I cannot have a data point address of MAS1[3,1] (the largest address in the first dimension is MAS1[2,1]. Even though that address is non-existant because it is outside the bounds of the array, the code compiled okay, but when you try to run the program is causes all the values in the array to report as zeroes.

Because the index of an array is start from zero therefore if you declare an array with

double MAS1[3][2]

you can have MAS1[0][0], MAS1[1][0] ... MAS1[2][1] but not MAS1[3][2], it means you have 3 elements in first dimension of an array from 0 to 3 and 2 elements in 2nd dimension of and array from 0 to 1. Hope you know what I mean. Thanks.

P.S. you can use ArrayRange() to detect the largest index of an array.

Reason: