Error Array Out Of Range

 

Hello all,

first off apologies for the basic question... I'm a newbie in coding. I am profoundly confused! Below I did the following:

void start(){

double monthlyHi0=iHigh(NULL,PERIOD_MN1,0);

Print("Monthly High 0 is: "+monthlyHi0);

double monthlyHi1=iHigh(NULL,PERIOD_MN1,1);

Print("Monthly High 1 is: "+monthlyHi1);

double monthlyHi2=iHigh(NULL,PERIOD_MN1,2);

Print("Monthly High 2 is: "+monthlyHi2);

}


 And this worked just fine, it printed accurate info. However, when I tried to convert this into a small for loop below, code compiles however I get "Array out of range" error, and I don't understand what I have done wrong! Any pointers are highly appreciated.

void start(){

double monthlyHi[];
  for(int i=1;i<=3;i++)
         {
         Print(i);
         monthlyHi[i] = iHigh(NULL,PERIOD_MN1,i);      // Reports error on this line.
         Print("Monthly High 0 is: "+DoubleToStr(monthlyHi[i]));
         }

}
 

I am not a coder but I found the thread where this issue was solved:

Forum on trading, automated trading systems and testing trading strategies

Something Interesting

Sergey Golubev, 2016.01.27 12:41

Just some example about how to solve An array is out of range issue (I personally had this issue few times so it may be good to read the thread about how we can solve this our error during the coding for example).

 
w84larisa:
void start(){

double monthlyHi[];
  for(int i=1;i<=3;i++)
         {
         Print(i);
         monthlyHi[i] = iHigh(NULL,PERIOD_MN1,i);      // Reports error on this line.
         Print("Monthly High 0 is: "+DoubleToStr(monthlyHi[i]));
         }

}

You need to give the array a size, and then not go beyond that size when accessing the array:

double monthlyHi[4];
 
honest_knave:

You need to give the array a size, and then not go beyond that size when accessing the array:

double monthlyHi[4];

Thank you so much for your response, I saw your reply late, by now I already had it figured out, but I really appreciate you taking the time.
 
Sergey Golubev:

I am not a coder but I found the thread where this issue was solved:



Thanks Sergey, good pointer.
Reason: