Multidimensional Array

 Posts: 31
 Joined: April 15th, 2017, 10:48 am
Multidimensional Array
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
void function(float a[][],b[])
i can't send this matrix into funtion. How can i send matrix to function
Re: Multidimensional Array
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])
If it's stricly 3x3 matrices you can also do this
void function(float a[3][3])

 Posts: 31
 Joined: April 15th, 2017, 10:48 am
Re: Multidimensional Array
But function doesn't take matrix,it want vector.
The problem is this.
The problem is this.

 Posts: 31
 Joined: April 15th, 2017, 10:48 am
Re: Multidimensional Array
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?
İ 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
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
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
You need to read up on pointer decay of arrays. https://stackoverflow.com/questions/146 ... ydecaying
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.
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.
Chili

 Posts: 4039
 Joined: February 28th, 2013, 3:23 am
 Location: Oklahoma, United States
Re: Multidimensional Array
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 Carrays 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.
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 Carrays 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;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves.  gameprogrammingpatterns.com