Adding an Additional Element to Array to Avoid 'Out of Range' Error

 
double DR[260]; 

for(j=1; j<=259; j++)
   {
   DR[j]=Close[j]-Open[j]; 
   Sum=Sum+DR[j];
   Count++;
   }

In the above code, to the best of my knowledge, the loop should run for 259 iterations and therefore the array 'DR' would require 259 elements for the values to be stored.

However if I set 'DR' to 259:

double DR[259];

The error: 'array out of range' is returned in the below position:

DR[j]=Close[j]-Open[j];

If I increase the number of elements in 'DR' to 260 (as in the first code excerpt), the error disappears, but I am struggling to fathom why or how the array requires an additional element. 

Is there an explanation for why this is happening? It may well be something simple that I have missed but can't seem to understand what it is.

Thanks in advance. 

 
koranged:

In the above code, to the best of my knowledge, the loop should run for 259 iterations and therefore the array 'DR' would require 259 elements for the values to be stored.

However if I set 'DR' to 259:

The error: 'array out of range' is returned in the below position:

If I increase the number of elements in 'DR' to 260 (as in the first code excerpt), the error disappears, but I am struggling to fathom why or how the array requires an additional element. 

Is there an explanation for why this is happening? It may well be something simple that I have missed but can't seem to understand what it is.

Thanks in advance. 

It doesn't require an addition element

you have sized the array at 259 and that gives you element index 0 to 258, so there is no index 259.

 
Keith Watford:

It doesn't require an addition element

you have sized the array at 259 and that gives you element index 0 to 258, so there is no index 259.

I have sized the array at 260 in order to get it to work. 

If you size the array at 259 it returns the error 'out of range'. 

Of course the elements are indexed from zero but as far as I am aware, the number of elements (not the index) is counted from 1, not 0. So if you declare an array like this:

double DR[260];

Then to my understanding there are 260 elements in this array, indexed 0-259. 

But if I run a for loop with 259 iterations and declare the array as below:

double DR[259];

then as mentioned it returns the 'out of range' error. Despite the seeming fact that there are 259 elements with 259 values to be saved.

Unless you are saying that the elements (not the index) are actually counted from 0, if this is the case then the below array:

double DR[259];

actually contains 260 elements (0-259).

Is this correct or have I just gone off on a tangent? 

 

Yes and you already know about 

ArrayResize(array,size,reserve);
 
Marco vd Heijden:

Yes and you already know about 

Yeah I know about ArrayResize but my interpretation is that one would only use this function when the number of elements in an array must be dictated by a variable, rather than a constant, or if the number of elements needed to be altered during program execution. 

In this example I am simply trying to calculate values for a years worth of trading days, so the array can be declared with a constant.

Thanks.

 
Marco vd Heijden:

Yes and you already know about 

So just to confirm you are saying that an array declared like so:

double DR[259];  

contains 260 elements (indexed 0-259), rather than 259 elements (indexed 0-258)?

If this is the case then every value from the 259 iterations of the loop should be recorded in the array, with one element to spare (260-259=1).

I'm struggling to understand how this can be true because if I declare the array in this fashion, then as mentioned the error 'out of range' is returned, which is nonsensical because according to my understanding of your explanation, there is actually an extra element rather than one too few in the array. 

 
https://www.mql5.com/en/docs/basis/variables#array_define
Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables must be declared before they are used. Unique names are used to identify variables. To declare a variable, you must specify its type and a unique name. Declaration of variable is not an operator. Only an integer can be an array index. No more than four-dimensional arrays are allowed. Numbering of array elements starts with 0. The...
 

The problem was due to the loop, instead of this:

for(j=1; j<=259; j++)
   {
   DR[j]=(Close[j]-Open[j])/Open[j]; 
   Sum=Sum+DR[j];
   Count++;
   }

It should have been this:

for(j=0; j<=258; j++)
   {
   DR[j]=(Close[j+1]-Open[j+1])/Open[j+1]; 
   Sum=Sum+DR[j];
   Count++;
   }

In the first line of the 'for' operator's body, I was assigning the quotient to index value 1 of 'DR' rather than index value 0. 

Reason: