English

## Help with array

 82 scfx 2014.07.07 21:58   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].Could you please help me to correct it?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++) {    buy[i]=0;    //cut up    if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]
 Moderator3527 GumRai 2014.07.07 22:10 #   ```      //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++) {    buy[i]=0;    //cut up    if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]
 145 aakcaagac 2014.07.07 22:11 #   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 This will store your value.
 82 scfx 2014.07.07 22:22 #   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++) {    buy[i]=0;    //cut up    if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]
 Moderator3527 GumRai 2014.07.07 22:34 #   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.
 82 scfx 2014.07.07 23:12 #   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++) {    buy[i]=0;    //cut up    if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]
 Moderator3527 GumRai 2014.07.08 02:26 #   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
 82 scfx 2014.07.08 04:56 #   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);  ```
 82 scfx 2014.07.08 05:08 #   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++) {    buy[i]=0;    if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]0)     {   buy[i]=ch[i];       buy[i-1]=ch[i];}```
 Moderator3527 GumRai 2014.07.08 05:32 #   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 globallyelseyou 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
 82 scfx 2014.07.08 23:22 #   Thanks, GumRai.SCFX