473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MultiDim Array Challenge

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.
Nov 14 '05 #1
2 2067

"Salman Khilji" <sa**********@yahoo.com> wrote in message
news:b3**************************@posting.google.c om...
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.

Nov 14 '05 #2
On 27 Feb 2004 17:15:11 -0800, sa**********@yahoo.com (Salman Khilji)
wrote:
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>>
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sona | last post by:
Please could someone tell me how to use a single-dimensional array as a two-dimensional array? Is the following correct: int ROW_SIZE = 5; int COL_SIZE = 3; int *arr = (int*)...
0
by: EMiller | last post by:
Hello, I am encountering a development challenge here that seems to be stumping me. I am developing a C#/.NET application using an MSDE database. There is a particular field in a table that I...
8
by: Levent | last post by:
Hi, Is it possible to have static multi-dim array as a function return parameter? in particular, class Foo { int arr; public: ? getArr() {return (?) arr;}
9
by: Oliver Saunders | last post by:
This is an amazing opportunity to win a tin of sardines if you can answer this difficult question correctly! Question: What kind of loop do you need to turn an array like this: $notes =...
14
by: dam_fool_2003 | last post by:
Hai, When I declare a zero-based array I get a compile time error stating that zero based array is not possible. But if I declare the same in the function argument I don't get any error. Is...
10
by: Peter Stojkovic | last post by:
I want store an integer-array of 1000 Values in a blob in a SQL-database. I will do this every 10 Seconds. How can I do this ???? What datatypes a have to use ??? Thanks
1
by: Abhi | last post by:
Hi! I am wondering if someone can point me to the needed sort function. There are so many of them that I simply got lost by reading their descriptions. I try to explain what I need. I have a...
0
by: guerrerofarias | last post by:
I have a "population" of objects that is suppose to do a lot of stuff during its lifecycle... I had everything working fine (not fancy code, though) with an array, but now I've received instructions...
6
by: Michael Bray | last post by:
I'm trying to figure out what is the easiest way in C# 2.0 to convert an object array (object, int, anything) to a string array (string or List<string>) in one line of code. At first I thought I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.