Array initialisation

 

I've been learning about arrays from the mql4 guide, and I have come across a topic that I am having trouble with; array initialisation is slightly bewildering I don't understand the statement below

"

Array Initialization


An array can be initialized only by constants of a corresponding type. One-dimensional and multi-dimensional arrays are initialized by one-dimensional sequence of constants separated by commas. The sequence is included into curly brackets:

int Mas_i[3][4] = { 0, 1, 2, 3,   10, 11, 12, 13,   20, 21, 22, 23 };
 
double Mas_d[2][3] = { 0.1, 0.2, -0.3,    -10.2, 1.5, 7.0 };
 
bool Mas_b[5] = { false, true, false, true, true }

"

I'm not confused by the fact that an array can only be initialised by constants of a corresponding type, but I am confused by the concept of array initialisation and the fact that a one-dimensional sequence can initiate a multi-dimensional array.

I am confused by the following.

"

In the initialized sequence one or several constants can be omitted. In such a case corresponding array elements of numeric type are initialized by zero, elements of arrays of string type are initialized by string value "" (quotation marks without a space), i.e by an empty line (shouldn't be confused with a space). The next program displays values of arrays, initialized by a sequence with omission of some values (script arrayalert.mq4):

//--------------------------------------------------------------------
// arrayalert.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
int start()                                     // Special funct. start()
  {
   string Mas_s[4] = {"a","b", ,"d"};           // String array
   int Mas_i[6] = { 0,1,2, ,4,5 };              // Integer type array
   Alert(Mas_s[0],Mas_s[1],Mas_s[2],Mas_s[3]);  // Displaying
   Alert(Mas_i[0],Mas_i[1],Mas_i[2],Mas_i[3],Mas_i[4],Mas_i[5]);
   return;                                      // Exit start()
  }
//--------------------------------------------------------------------

"

Ultimately I am not grasping the concept of array initialisation.

 

You do understand the concept of arrays thought right ? a variable with multiple indexes ?

All initialization does is set a starting value to all the indexes.

You can set a value to each index seperately like in the examples you posted or,

if you want the initial value to be the same for all of the indexes you can enter the value once like this,

int myarray[100]={0};   //initializes all 100 indexes to value zero

or,

int myarray[100][5]={-1}; //multi dimensions, initializes all 500 indexes to -1.

Also be aware there are some errors in the mql4 book. I think in the second example when it says,

"In the initialized sequence one or several constants can be omitted. In such a case corresponding array elements of numeric type are initialized by zero, elements of arrays of string type are initialized by string value "" (quotation marks without a space), i.e by an empty line (shouldn't be confused with a space)."

and then shows this example,

string Mas_s[4] = {"a","b", ,"d"};           // String array

it was probably meant to be:

string Mas_s[4] = {"a","b","","d"};           // String array
it also says somewhere in the book "all arrays are of the static type by default" that is also an error they are like other variables, not static unless defined as static, except when they are global.
 
SDC:

You do understand the concept of arrays thought right ? a variable with multiple indexes ?

All initialization does is set a starting value to all the indexes.

If you want the starting value to be the same for all of the indexes you can enter the value once like this

int myarray[100] {0} //initializes all 100 indexes to value zero.


Right

I'm still a bit confused.

So If I have an array

Double myarray[100]

I've declared the Array type (double), Array name (myarray) and number of indexes (100) .

Can the value of the array MyArray be assigned using Double myarray[100] = {1, 2, 3, ....}, so that myarray[1]=1 and myarray[2]=2 ect.

So far I have only constructed arrays for the purpose of grabbing values from loops and even then the array was used very loosely.

Thanks, pointing out that mistake cleared up a minor question I had.

 

The first array index is zero so if you do,

double myarray[100] = {1,2,3 .....};

that will make, myarray[0] == 1, myarray[1] ==2 etc

 

Ah right ok.

So what if I have a multi-dimensional Array

Double Myarray[3][4] = {0.1,0.2,0.3,0.4,0.5} (sorry I noticed I used integers earlier)

How can I call a value?

 

you call it like myarray[index,index]; or myarray[index][index]; does the same thing.

val = myarray[2,2]; //gets the value from array index 2,2 puts it in val.

Print(myarray[2,2]); //prints it

Alert(myarray[2][2]); //alerts it

 

What would the value be?

Double Myarray[3][4] = {0.1,0.2,0.3,0.4,0.5}

Myarray[1][3]=?

 
zero because you didnt assign anything to it
 

you have 3 x 4 array

double Myarray[3][4] = {0.1,0.2,0.3,0.4,0.5} ;

it would fill like this

Myarray[0,0] = 0.1;

Myarray[0,1] = 0.2;

Myarray[0,2] = 0.3;

Myarray[0,3] = 0.4;

Myarray[1,0] = 0.5;

Myarray[1,1] = 0.0;

Myarray[1,2] = 0.0;

etc

 
How does array[0,0] have a value that was declared for Myarray[3][4]?
 

I'm clueless right now..

Isn't Myarray[3][4] multi-dimensional, if so why can I only call values by changing the value of one of the dimensions?

Reason: