# Help with array

267

Hi Everyone,

I try to work with array and write this simple code.

I am not sure why it show the "array out of range" right away at the cutup[I]=High[I].

Thank you so much,

SCFX

```   int limit=rates_total-prev_calculated-1;
if(limit<=0) limit=1;

double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
//cut up
if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
cutup[i]=High[i]; //store value to that variable

}```
Moderator
10568

```
//IF 50 BARS ON CHART EARLIEST BAR WILL BE INDEX 49
int limit=rates_total-prev_calculated-1;
// AT FIRST RUN 50-0-1 , LIMIT WILL EQUAL 49
if(limit<=0) limit=1;

double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
//cut up
if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
//i+1 IS TRYING TO ACCESS BAR INDEX 50 WHICH DOESN'T EXIST
cutup[i]==High[i]; //store value to that variable

}```
168

Tried to give your array a size ?

double cutup[1024];

might help. Maybe you need to create an even bigger array 4096/8192/...

cutup[i]==High[i]; //store value to that variable

This doesn't story anything. == is a comparison.

cutup[i]=High[i]; //store value to that variable

267

Hi GumRai,

I am not sure if it is the cause . If I simply skip the cutup step and put buy=High directly, there is no error at all.

I see the logic behind that 50 and 49 and it make me confused why no "array out of range" error show up.

```   int limit=rates_total-prev_calculated-1;
if(limit<=0) limit=1;

double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
//cut up
if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
//cutup[i]==High[i]; //store value to that variable

I also change to below limit but still cannot solve the issue

```   int limit=rates_total-prev_calculated-2;
if(limit<=0) limit=2;```

that trick double cutup[1024]; is not working either.

SCFX

Moderator
10568

scfx:

I am not sure why it show the "array out of range" right away at the cutup[I]=High[I].

scfx:

I see the logic behind that 50 and 49 and it make me confused why no "array out of range" error show up.

Are you getting "array out if range " or not?

You seem to be contradicting yourself.

267

GumRai:

Are you getting "array out if range " or not?

You seem to be contradicting yourself.

What I mean is that this code below has No "array out of range' error, even though I only skip the cutup step

//cutup[i]==High[i]; //store value to that variable

and put

`   buy[i]=High[i];`

That's why I don't think the 50 to 49 is the reason for the error in the original code.

```   int limit=rates_total-prev_calculated-1;
if(limit<=0) limit=1;

double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
//cut up
if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
//cutup[i]==High[i]; //store value to that variable

Moderator
10568

scfx:

What I mean is that this code below has No "array out of range' error, even though I only skip the cutup step

//cutup[i]==High[i]; //store value to that variable

and put

That's why I don't think the 50 to 49 is the reason for the error in the original code.

Oh, I see.

You need to ReSize the array cutup

267

GumRai:

Oh, I see.

You need to ReSize the array cutup

So I do the following resize. Is that the right way to do it?

The indicator at least works now.

Thank you,

SCFX

```   double ch[];
ArrayResize(ch, rates_total);  ```
267

I want to give value when cut happened to both: bar[I] and bar[I-1].

This code below give value on bar[I] but it sometime give value to bar[I-1], sometime not.

Could you help me see why it is like that?

Thank you,

SCFX

```   int limit=rates_total-prev_calculated-1;
if(limit<=0) limit=1;

double ch[];
ArrayResize(ch, rates_total);

//---
for(int i=1; i<=limit ;i++)
{

if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))   )

ch[i]=High[i];

if(ch[i]>0)
Moderator
10568

scfx:

So I do the following resize. Is that the right way to do it?

The indicator at least works now.

Thank you,

SCFX

```   double ch[];
ArrayResize(ch, rates_total);  ```

As the array is declared locally, you will be be allocating many more bytes to the array than necessary.

If you want to store the values in the array, declare it globally

else

you will only need it to be large enough to work with the last few bars (following the initial tick)

`ArrayResize(ch, limit+1);`

should be enough

267

Thanks, GumRai.

SCFX