473,748 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(a rrayIndices[], 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 2097

"Salman Khilji" <sa**********@y ahoo.com> wrote in message
news:b3******** *************** ***@posting.goo gle.com...
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(a rrayIndices[], 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**********@ya hoo.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
5459
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*) malloc(sizeof(int) *ROW_SIZE*COL_SIZE); arr;
0
1405
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 need to accept a two-dimensional array of integers. When creating the database, I chose the SQL_Variant datatype for this column to which I will be writing the array.
8
1911
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
1875
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 = array( '1:1', '1:2', '1:3'
14
2491
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 related with the function in C in which the argument's references are passed in the stack during the runtime. I am confused.
10
5237
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
1629
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 multidimensional array and I have a function that outputs it as a table. $arr contains an array that contains usernames $arr contains an array that contains real names $arr contains an array that contains e-mails
0
1395
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 to let the population change size over generations. This means my array no longer works...I want to change it to a vector (or map), keeping the basic structure of the code. But it beats my skills...I read a thread...
6
6336
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 could do something like the following: object v; // initialized with some values return Array.ConvertAll<object,string> (v, new Converter<object,string>(
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.