Multi-Dimensional Array problem

 

hello everyone,

I think this maybe an easy problem to solve, but I can't get it to work with out causing an error.

   int a=0,i=1;
   double test [a][i]; // invalid index value
   test[a][i] = 1.35; 
  Alert(test[a][i]);
 
  • you can define the array sizes only with fixed numbers:
    #define a 0
    #define i 1
    double test[a][i], test2[][1]; // that would work
    
    
  • see in the reference (editor -> f1 -> ArraySize() or google for examples for multi-dimension arrays
 

Here's an easy solution... use this data collection type instead ;) 

#include <Custom\two_dimensional_data.mqh>
void OnStart()
{
   TwoDimensionalData<double> arr;
   
   arr.Set(3.14159,3,4);
   arr.Set(1.21,1,2);
   Print("Get method 1 = ",arr.Get(3,4));
   
   double num;
   
   Print("Get method 2 = ",arr.Get(num,20,20)," = ",num);
   
   Print("Array is ",arr.Total_D1()," by ",arr.Total_D2());
   for(int i=0;i<arr.Total_D1();i++)
      for(int j=0;j<arr.Total_D2();j++)
         Print("iterate_array[",i,"][",j,"] = ",arr.Get(i,j));
}

File attached

Files:
 
Carl Schreiber:
  • you can define the array sizes only with fixed numbers:
    #define a 0
    #define i 1
    double test[a][i], test2[][1]; // that would work
  • see in the reference (editor -> f1 -> ArraySize() or google for examples for multi-dimension arrays
I wouldn't a double dimension at all. Use a structure.
struct aType{
   double a[5];
   int    i;
   double get(){ return a[i]; }
}
aType test2[]; ... test2[x].i=...
Reason: