I am trying to write a function which can take array of any dimension and print values in the array successfully. But I am not able to move forward because we have to declare all the dimensions except left most one when declaring function. Is there any possibility that we can write a generalised function which can take array as input for any dimensions?
for example, the function should be able to take 2 dimensional array or 3 dimensional array or n dimensional array where n is any number.
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.
More than Three Dimensions Although an array can have as many as 32 dimensions, it is rare to have more than three. When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.
Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays.
Using recursion for each dimension and template (so in C++), 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;
}
Example usage:
int a[2][3][4];
print(a);
Live example
If you code you array as one-dimensional and then calculate the single index yourself, you can certainly have a program act as though the array was made for a variable number of dimensions.
My initial though on how to do that would be to start with a vector containing the extent of each dimension you intend to use.
The number of elements in that vector would be the number of dimensions you have.
An array is passed to a function as a pointer to the type of the array elements, regardless of the dimensionality of the array. You could have further arguments to specify the number of dimensions, n, and an array (another one) of length n specifying the number of elements in each dimension. Note that the [] notation is simply a tidy way of performing pointer addition.
If you want to access particular element or operate on the array but if you want to create the matrix dynamically, you can use use pointers to access each element by passing the dimensions in the print function.
Since if you have a multidimensional array defined as int [][]
, then
x = y[a][b]
is equivalent to x = *((int *)y + a * NUMBER_OF_COLUMNS + b);
Check this post for more details: How to use pointer expressions to access elements of a two-dimensional array in C?
So, if you want to print whole matrix or access any particular element, you can do like:
#include <iostream>
using namespace std;
//the function print_2D_matrix receives 4 arguments: pointer to first element
// dimension of array arr, i.e. n x m
// index of the element to be printed, i.e. 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, i.e. (a - 1) * NO_OF_COLUMNS
//then go to the address on b column, i.e. (a - 1) * NO_OF_COLUMNS + b
//since we started from the base address, i.e. 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;
}
Output for above program (for n x m = 4 x 5) is:
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
arr[3][3] = 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With