Download MetaTrader 5

Problem with populating an array..?

To add comments, please log in or register
mar
1147
mar  

Hi Forum,

can someone tell me how to populate the array correct? I only get zeros as a result of test[i].

int init()
  {
   int test[];
   for(int i = 1; i<=10; i++)
   {
      test[i] = i;
      Print(i," , ", test[i]);
   }
   return(0);
  }
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
mar:

Hi Forum,

can someone tell me how to populate the array correct? I only get zeros as a result of test[i].

How big is your array, how many elements are in it ?

int test[1];     // array is 1 element

int test[10];    // array is 10 elements

int test[1000];  // array is 1000 elements

int test[];    // how many elements ? ?
mar
1147
mar  

Ah ok... I didn't know that is necessary to define the array-size. Now it is working. the results are now almost correct. The last print is 10, 0 instead of 10,10. Is that because the array starts counting with 0? But then I wonder why the numbers from 1-9 are correct.


Edit: I think that an array must be populated with 0 as the start, correct? When I see my example, I start populating it with 1.

Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
mar:

Ah ok... I didn't know that is necessary to define the array-size. Now it is working. the results are now almost correct. The last print is 10, 0 instead of 10,10. Is that because the array starts counting with 0? But then I wonder why the numbers from 1-9 are correct.


Edit: I think that an array must be populated with 0 as the start, correct? When I see my example, I start populating it with 1.

Yes, all arrays on any programming language I have seen all start at 0 as the first element number . . . the Order and History pools are also arrays, bear that in mind for the future.
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
mar:

Ah ok... I didn't know that is necessary to define the array-size.

You don't have to define it with a size . . . but at some point before you use the array you have to give it a size, so you could do this . . .

int init()
  {
   int test[];
   for(int i = 0; i < 10; i++)
      {
      ArrayResize(test, ArraySize(test) + 1);
      test[i] = i;
      Print(i," , ", test[i]);
      }
   return(0);
  }
Alain Verleyen
Moderator
32524
Alain Verleyen  
RaptorUK:
Yes, all arrays on any programming language I have seen all start at 0 ass the first element number . . . the Order and History pools are also arrays, bear that in mind for the future.

Please watch your language on this forum
mar
1147
mar  
Thanks, Raptor! I bear that in mind.
To add comments, please log in or register