Statically Declared 2-D Array C++ as Data Member of a Class

Statically declared 2-D array C++ as data member of a class

class Array2D {
public:
vector<int> v;
int nc;
Array2D(int NR, int NC) : v(NR*NC), nc(NC) {}
int* operator[](int r) { return &v[r*nc]; }
};

int main()
{
Array2D array2d(2, 3);
array2d[0][0] = 1;
array2d[1][2] = 6;
}

This allows you to create a class that will function like a 2D array. It's fast and the data is contiguous.

What is the right way to work 2 static 2d arrays in C?

If your compiler supports variable length arrays you can declare the function like

void PrintStaticMatrix( int rows, int cols, int matrix[rows][cols] );

or

void PrintStaticMatrix( int rows, int cols, int matrix[][cols] );

or

void PrintStaticMatrix( int rows, int cols, int ( *matrix )[cols] );

Another approach is to use dynamically allocated arrays as for example

int **matrix = malloc( rows * sizeof( int * ) );
for ( int i = 0; i < rows; i++ )
{
matrix[i] = malloc( cols * sizeof( int ) );
}

And declare the function like

void PrintStaticMatrix( int **matrix, int rows, int cols );

One more approach is reinterpret a two-dimensional array as a one-dimensional array. In this case the function will look like

void PrintStaticMatrix( int *matrix, int rows, int cols );

And a function call can look like

PrintStaticMatrix( ( int * )matrix, rows, cols );

Within the function you will deal with a one-dimensional array. Using an appropriate expression as an index you will be able to simulate a two-dimensional array for example in for loops.

Otherwise you will need two separate functions like

void PrintStaticMatrix1( int matrix[][M], int rows );

and

void PrintStaticMatrix2( int matrix[][N], int rows );

How to correctly initialize a 2D array in a called class

In your constructor, you are doing:

int[,] EdgeList = new int[(arrayLength * arrayLength), 2];

which creates a new (local) variable with the same name as the field. Instead you should do:

this.EdgeList = new int[(arrayLength * arrayLength), 2];

You could omit the this, but it can prevent you from making this mistake again.

Further, you should change the field declaration to

public int[,] EdgeList

Then you can set individual fields in the array via:

 EdgeList[i,j] = value; 

Initialize large two dimensional array in C++

Any part of an array which is initialized, that is beyond the initialization, is initialized to 0. Hence:

int const A::test[10][10];           // uninitialized

int const A::test[10][10] = { {0} }; // all elements initialized to 0.

int const A::test[10][10] = {1,2}; // test[0][0] ==1, test[0][1]==2, rest==0

That means all you have to initialize is up to the last non-zero:

int const A::test[10][10] = { 
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 7, 7, 7, 7, 0, 0, 0},
{0, 0, 0, 7, 7, 7, 7, 0, 0, 0},
{0, 0, 0, 7, 7, 7, 7, 0, 0, 0},
{0, 0, 0, 7, 7, 7, 7, 0, 0, 0}
};

It is not the best solution, but will save some work.



Related Topics



Leave a reply



Submit