Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create an n-dimensional array in c

I was thinking of writing a function that takes n parameters and returns an n-dimensional array using those parameters as the dimensions. Now i realize an one-d and 2d array is easy to implement with pointers. For 2d array the snippet would be something like (standard way) :

int** x;
int* temp;

x = (int**)malloc(m * sizeof(int*));
temp = (int*)malloc(m*n * sizeof(int));
for (int i = 0; i < m; i++) {
  x[i] = temp + (i * n);
}

where the array is of size m*n; But the problem lies how do we find the nested loop parameters for a n-dimensional array? Is there any way to optimize the code?

like image 833
shortCircuit Avatar asked Nov 09 '13 21:11

shortCircuit


People also ask

Can arrays be N dimensional?

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape , which is a tuple of N non-negative integers that specify the sizes of each dimension.

Can you have 3 dimensional array in C?

You can think the array as a table with 3 rows and each row has 4 columns. Similarly, you can declare a three-dimensional (3d) array.

How do you create a 3 dimensional array?

A three dimensional means we can use nested levels of array for each dimension. To create a 3-dimensional numpy array we can use simple numpy. array() function to display the 3-d array.


2 Answers

This shows how to create an N-dimensional array and how to index its elements. These provide the basic mechanisms needed. This is something students consider when learning, but it is rarely used in practice. There are usually better ways to organize data structures. Additionally, most useful algorithms would have patterns in how they traverse the data, so it would be better to build code that updates indices efficiently in an incremental manner rather than recalculating them from scratch as shown below.

/*  Note:  For demonstration purposes only.  Depending on needs, other types
    might be used for indices and sizes, and the array type might be wrapped
    in an opaque struct rather than exposed as "int *".
*/


//  Create an array with N dimensions with sizes specified in D.
int *CreateArray(size_t N, size_t D[])
{
    //  Calculate size needed.
    size_t s = sizeof(int);
    for (size_t n = 0; n < N; ++n)
        s *= D[n];

    //  Allocate space.
    return malloc(s);
}

/*  Return a pointer to an element in an N-dimensional A array with sizes
    specified in D and indices to the particular element specified in I.
*/
int *Element(int *A, size_t N, size_t D[], size_t I[])
{
    //  Handle degenerate case.
    if (N == 0)
        return A;

    //  Map N-dimensional indices to one dimension.
    int index = I[0];
    for (size_t n = 1; n < N; ++n)
        index = index * D[n] + I[n];

    //  Return address of element.
    return &A[index];
}

Example of use:

//  Create a 3*3*7*7*9 array.
size_t Size[5] = { 3, 3, 7, 7, 9 };
int *Array = CreateArray(5, Size);

//  Set element [1][2][3][4][5] to -987.
*Element(Array, 5, Size, (size_t []) { 1, 2, 3, 4, 5 }) = -987;
like image 63
Eric Postpischil Avatar answered Oct 19 '22 07:10

Eric Postpischil


I prefer not to use multidimensional arrays, use 1D instead:

int* pArray = (int*) malloc(m * n * sizeof(int*));

// access pArray[a][b]
int result = pArray[a*m + b];
like image 23
Hhyperion Avatar answered Oct 19 '22 06:10

Hhyperion