Is it possible to write a function that can take an array of n dimensions?

I am trying to write a function that can take an array of any dimension and print the values ​​in the array successfully. But I can’t move forward, because when declaring a function, we must declare all dimensions except the leftmost one. Is there a chance that we can write a generalized function that can take an array as an input for any measurements?

for example, a function should be able to accept a 2-dimensional array or a 3-dimensional array or an n-dimensional array, where n is any number.

+6
source share
5 answers

Using recursion for each dimension and template (therefore in C ++) the following may help:

template <typename T> void print(const T&e) { std::cout << e << " "; } template <typename T, std::size_t N> void print(const T (&a)[N]) { std::cout << "{"; for (const auto& e : a) { print(e); } std::cout << "}" << std::endl; } 

Usage example:

 int a[2][3][4]; print(a); 

Living example

+11
source

If you encode the array as one-dimensional, and then calculate the single index yourself, you can probably have a program as if the array were created for a variable number of dimensions.

First, I would like to start with a vector containing the size of each dimension that you are going to use.

The number of elements in this vector will be the number of sizes you have.

+3
source

The array is passed to the function as a pointer to the type of array elements, regardless of the dimension of the array. You can have additional arguments to indicate the number of dimensions, n and an array (another) of length n, which determines the number of elements in each dimension. Note that the [] notation is just a neat way to do the addition of a pointer.

+3
source

If you want to access a specific element or work with an array, but if you want to dynamically create a matrix, you can use usage pointers to access each element by passing measurements to the print function.

Since if you have a multidimensional array defined as int [][] , then x = y[a][b] equivalent to x = *((int *)y + a * NUMBER_OF_COLUMNS + b);

Check this post for more information: How to use pointer expressions to access elements of a two-dimensional array in C?

So, if you want to print the entire matrix or access a specific element, you can do this:

 #include <iostream> using namespace std; //the function print_2D_matrix receives 4 arguments: pointer to first element // dimension of array arr, ie nxm // index of the element to be printed, ie a and b void print_2D_matrix(int *arr, int n, int m, int a, int b){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++) printf("%d ", *(arr + (i * m) + j)); printf("\n"); } //go to the address just before a row, ie (a - 1) * NO_OF_COLUMNS //then go to the address on b column, ie (a - 1) * NO_OF_COLUMNS + b //since we started from the base address, ie first element( arr[0][0] ), subtract 1 printf("arr[3][3] = %d\n", *(arr + ((a - 1) * m) + b - 1)); //print arr[a][b] } int main() { int n, m; cin>>n>>m; int arr[n][m]; for(int i = 0; i < n; i++) //initialize the matrix for(int j = 0; j < m; j++) arr[i][j] = i * j; print_2D_matrix((int *) arr, n, m, 3, 3); return 0; } 

The output for the program above (for nxm = 4 x 5):

 0 0 0 0 0 0 1 2 3 4 0 2 4 6 8 0 3 6 9 12 arr[3][3] = 4 
+3
source

I am sure that this violates at least one rule of the C standard, but it should work in practice. Note that it uses 0 as a control value for the final element of any level of the array.

 void print(void* p, int dim) { if (dim == 1) { int* a = (int*) p; while (*a) { printf("%d ", *a++); } printf("\n"); } else { void** a = (void**)p; while (*a) { print(*a++, dim - 1); } } } void test() { int x0 [] = { 11, 12, 13, 14, 15, 0 }; int x1 [] = { 21, 22, 23, 0 }; int x2 [] = { 0 }; int x3 [] = { 41, 42, 0 }; int x4 [] = { 51, 52, 53, 0 }; int* y0 [] = { x0, x3, 0 }; int* y1 [] = { 0 }; int* y2 [] = { x1, x2, x4, 0 }; int** z [] = { y0, y1, y2, 0 }; print(z, 3); } 

Print

 11 12 13 14 15 41 42 21 22 23 51 52 53 
0
source

Source: https://habr.com/ru/post/975552/


All Articles