Page **1** of **1**

### Multidimensional Array

Posted: **September 30th, 2019, 10:26 am**

by **aslanbey0158**

I have a matrix like a[3][3] (float) ,

void function(float a[][],b[])

i can't send this matrix into funtion. How can i send matrix to function

### Re: Multidimensional Array

Posted: **September 30th, 2019, 11:21 am**

by **Yumtard**

I would've made a matrix class. Else you could use vectors.

If it's stricly 3x3 matrices you can also do this

void function(float a[3][3])

### Re: Multidimensional Array

Posted: **September 30th, 2019, 12:04 pm**

by **aslanbey0158**

But function doesn't take matrix,it want vector.

The problem is this.

### Re: Multidimensional Array

Posted: **October 1st, 2019, 10:44 am**

by **chili**

The problem, is *this*.

### Re: Multidimensional Array

Posted: **October 1st, 2019, 12:08 pm**

by **aslanbey0158**

i wrote my function in main class;it works well.

İ wanted to throw this function to

void function( float a[][],float b[]) <----

function doesnt take matrice ,it wants vector

How can i solve this problem?

### Re: Multidimensional Array

Posted: **October 1st, 2019, 1:18 pm**

by **Yumtard**

Just rewrite the signature, I don't understand your problem.

If you want vectors then something beautiful like this:

void function( const std::vector<std::vector<int>>& a, const std::vector<int>& b )

or create a matrix class and just change the signature so it takes a matrix instead.

If you need more help, please post all the code

### Re: Multidimensional Array

Posted: **October 1st, 2019, 5:27 pm**

by **chili**

You need to read up on pointer decay of arrays.

https://stackoverflow.com/questions/146 ... y-decaying
you don't pass arrays by value, they become pointers.

If you wanna avoid that, use std::array<std::array>, or pass the array by reference. Passing by reference will lock you down to a specific dimensionality, but you can use templating to make your life easier in that regard. You can do a nested vector as well. You can pass as a flat array (i.e. as a pointer, remember above), and then do the index calculation yourself. You can create your own custom type that does one of these things and encapsulates the implementation away. You can do so many things. So many options.

### Re: Multidimensional Array

Posted: **October 4th, 2019, 6:16 am**

by **albinopapa**

As stated by chili

void function( float arr[3][3] ){ }

float matrix[3][3];

function( matrix );

is the same as

void function( float* arr ){};

function( matrix ) // still works

void function( float(&arr)[3][3] ){ }

Now the function ONLY takes in 2D C-arrays with 3 elements per dimension, but you are actually passing in the "matrix" and can return values with it.

If you are needing nested std::vector, then do what Yumtard said.

void function( std::vector<std::vector<float>>& _matrix );

Or, as chili pointed out, you can go from a flattened 1D array to 2D array with a bit of math:

for(int i = 0; i < 9; ++i ){

int column = i % 3;

int row = i / 3;

}

or from 2D array to 1D array

int index = column + ( row * num_columns );

Here's a partial Matrix class that might be useful.

Code: Select all

```
class Matrix
{
public:
Matrix()=default;
Matrix( float _00, float _01, float _02,
float _10, float _11, float _12,
float _20, float _21, float _22 );
float operator()( int Column, int Row )const
{
return elements[Row][Column];
)
float& operator()( int Column, int Row )
{
return elements[Row][Column];
}
float elements[3][3];
};
Matrix Identity( ){
Matrix matrix;
for( int j = 0; j < 3; ++j )
{
for( int i = 0; i < 3; ++i )
{
if( i == j )
matrix( i, j ) = 1.f;
else
matrix( i, j ) = 0.f;
}
}
return matrix;
}
```