Using "arrays" as input of functions

To add comments, please log in or register
parham.trader
656
parham.trader  

Hi

Hope you're all doing great,

I want to use array as an input of my function. My function can work with all kind of arrays (1D or 2D or 3D arrays) so the array that I'm going to use as an input in my function might be a 1 dimension array, or even 2 dimension or even 3 dimension. Now I want to know is it possible for me to have only one input of array in my function that is independent of its dimension and within the function code it distinguish what is the dimension of the array. I mean an structure similar to the structure of "FileWriteArray" function that it gets an array and it doesn't care the dimension of the array so the input array in the "FileWriteArray" function matter the array is 1D, 2D or 3D.

The current structure that I'm using for array inputs into function is shown below:

func(double &array_to_show_1D[],double &array_to_show_2D[][],double &array_to_show_3D[][][])

But to be honest I think this is not a logical way and instead of the code above, I need something like "FileWriteArray" that takes only one array input and it distinguish the dimension of array automatically :

uint  FileWriteArray( 
   int          file_handle,         // File handle 
   const void&  array[],             // Array 
   int          start=0,             // Start index in the array 
   int          count=WHOLE_ARRAY    // Number of elements 
   );

Your kind guidance is highly appreciated.

Amir Yacoby
1322
Amir Yacoby  
parham.trader:

Hi

Hope you're all doing great,

I want to use array as an input of my function. My function can work with all kind of arrays (1D or 2D or 3D arrays) so the array that I'm going to use as an input in my function might be a 1 dimension array, or even 2 dimension or even 3 dimension. Now I want to know is it possible for me to have only one input of array in my function that is independent of its dimension and within the function code it distinguish what is the dimension of the array. I mean an structure similar to the structure of "FileWriteArray" function that it gets an array and it doesn't care the dimension of the array so the input array in the "FileWriteArray" function matter the array is 1D, 2D or 3D.

The current structure that I'm using for array inputs into function is shown below:

But to be honest I think this is not a logical way and instead of the code above, I need something like "FileWriteArray" that takes only one array input and it distinguish the dimension of array automatically :

Your kind guidance is highly appreciated.

use overriden functions:

