Please help me!! how to pass 2 dimension array as parameter passed by reference

 

I cannot find out my mistake on the following code of my custom indicator.

I can compile it.

But the MT4 starts thinking forever on this part when I insert it on the chart.

On the other hand it works well on C++.

Please point out my mistake.

void Func(int no, int arrayA[], int no_arrayA, double arrayB[], double arrayC[][], double &result[][])
{
   int i, k, j, c;

   for(i = 0; i <= no_arrayA - 1; i++)
   {
      k = 0;
      j = 0;

      while(j <= no - 1 - 1)
      {
         for(c = 1; c <= arrayA[i]; c++)
         {
            result[i][j] = arrayB[j] - arrayC[i][k];
            j++;
         }
         k++;
      }
   }
}

int start()
{
:
:
:
        double arrayD[][];
        Func(no, arrayA, no_arrayA, arrayB, arrayC, arrayD);
:
:
:
 
buffett1930:

I cannot find out my mistake on the following code of my custom indicator.

I can compile it.

But the MT4 starts thinking forever on this part when I insert it on the chart.

On the other hand it works well on C++.

Please point out my mistake.

Hi buffett1930,

1. Please edit and use SRC button to post your code. It will make your code much easier to read.

2. Did you mean you can compile on both MQL4 and C++, but it takes forever for MQL4 code to show the calculation ?

If that's what you mean I think this is the cause : MQL (MQL4 and MQL5) are executed by MetaTrader while dll is executed by your OS, and this code below is considered heavy for MT : a loop within a loop.

for(i = 0; i <= no_arrayA - 1; i++)                // <<== main loop
   {
   k = 0;
   j = 0;

   while(j <= no - 1 - 1)                          // <<== first loop within main loop
      {
      for(c = 1; c <= arrayA[i]; c++)              // <<== second loop within first and main loop
         {
         result[i][j] = arrayB[j] - arrayC[i][k];
         j++;
         }
      k++;
      }
   }

been there done that

 
onewithzachy:

Hi buffett1930,

1. Please edit and use SRC button to post your code. It will make your code much easier to read.

2. Did you mean you can compile on both MQL4 and C++, but it takes forever for MQL4 code to show the calculation ?

If that's what you mean I think this is the cause : MQL (MQL4 and MQL5) are executed by MetaTrader while dll is executed by your OS, and this code below is considered heavy for MT : a loop within a loop.

been there done that


Thank you so much for the reply, onewithzachy.

And thank you for the advice to use SRC button too.

Regarding to No.2 on your message you are exactly right.

I can compile on both C++ and MQL4.

Moreover on C++ I can run whole the program including this function.

Though you say the code of this function is too hevy for MT4, in my program the complex roop is very usual.

Then until now all the programs work without any problems.

In this program too MT4 has calcuraled another function just before this function as below.

void Average(int no, int arrayA[], int no_arrayA, double arrayB[], double &result[][])
{
// Sum
        int i, j, k, c;
        double d = 0;

        k = 0;
        for(i = 0; i <= no_arrayA - 1; i++)
        {
                for(j = 0; j <= (no - 1) / arrayA[i] - 1; j++)
                {
                        for(c = 1; c <= arrayA[i]; c++)
                        {
                                d += arrayB[k];
                                k += 1;
                        }
                        result[i][j] = d;
                        d = 0;
                }
                k = 0;
        }

// Average
        for(i = 0; i <= no_arrayA - 1; i++)
        {
                for(j = 0; j <= (no - 1) / arrayA[i] - 1; j++)  // L80
                {
                        result[i][j] = result[i][j] / arrayA[i];
                }
        }
}

int start()
{
:
:
:
        double arrayC[][];
        Average(no, arrayA, no_arrayA, arrayB, arrayC);
:
:
:

So I think I may have a mistake.

Or MQL managemes multidimentional arrays on a kind of way which I do not know?

 

Hi buffet1930,

I search the forum (and maybe I didn't search enough :) ) and so far I can only find this https://docs.mql4.com/basis/variables/formal which tell that array can be passed by reference.

I play a little with array and I think there's no problem in passing array by reference as long as it's a static array - see code below.

I haven't look at your code entirely.

int start()
  {   
   double a [3][3], // print correctly unless we use dynamic array like a[][] or a[][3] or a[3][] - lol
   b[3] = {100, 200, 300};
  
   func (a, b);

   for (int count_1 = 0 ; count_1 < 3; count_1 ++)
      {
      for (int count_2 = 0 ; count_2 < 3; count_2 ++)
         {
         Alert (" Array "+count_1+" & "+count_2+" = "+a[count_1][count_2]); // print correctly if static array
         }
      } 

   PlaySound ("wait.wav");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void func (double &x[][], double y[])  // passing by reference
  {
  Alert ("Func array start");
  for (int count_1 = 0 ; count_1 < 3; count_1 ++)
    {
    for (int count_2 = 0 ; count_2 < 3; count_2 ++)
       {
       x [count_1][count_2] = y[count_1] + (count_1*10) + count_2;
       }
    } 
  }
 
onewithzachy:

Hi buffet1930,

I search the forum (and maybe I didn't search enough :) ) and so far I can only find this https://docs.mql4.com/basis/variables/formal which tell that array can be passed by reference.

I play a little with array and I think there's no problem in passing array by reference as long as it's a static array - see code below.

I haven't look at your code entirely.

I think you can also resize the array to your requirement within the function.
 
RaptorUK:
I think you can also resize the array to your requirement within the function.

Yep, I've read some like this https://www.mql5.com/en/forum/139964.

 

Thank you so much guys!

I know we can pass the multidimention array by reference.

So why the MQL4 starts forever considering at my Func function though it can work on Average function?

Where is my mistake?

I have spent almost a month for finding it.

Please advise me!

 

I could find something??

The program might not work at all from the first.

The following is the first step of the program on which I would get the common logarithm.

However today I found the Log10 function always returned 0 only.

Why?

I do not think MQL cannot calculate 0.xxxxxxx * 0.xxxxxxxx.

So where is my mistake?

void Log10(double array[], int no, double &result[])
{
        int i;

        for(i = 0; i <= no - 1; i++)
        {
                result[i] = MathLog(array[i]) * 0.434294;
        }
}

int start()
{
:
:
:
        double log10[];
        Average(price, Period, log10);
:
:
:
	print(Mathlog(price[i]),",",log10[i]);
:
:
:
 
buffett1930:

I could find something??

The program might not work at all from the first.

The following is the first step of the program on which I would get the common logarithm.

However today I found the Log10 function always returned 0 only.

Why?

I do not think MQL cannot calculate 0.xxxxxxx * 0.xxxxxxxx.

So where is my mistake?

The problem is your log10 array . . . it has a size of 0 elements . . .

Try adding this . . .

void Log10(double array[], int no, double &result[])
{
   int i;

   ArrayResize(result, no);      //  <----  add this line

   for(i = 0; i <= no - 1; i++)
   {
      result[i] = MathLog(array[i]) * 0.434294;
   }
}
 
RaptorUK:

The problem is your log10 array . . . it has a size of 0 elements . . .

Try adding this . . .


RaptorUK, you are great!

Your pointing is very basic but extremly important and helped me.

Thanks a lot.

However I think I still have a problem on resize the 2 dimentional array.

I will try something on it.

Please keep touching me.

 
buffett1930:


Please keep touching me.

Thanks for the invitation but I will politely decline ;-)


I will keep an eye on this thread though.