View unanswered posts | View active topics It is currently Sat Nov 01, 2014 5:02 am






Reply to topic  [ 13 posts ] 
4-dimensional arrays ? 
Author Message
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post 4-dimensional arrays ?
hi,
I need 4 dimensional arrays of int in my program.
Strangely RobotC supports only 2 dimensions, but this is far too little.
Could you please enable more dimensional arrays in a future (maybe in the next) release?


Sat Oct 11, 2008 4:50 pm
Profile
Rookie

Joined: Sun Sep 28, 2008 1:06 pm
Posts: 27
Post Re: 4-dimensional arrays ?
wow 4D arrays? not many languages support this at all i believe...could you please tell us what your application is and maybe we can help you find a workaround?


Sat Oct 11, 2008 11:29 pm
Profile
Rookie
User avatar

Joined: Wed Apr 30, 2008 7:05 am
Posts: 18
Location: Brisbane, QLD
Post Re: 4-dimensional arrays ?
i dodged up 3d arrays by using structs. Im sure u can do the same with 4d.
This works for 3d
Code:
typedef struct {
   int array2D[sizeX][sizeY]; //using an array of structures to create [][][]
} 3Dmatrix;

3Dmatrix test[sizeZ];


You could always try:-

Code:
typedef struct {
   int array2D[sizeX][sizeY]; //using an array of structures to create [][][][]
} 3Dmatrix;

3Dmatrix test[sizeZ][sizeK];

_________________
experiencing slow death by thesis


Sun Oct 12, 2008 12:33 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: 4-dimensional arrays ?
basicxman wrote:
wow 4D arrays? not many languages support this at all i believe...

In ANSI C, 7-dimensional arrays are standard!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sun Oct 12, 2008 4:57 am
Profile
Rookie

Joined: Sun Sep 28, 2008 1:06 pm
Posts: 27
Post Re: 4-dimensional arrays ?
Ford Prefect wrote:
basicxman wrote:
wow 4D arrays? not many languages support this at all i believe...

In ANSI C, 7-dimensional arrays are standard!


guess i learned something new today. 7D eh? wow... still curious as to your application spaceballs


Sun Oct 12, 2008 11:54 am
Profile
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post Re: 4-dimensional arrays ?
it's for kind of look up table :programmer:


Sun Oct 12, 2008 12:50 pm
Profile
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post Re: 4-dimensional arrays ?
@ the developers:
Could you please enlarge the maximum of dimensions?
It's very urgent, and it should not be a problem to just raise the dim limit up to the standards!


Sat Oct 18, 2008 6:40 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: 4-dimensional arrays ?
I'm one of the developers.

This is not an easy limit to "simply change a constant to increase the limit".

I will add this to the list of possible feature requests.

Meanwhile you can work around it as someone has suggested with a two dimensional struct containing a two dimensional member variable.


Mon Oct 20, 2008 3:21 pm
Profile
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post Re: 4-dimensional arrays ?
hi,
thx, I'll try it, but I think it won't be easy.


Tue Oct 21, 2008 5:36 am
Profile
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post Re: 4-dimensional arrays ?
There is a simpler solution to create a multidimensional array. The trick is to convert it to a one dimensional array. I'll explain using a two dimensional array as an example, but the trick works for any number of dimensions.

Suppose you want a 2 dimensional array with 10 positions in each dimension. This array has 10 times 10 equals 100 positions. So a one dimensional array of 100 positions can store exactly the same amount of information. In general, the required length of a one dimensional array equals the product of the dimensions of a multidimensional array.

This leaves only the problem of addressing each item in your array. But, this can also be solved using a little math. Suppose you want to address row=1 and col=3 in a two-dimensional array. For a one-dimensional array this address can be translated to 13 (=10*row+col). The number 10 corresponds to the length of the col dimension, or is the number of posions one needs to go to the next row in a one dimensonal array.

Now on to the fourth dimension. I'll give an example. But remember, arrays start counting at 0, so to get the length of a dimension one needs to add 1 to the upperbound.
(Warning, this code has not been tested.)
Code:
// These are the upperbounds of each dimension
const int dim1=4, dim2=4, dim3=4, dim4=4;

// This creates a four-dimensional array
int myArray[dim1][dim2][dim3][dim4];

// This writes a value to my array;
myArray[1][2][3][4]=99;

// This function translated a four dimensional address to a one-dimensional address
int fourToOne(int d4, int d3, int d2, int d1)
{
  return d4*(dim4+1)*(dim3+1)*(dim2+1) +  d3*(dim4+1)*(dim3+1)+  d2*(dim4+1) +d1;
}

// This creates a one-dimensional array with the same size
int myArray[forToOne(dim1,dim2,dim3,dim4];

// This writes a value to my array;
myArray[fourToOne(1,2,3,4)]=99;



_________________
My most recent blog: A grain of sugar


Tue Oct 21, 2008 4:38 pm
Profile WWW
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post Re: 4-dimensional arrays ?
very tricky, thanks a lot :P


Tue Oct 21, 2008 4:56 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: 4-dimensional arrays ?
Aswin wrote:
There is a simpler solution to create a multidimensional array. The trick is to convert it to a one dimensional array. . . .

A furthur simplification is to write a small helper function (or macro) that does the array index calculation for you. It would have four parameters for a four dimensional array. The actual "index" calculation can be localized to the one helper function.


Tue Oct 21, 2008 5:08 pm
Profile
Rookie

Joined: Sat Apr 19, 2008 11:51 am
Posts: 43
Post Re: 4-dimensional arrays ?
hi,
I managed to built my table with an 2D array of a 2D structure type.
Thx to all!


Wed Oct 22, 2008 12:07 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.