func(double &array_to_show_1D[])
{
..
};
func(double &array_to_show_2D[][])
{
..
};
func(double &array_to_show_3D[][][])
{
..
};
Amir Yacoby
1322
Amir Yacoby  
or use template (haven't compile)
template<typename T>
func(T array)
  {
  };

// use with 1D
double arr[];
func<double[]>(arr);

// use with 2D
double arr[][];
func<double[][]>(arr);

// use with 3D
double arr[][][];
func<double [][][]>(arr);


parham.trader
656
parham.trader  
Amir Yacoby:

use overriden functions:

Thank you very much for your time and kind response,

but I'm looking for a more convenient solution, something like "FileWriteArray" that it only takes one array type input and it does not care what is the dimension of the array so you can easily pass a 1D or 2D or even 3D array as an input to this function.

Amir Yacoby
1322
Amir Yacoby  
parham.trader:

Thank you very much for your time and kind response,

but I'm looking for a more convenient solution, something like "FileWriteArray" that it only takes one array type input and it does not care what is the dimension of the array so you can easily pass a 1D or 2D or even 3D array as an input to this function.

This is the way it is done, FileWriteArray is an overriden function. Why do you find it inconvinient? For the user of FileWriteArray it does not matter, it just pass the array, 1/2/3 dimensions, whatever. So is with the overridden func, you just pass the array.

You write 3 functions, but call all 3 the same:
func(arr);


parham.trader
656
parham.trader  
Amir Yacoby:
This is the way it is done, FileWriteArray is an overriden function. Why do you find it inconvinient? For the user of FileWriteArray it does not matter, it just pass the array, 1/2/3 dimensions, whatever. So is with the overridden func, you just pass the array.

You write 3 functions, but call all 3 the same:
func(arr);


Dear Amir,

I'm sorry for my misunderstanding, at first I didn't get what you mean but now it seems that it would be a fantastic solution for the matter.

Now as I tried it to see its operation I faced with some errors stating that "function already defined and has body" as shown in the image below:

Your kind guidance is highly appreciated my friend

Amir Yacoby
1322
Amir Yacoby  
parham.trader:

Dear Amir,

I'm sorry for my misunderstanding, at first I didn't get what you mean but now it seems that it would be a fantastic solution for the matter.

Now as I tried it to see its operation I faced with some errors stating that "function already defined and has body" as shown in the image below:

Your kind guidance is highly appreciated my friend

Can you post the code?
parham.trader
656
parham.trader  
Amir Yacoby:
Can you post the code?

Sure. Here is the code:

//+------------------------------------------------------------------+
//|                                              Overriden_funcs.mq4 |
//|                                                           Parham |
//|                                                                  |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
input string InpFileName="data.bin";
input string InpDirectoryName="SomeFolder";
string path=InpDirectoryName+"//"+InpFileName;
int row_base,sheet_base;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   int src_arr[][3][5],dst_arr[][3][5];

   ArrayResize(src_arr,10);
//ArrayResize(dst_arr,10);

   for(int sheet=0;sheet<ArrayRange(src_arr,2);sheet++)
     {
      sheet_base=(sheet+1)*100;
      for(int row=0;row<ArrayRange(src_arr,1);row++)
        {
         row_base=(row+1)*10;
         for(int column=0;column<ArrayRange(src_arr,0);column++)
           {
            src_arr[column][row][sheet]=sheet_base+row_base+column;
            Print("src_arr[",column,"][",row,"][",sheet,"]: ",src_arr[column][row][sheet]);
           }
        }
     }

   WriteDataOnFile(src_arr);

   Print("Before ReadDataFromFile:");
   Print("column dst_arr: ",ArrayRange(dst_arr,0)
         ,"\nrow dst_arr: ",ArrayRange(dst_arr,1)
         ,"\nsheet dst_arr: ",ArrayRange(dst_arr,2));

   ReadDataFromFile(dst_arr);

   Print("After ReadDataFromFile:");
   Print("column dst_arr: ",ArrayRange(dst_arr,0)
         ,"\nrow dst_arr: ",ArrayRange(dst_arr,1)
         ,"\nsheet dst_arr: ",ArrayRange(dst_arr,2));

   Print("column src_arr: ",ArrayRange(src_arr,0)
         ,"\nrow src_arr: ",ArrayRange(src_arr,1)
         ,"\nsheet src_arr: ",ArrayRange(src_arr,2));

   for(int sheet=0;sheet<ArrayRange(dst_arr,2);sheet++)
     {
      for(int row=0;row<ArrayRange(dst_arr,1);row++)
        {
         for(int column=0;column<ArrayRange(dst_arr,0);column++)
           {
            Print("dst_arr[",column,"][",row,"][",sheet,"]: ",dst_arr[column][row][sheet]);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Write n elements of the array to file                            |
//+------------------------------------------------------------------+
void WriteDataOnFile(int &arr_1D[])
  {
//--- open the file
   ResetLastError();
   int file_flags=FILE_WRITE|FILE_BIN,
   handle=FileOpen(path,file_flags);
   if(handle!=INVALID_HANDLE)
     {
      //--- write array data to the end of the file
      FileSeek(handle,0,SEEK_SET);
      Alert("FileWriteArray: ",FileWriteArray(handle,arr_1D,0,WHOLE_ARRAY));
      //--- close the file
      FileClose(handle);
     }
   else
      Print("Failed to open the file, error ",GetLastError());
  }
//+------------------------------------------------------------------+
//| Write n elements of the array to file                            |
//+------------------------------------------------------------------+
void WriteDataOnFile(int &arr_3D[][][])
  {
//--- open the file
   ResetLastError();
   int file_flags=FILE_WRITE|FILE_BIN,
   handle=FileOpen(path,file_flags);
   if(handle!=INVALID_HANDLE)
     {
      //--- write array data to the end of the file
      FileSeek(handle,0,SEEK_SET);
      Alert("FileWriteArray: ",FileWriteArray(handle,arr_3D,0,WHOLE_ARRAY));
      //--- close the file
      FileClose(handle);
     }
   else
      Print("Failed to open the file, error ",GetLastError());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ReadDataFromFile(int &arr_1D[])
  {
   ResetLastError();
   int file_flags=FILE_READ|FILE_BIN,
   file_handle=FileOpen(path,file_flags);
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read all data from the file to the array 
      FileSeek(file_handle,0,SEEK_SET);

      Alert("First try result: ",FileReadArray(file_handle,arr_1D));

      Alert("Second try result: ",FileReadArray(file_handle,arr_1D));

      //--- close the file
      FileClose(file_handle);
     }
   else
      Print("File open failed, error ",GetLastError());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ReadDataFromFile(int &arr_3D[][][])
  {
   ResetLastError();
   int file_flags=FILE_READ|FILE_BIN,
   file_handle=FileOpen(path,file_flags);
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read all data from the file to the array 
      FileSeek(file_handle,0,SEEK_SET);

      Alert("First try result: ",FileReadArray(file_handle,arr_3D));

      Alert("Second try result: ",FileReadArray(file_handle,arr_3D));

      //--- close the file
      FileClose(file_handle);
     }
   else
      Print("File open failed, error ",GetLastError());
  }
//+------------------------------------------------------------------+
Amir Yacoby
1322
Amir Yacoby  
parham.trader:

Sure. Here is the code:

Sorry, not near a computer. But why did you put a comma at the end of this line instead of semi colon? It appears 4 times
int file_flags=FILE_WRITE|FILE_BIN,
Alain Verleyen
38496
Alain Verleyen  

Amir Yacoby:

use overriden functions:
or use template (haven't compile)

This is not working as the type is always the same.

I don't think it can be done in mql.

parham.trader
656
parham.trader  
Alain Verleyen:

This is not working as the type is always the same.

I don't think it can be done in mql.

So how does "FileWriteArray" work properly?

12
To add comments, please log in or register