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

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


I will keep an eye on this thread though.

I mean it!
 
onewithzachy:

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


My poor brain cannot understand this following code which is shown by WHRoeder in the link above.

int dim2; double arr[];
void   setDim(int d1, int d2){ dim2=d2; arrayResize(arr, d1*d2); }
void   setArr(int d1, int d2, double v){ arr[d1*dim2+d2] = v;    }
double getArr(int d1, int d2){ return( arr[d1*dim2+d2] );        }
:
setDim(2,3); // looks like array[2][3]
setArr(1,2,v) // looks like array[1][2]=v

Please someone explain it.

 

Explain me what you are doing

I see ArrayA[ ] is type integer

ArrayB[ ] is log10

and there is an ArrayC [ ] [ ] type double

and with some calculation you get result [ ] [ ]

if i see some example with result maybe i can help

 

Hi, it has been a long time, bright advisers here!

I finally gave up using 2 dimentional array.

I think it is not so useful on MQL as long as we cannot resize it.

So have rebuilt whole the program with single dimentional arrays as below for example.

A kind of virtual multi dimention........

How inconvenient!

Anyway, thank you all for kind advisings!

int Average(int no_elements, int arrayA[], int no_arrayA, double arrayB[], double &result[])
{
// Sum
        int h, i, j, k, c;
        double d = 0;
        ArrayResize(result, no_elements * 0.5 * no_arrayA);

        h = 0;
        k = 0;
        for(i = 0; i <= no_arrayA - 1; i++)
        {
                for(j = 1; j <= (Period - 1) / arrayA[i]; j++)
                {
                        for(c = 1; c <= arrayA[i]; c++)
                        {
                                d += arrayB[k];
                                k++;
                        }
                        result[h] = d;
                        d = 0;
                        h++;
                }
                result[h] = 1000000;         // MAKING VIRTUAL DIMENTION
                h++;
                k = 0;
        }

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

        return(h);
}
 
buffett1930:

Hi, it has been a long time, bright advisers here!

I finally gave up using 2 dimentional array.

I think it is not so useful on MQL as long as we cannot resize it.

So have rebuilt whole the program with single dimentional arrays as below for example.

A kind of virtual multi dimention........

How inconvenient!

Anyway, thank you all for kind advisings!

You might want to change this line, I'm not 100% sure, but what you have may give you some unexpected type casting issues . . .

From . . .

ArrayResize(result, no_elements * 0.5 * no_arrayA);

to . . .

ArrayResize(result, ( no_elements * no_arrayA ) / 2 );


ArrayResize() takes an int type for it's new_size parameter. Dividing by 2 instead of multiplying by 0.5 keeps all the values in the calculation as ints.

 
RaptorUK:

You might want to change this line, I'm not 100% sure, but what you have may give you some unexpected type casting issues . . .

From . . .

to . . .


ArrayResize() takes an int type for it's new_size parameter. Dividing by 2 instead of multiplying by 0.5 keeps all the values in the calculation as ints.


Thanks, RaptorUK!

You always help me.

Actually I have never been aware that mistake because I have always put even numbers as the no_elements.

Anyway I will post a new topic.

I will appreciate you replying on it too.

 
buffett1930:


Thanks, RaptorUK!

You always help me.

Actually I have never been aware that mistake because I have always put even numbers as the no_elements.

That isn't the potential issue . . . you are doing an int calculation and have a double in the middle of it . . . 0.5 is a double not an int
Reason: