By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

how to initialize n-dimensional matrix ?

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 n-dimensional 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
Aug 15 '07 #1
Share this Question
Share on Google+
12 Replies


Ganon11
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 n-dimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3-dimensional, 4-dimensional, etc)?
Aug 15 '07 #2

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 n-dimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3-dimensional, 4-dimensional, etc)?
n*n is a 2d matrix, what I am looking for is a n-dimesional matrix, Was thinking of recursively allocating it... but couldnt figure out how to do that.
Aug 16 '07 #3

ilikepython
Expert 100+
P: 844
n*n is a 2d matrix, what I am looking for is a n-dimesional 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.
Aug 16 '07 #4

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.
Aug 16 '07 #5

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.
Aug 16 '07 #6

P: 7
aint any experts interested to show me path ??

or is it so naive a problem to be asked in this forum ???
Aug 16 '07 #7

Ganon11
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:

Expand|Select|Wrap|Line Numbers
  1. n  |  Number of ints
  2. --------------------
  3. 1  |  2
  4. 2  |  4
  5. 3  |  8
  6. 4  |  16
  7. ...|...
  8. n  |  2^n
So the function needs to allocate 2^n * sizeof(int) memory. Is this any help?
Aug 16 '07 #8

Banfa
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

Expand|Select|Wrap|Line Numbers
  1. int *data = malloc((1<<n) * sizeof *data);
  2.  
C++ helps a bit by removing the need to calculate the data size

Expand|Select|Wrap|Line Numbers
  1.     int *data = new int[(1<<n)];
  2.  
Aug 16 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
int *data = new int[(1<<n)];
Unfortunately, this creates a one dimensional array.

A 2x2x2 is declared as:

Expand|Select|Wrap|Line Numbers
  1. int arr[2][2][2];
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[2][2]  = new int[2][2][2];
  2.  
and you have your array.
Aug 16 '07 #10

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:

Expand|Select|Wrap|Line Numbers
  1. n  |  Number of ints
  2. --------------------
  3. 1  |  2
  4. 2  |  4
  5. 3  |  8
  6. 4  |  16
  7. ...|...
  8. 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 "n-dimesional 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 2-d array but I cant access this memory with p[i][j]
I will need some more logic to do that.
Aug 16 '07 #11

P: 7
Unfortunately, this creates a one dimensional array.

A 2x2x2 is declared as:

Expand|Select|Wrap|Line Numbers
  1. int arr[2][2][2];
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[2][2]  = new int[2][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 n-dimensional 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 10-d matrix
Aug 16 '07 #12

Banfa
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.
Aug 16 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.