MultiDim Array Challenge

S

Salman Khilji

After reading all the FAQs, I cannot solve the following problem:

I have a pointer of type double. I am supposed to

1) Allocate memory for it assuming that the pointer will be pointing
to a multi-dimensional array whose dimensions will be specified at
run-time.

2) Set the individual elements of the array using pointer-arithmetic.

I can do 1) but 2) is hard and requires some recursive logic that I
cannot come up with.

Suppose we are given the dimensions of a multi-array using a 1D array
of length D. For example, [2 3] means that we are dealing with a
multi-array of 2x3 and[2 3 5] would mean the array in question is
2x3x5. Someone calls a function like the following to tell us that.

void setArrayDim(int arrayDim[], int size, int elemSize);

Assming that the multi-array's element type is T.

1) is solved by:

malloc( product of all elements of arrayDim * sizeof(double) );


For 2), we have another function like:

setArrayValue(arrayIndices[], int size, double value);

In this case the arrayIndices array has the indices into which value
must be deposited. We somehow have to come up with an equation that
computes the correct pointer location.

I can do this manually for a 2D array. Maybe a 3D array won't be
hard. But what I need to do is a multi-dimensional array of
dimensions D.
 
W

William L. Bahn

Salman Khilji said:
After reading all the FAQs, I cannot solve the following problem:

I have a pointer of type double. I am supposed to

1) Allocate memory for it assuming that the pointer will be pointing
to a multi-dimensional array whose dimensions will be specified at
run-time.

2) Set the individual elements of the array using pointer-arithmetic.

I can do 1) but 2) is hard and requires some recursive logic that I
cannot come up with.

Suppose we are given the dimensions of a multi-array using a 1D array
of length D. For example, [2 3] means that we are dealing with a
multi-array of 2x3 and[2 3 5] would mean the array in question is
2x3x5. Someone calls a function like the following to tell us that.

void setArrayDim(int arrayDim[], int size, int elemSize);

Assming that the multi-array's element type is T.

1) is solved by:

malloc( product of all elements of arrayDim * sizeof(double) );


For 2), we have another function like:

setArrayValue(arrayIndices[], int size, double value);

In this case the arrayIndices array has the indices into which value
must be deposited. We somehow have to come up with an equation that
computes the correct pointer location.

I can do this manually for a 2D array. Maybe a 3D array won't be
hard. But what I need to do is a multi-dimensional array of
dimensions D.

Ask what information you needed in order to do it for a 2D or a 3D array and
then ask what information, in general, you need for a D-dimensional array.
Even for a 2D array, you need more information than your setArrayValue()
prototype indicates.
 
B

Barry Schwarz

After reading all the FAQs, I cannot solve the following problem:

I have a pointer of type double. I am supposed to

1) Allocate memory for it assuming that the pointer will be pointing
to a multi-dimensional array whose dimensions will be specified at
run-time.

2) Set the individual elements of the array using pointer-arithmetic.

I can do 1) but 2) is hard and requires some recursive logic that I
cannot come up with.

Suppose we are given the dimensions of a multi-array using a 1D array
of length D. For example, [2 3] means that we are dealing with a
multi-array of 2x3 and[2 3 5] would mean the array in question is
2x3x5. Someone calls a function like the following to tell us that.

From here it is obvious that you have figured out that the 2-D array
would contain 6 elements and the 3-D array would contain 30.
void setArrayDim(int arrayDim[], int size, int elemSize);

Are we to assume that size is the number of elements in arrayDim and
therefore also the number of dimensions in "to be allocated" array?

What purpose does elemSize serve? Your pointer must be a pointer to
the correct type or your code will require enough casts to be
unreadable.
Assming that the multi-array's element type is T.

1) is solved by:

malloc( product of all elements of arrayDim * sizeof(double) );

Make up your mind; is it T or is it double? But you have the right
idea.
For 2), we have another function like:

setArrayValue(arrayIndices[], int size, double value);

In this case the arrayIndices array has the indices into which value
must be deposited. We somehow have to come up with an equation that
computes the correct pointer location.

Using your [2 3 5] example, a real 3-D array
double a1[2][3][5];
would be laid out in memory such that a1[0][0][0] is immediately
followed by a1[0][0][1] and a1[0][0][4] is immediately followed by
a1[0][1][0]. You should be able to extrapolate for the other array
elements.

Looking at the memory in this linear fashion, a1[0][0][0] is the first
element, a1[0][0][4] is the fifth, a1[0][1][0] is the sixth,
a1[1][0][0] is the sixteenth, etc.

If we have a pointer that points to the first element
double *p1 = &a1[0][0][0];
we know that the value of a1[0][0][0] can be accessed through p using
subscript notation p1[0] or pointer notation *(p1+0) (while this could
be written simply as *p1 it wouldn't flow as nice in the next
paragraph).

Since a1[0][1][0] is the sixth element of the array, we could access
it as p1[5] or *(p1+5). Any element n+1 can be accessed as p[n] or
*(p+n). So a1[1][0][0] would be the same as p[15] or *(p+15).

To simulate an array which has dimensions d1, d2, ...., dn you would
allocate enough space to hold d1 * d2 * ... * dn elements. To access
an element whose subscripts are s1, s2, ..., sn you would compute the
offset from the first element as
(...(s1 * d2 + s2) * d3 + s3) * d4 + ... ) * dn + sn

Note that this means your function setArrayValue needs another
parameter, namely the same array arrayDim of dimension values you
passed to setArrayDim. Since arrayDim has the same number of elements
as arrayIndices, the parameter size will serve for both.
I can do this manually for a 2D array. Maybe a 3D array won't be
hard. But what I need to do is a multi-dimensional array of
dimensions D.

Once you have done it for two, it is a simple extension to do it for
any number of dimensions.



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top