Why in the world can't I initialize a multidimensional array with variables?

trevor88  

Seriously guys... why?

I have a complex EA and I can't manually write the Array rows and columns every fricking time. It would mean stopping the program and run it again for like 10 times. It is not possibile.

I would like to pass those values in a function like this:


void create_array( int rows, int columns)
{
        double arr[rows][columns];
        ...
        ...
        ...
        
}

All I was able to do was to "ArrayResize" the first dimension, but i need both of the dimensions to be initialized automatically.

I couldn't find nothing online...

Can you pls help me?

Thank you.

Tobias Johannes Zimmer  
trevor88:

Seriously guys... why?

I have a complex EA and I can't manually write the Array rows and columns every fricking time. It would mean stopping the program and run it again for like 10 times. It is not possibile.

I would like to pass those values in a function like this:


All I was able to do was to "ArrayResize" the first dimension, but i need both of the dimensions to be initialized automatically.

I couldn't find nothing online...

Can you pls help me?

Thank you.

And why is it that you can't pass the variables like that? I am not sure what the problem is, something about multidimensional arrays and initialisation...
trevor88  
pennyhunter #:
And why is it that you can't pass the variables like that? I am not sure what the problem is, something about multidimensional arrays and initialisation...
are u seriuous? Try code it for yourself and see... it is not possible. Incredibile, I know.
Alexandre Borela  
trevor88 #:
are u seriuous? Try code it for yourself and see... it is not possible. Incredibile, I know.

The second dimension must always be defined which is probably not what you want since it looks like you want a dynamic matrix.

A solution to this is to use a single dimension dynamic array and do a bit of calculation when accessing specific cells of
the matrix, for example.

double matrix[];

// 4X3 matrix (4 rows, 3 columns).
ArrayResize(matrix, 12);
ArrayInitialize(matrix, 0);

int row = 2; // Row goes from 0 - 3
int column = 0; // Column goes from 0 - 2

// Access the first cell in the 4th row.
matrix[row * 3 + column] = 123;

Ideally you could put all this logic inside a class and overload the operator[] to do all these calculations behind the scenes.
William Roeder  
Or, create a struct/class with a dynamic array. Then create an array of them.
Tobias Johannes Zimmer  
trevor88 #:
are u seriuous? Try code it for yourself and see... it is not possible. Incredibile, I know.
If I only knew what you are trying to achieve but I won't guess if you won't tell. I am helping you by clariying what is your problem. Then at least it will be possible to help you.

Your original post left me with question marks and it is exausting to find out what you meant with your rambling and this unconclusive snippet of code. It says nothing, seriously.
trevor88  
Alexandre Borela #:

The second dimension must always be defined which is probably not what you want since it looks like you want a dynamic matrix.

A solution to this is to use a single dimension dynamic array and do a bit of calculation when accessing specific cells of
the matrix, for example.


Ideally you could put all this logic inside a class and overload the operator[] to do all these calculations behind the scenes.

I can't do this... you see I am working with excel databases... columns and rows are very large and there are also multiple nested loops in the code which work properly only in a matrix array logic.

... I really don't know what to do....


fun fuct: theoretically you could insert "constants" into the matrix declaration. But of course you can't assing variabiles to them. So you always need to type those numbers. The more I think of it, the crazier it gets


#define Columns 66
#define Rows 55

double arr[Rows][Columns];
Dominik Christian Egert  
Traverse your excel to json and use the json from code base to access your data.

Maybe that helps
trevor88  
Dominik Christian Egert #:
Traverse your excel to json and use the json from code base to access your data.

Maybe that helps

I can access my data... the only thing I can't do is create a multidimensional array automatically. I have to type the values.

Dominik Christian Egert  
Why don't you go with the solution in post 3?

To me it seems a way to go.