P: 7

hi everybody,
I am struggling to write a function which takes an int n as parameter, and inside the function I need to initilize a ndimensional matrix and return the pointer to the initialized matrix.
If you know any online sources where I can read through the related matter then do care to provide me the same. As its been a long time since I am struggling with this problem and so far I havent been able to find a convincing answer.
Thanks
 
Share this Question
Expert 2.5K+
P: 3,652

What you will have to do is allocate a piece of memory large enough to hold the elements, then return the address of that memory location. By ndimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3dimensional, 4dimensional, etc)?
 
P: 7

What you will have to do is allocate a piece of memory large enough to hold the elements, then return the address of that memory location. By ndimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3dimensional, 4dimensional, etc)?
n*n is a 2d matrix, what I am looking for is a ndimesional matrix, Was thinking of recursively allocating it... but couldnt figure out how to do that.
  Expert 100+
P: 844

n*n is a 2d matrix, what I am looking for is a ndimesional matrix, Was thinking of recursively allocating it... but couldnt figure out how to do that.
Are you using C or C++? In C you coutld use malloc and in C++ you could use the new operator.
 
P: 7

Let me further clarify things ..
Whatever I pass the value of n in the function, I want to initialize a matrix of that dimension in the function.
Like if the value of the n is 2 then it should be 2*2 matrix
if the value of n is 3 then 2*2*2 matrix
for n=4 it should be 2*2*2*2 matrix and so on ....
I hope this explanation helps the experts for guiding me.
 
P: 7

Are you using C or C++? In C you coutld use malloc and in C++ you could use the new operator.
C or C++ doesnt make any difference here.
Its the logic I am struggling with not with the incompetencies of new or malloc.
 
P: 7

aint any experts interested to show me path ??
or is it so naive a problem to be asked in this forum ???
  Expert 2.5K+
P: 3,652

The problem probably isn't that the question is a bad question, but that you are asking too much of us. Your past three responses have been within an hour of each other  experts come here to volunteer their time when they can, not at your bidding. Please allow a little more time between responses for people from around the world to try to help.
As for the question itself, let's try to find a pattern. Let n be the argument you pass. Then if n = 1, you need an array of length 2, so enough memory for 2 ints must be allocated. If n = 2, you need a 2x2 matrix  enough room for 4 ints. Following this pattern, we get the following chart:  n  Number of ints



1  2

2  4

3  8

4  16

......

n  2^n
So the function needs to allocate 2^n * sizeof(int) memory. Is this any help?
  Expert Mod 5K+
P: 8,916

C or C++ doesnt make any difference here.
Its the logic I am struggling with not with the incompetencies of new or malloc.
The is very little logic to it calculate the number of entries in the matrix, a 2x2 matrix has 4 entries, a 2x2x2 matrix has 8 entries, in fact it is
pow(2, n);
or
1 << n;
Then in C malloc this times the size of each entry so asssuming ints 
int *data = malloc((1<<n) * sizeof *data);

C++ helps a bit by removing the need to calculate the data size 
int *data = new int[(1<<n)];

  Expert Mod 5K+
P: 9,197

int *data = new int[(1<<n)];
Unfortunately, this creates a one dimensional array.
A 2x2x2 is declared as:
This array has two elements, not eight. It is the first index that determines the number of elements in the array, In this case, two. The other indexes just describe the elements. In this case, each element is an array of two int arrays of two int.
Next, in C and C++, the name of the array is the address of element 0. In this case, arr is the address of its element 0. Therefore, the name arr is the address of an array of two elements where each element is an array of two int.
That is, you need a pointer to a 2x2 array of int.
You declare this array as you normally do. You use new and ask for the number of 2x2 arrays that you want (in this case 2) and you assign the address returned from new to a pointer to a 2x2 array of int: 
int (*ptr)[2][2] = new int[2][2][2];

and you have your array.
 
P: 7

The problem probably isn't that the question is a bad question, but that you are asking too much of us. Your past three responses have been within an hour of each other  experts come here to volunteer their time when they can, not at your bidding. Please allow a little more time between responses for people from around the world to try to help.
As for the question itself, let's try to find a pattern. Let n be the argument you pass. Then if n = 1, you need an array of length 2, so enough memory for 2 ints must be allocated. If n = 2, you need a 2x2 matrix  enough room for 4 ints. Following this pattern, we get the following chart:  n  Number of ints



1  2

2  4

3  8

4  16

......

n  2^n
So the function needs to allocate 2^n * sizeof(int) memory. Is this any help?
Dude I will request you to relax a little and think a little.
I agree the function need to allocate 2^n * sizeof(int) memory, but how to do that so that I will have "ndimesional matrix" interface.
I am not concerned here with the memory layouts, but with the interface.
int *p = (int *)malloc(2*2*sizeof(int))
is valid for a 2d array but I cant access this memory with p[i][j]
I will need some more logic to do that.
 
P: 7

Unfortunately, this creates a one dimensional array.
A 2x2x2 is declared as:
This array has two elements, not eight. It is the first index that determines the number of elements in the array, In this case, two. The other indexes just describe the elements. In this case, each element is an array of two int arrays of two int.
Next, in C and C++, the name of the array is the address of element 0. In this case, arr is the address of its element 0. Therefore, the name arr is the address of an array of two elements where each element is an array of two int.
That is, you need a pointer to a 2x2 array of int.
You declare this array as you normally do. You use new and ask for the number of 2x2 arrays that you want (in this case 2) and you assign the address returned from new to a pointer to a 2x2 array of int: 
int (*ptr)[2][2] = new int[2][2][2];

and you have your array.
I think you are getting what I am trying to achieve.
Your explanation of arrays and pointers is fine
and it will really help me if you can help to generalize what you have done above
so that I can ndimensional matrix.
what if i pass value 10 to my function then by your method (which I have already given thought to) I need to have some 9 nested loops to initialize a 10d matrix
  Expert Mod 5K+
P: 8,916

Unfortunately, this creates a one dimensional array.
Not unfortunately, purposefully.
Reading mesalcut post immediately before this it is not possible to generalise the problem to produce a function that allocates the correct data and returns a pointer of the right type for any value of n given at run time (for a value N given at compile time I would suggest a template class).
In this case I would fall back to a single dimensioned array and handle calculating the required index for a given cell in the matrix explicitly.
Unless you can think of a way for a function to take an integer n and return an allocated array with that many dimensions.
The problem boils down to you can't have a type of a function return or variable that is dynamic at run time, it must be fixed when the code is compiled. In this case it needs to be fixed at the lowest common denominator which is a single dimensioned array.
    Question stats  viewed: 3672
 replies: 12
 date asked: Aug 15 '07
