How can i create an n-dimensional array in c

shortCircuit

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?

Eric Postpischil

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create an array of pointers point to a 2 dimensional array

From Dev

How can I create a two-dimensional dynamic array in C#?

From Dev

How can I create a two-dimensional array containing ArrayLists?

From Java

How can I create a two dimensional array in JavaScript?

From Dev

How can I create a higher dimensional table?

From Dev

How can I Create an array with N zeros?

From Dev

How i can sort 2 dimensional array?

From Dev

How can I create an n-dimensional grid in numpy to evaluate a function for arbitrary n?

From Dev

How can I create a simple 4x3 two dimensional array in Java?

From Dev

How can I create two dimensional SplitPane in Java Swing

From Dev

How to create a two dimensional array of given size in C++

From Dev

Initializing Error: How to create a multi dimensional zero array in C++

From Java

How can I convert Pair to 2 dimensional array in JAVA?

From Dev

How can I represent a 2-dimensional array in Protocol Buffers?

From Dev

How can I find the longest string in a multi-dimensional array?

From Dev

How can i sort a multi-dimensional array by Number

From Dev

PHP - How can I sort this multi dimensional array by sum value?

From Dev

How can I check if any button of a two dimensional array was clicked?

From Dev

How can I loop over this multi dimensional array?

From Dev

How can I dynamically generate and populate a multi-dimensional array

From Dev

How can I implement 5 dimensional array in one for loop in julia?

From Dev

How i can compare two dimensional array like below?

From Dev

How can I print a one dimensional array as grid in Python?

From Dev

How can I set elements of a two dimensional array in ruby?

From Dev

How can I store an integer two-dimensional array in SharedPreferences?

From Dev

How can I use a single-dimensional array for Roman Numerals?

From Dev

How can I get the length of a two dimensional array in VBA (excel)?

From Dev

How can I print data in multi dimensional array in Python

From Dev

How can I make a 3-dimensional array in python

Related Related

  1. 1

    How can I create an array of pointers point to a 2 dimensional array

  2. 2

    How can I create a two-dimensional dynamic array in C#?

  3. 3

    How can I create a two-dimensional array containing ArrayLists?

  4. 4

    How can I create a two dimensional array in JavaScript?

  5. 5

    How can I create a higher dimensional table?

  6. 6

    How can I Create an array with N zeros?

  7. 7

    How i can sort 2 dimensional array?

  8. 8

    How can I create an n-dimensional grid in numpy to evaluate a function for arbitrary n?

  9. 9

    How can I create a simple 4x3 two dimensional array in Java?

  10. 10

    How can I create two dimensional SplitPane in Java Swing

  11. 11

    How to create a two dimensional array of given size in C++

  12. 12

    Initializing Error: How to create a multi dimensional zero array in C++

  13. 13

    How can I convert Pair to 2 dimensional array in JAVA?

  14. 14

    How can I represent a 2-dimensional array in Protocol Buffers?

  15. 15

    How can I find the longest string in a multi-dimensional array?

  16. 16

    How can i sort a multi-dimensional array by Number

  17. 17

    PHP - How can I sort this multi dimensional array by sum value?

  18. 18

    How can I check if any button of a two dimensional array was clicked?

  19. 19

    How can I loop over this multi dimensional array?

  20. 20

    How can I dynamically generate and populate a multi-dimensional array

  21. 21

    How can I implement 5 dimensional array in one for loop in julia?

  22. 22

    How i can compare two dimensional array like below?

  23. 23

    How can I print a one dimensional array as grid in Python?

  24. 24

    How can I set elements of a two dimensional array in ruby?

  25. 25

    How can I store an integer two-dimensional array in SharedPreferences?

  26. 26

    How can I use a single-dimensional array for Roman Numerals?

  27. 27

    How can I get the length of a two dimensional array in VBA (excel)?

  28. 28

    How can I print data in multi dimensional array in Python

  29. 29

    How can I make a 3-dimensional array in python

HotTag

Archive