Thread: 3+d array?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    3+d array?

    Is it possible to have a 3+ dimensional array?

    I think a 3d array would work when working with 3d, but 4d+? Wouldnt that defy the laws of physics or something? What would it look like?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int three_dimensional_array[x][y][z];
    int four_dimensional_array[x][y][z][a];
    int eight_dimensional_array[x][y][z][a][b][c][d][e];
    You'll run out of memory pretty quickly if you have lots of large arrays, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    wait, how can you simulate a 4d+ array? Make a 4dimensional world?!

    Isnt 4d time, 5d space, 6d amtter, 7d i have no clue... but im gonna google it.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you talking about? You're not creating physical dimensions here. Just a series of arrays of arrays of arrays ...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You are creating a huge mess. The most dimensions I'd ever use is 2, and I normally use 1D arrays or linear arrays.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    I know, but arrays can be just like dimensions in real life. Isnt there a way to simulate it somehow? How 1d is like a string (a line) while 2d is like a square (down adn across) and 3d is like a 3d cube(detph/z axis). So what would 4d be?
    Oh and this is just for informational purposes... Its not like im really gonan be using anything more then 3d tops.
    Last edited by SG57; 06-24-2006 at 03:28 AM.

  7. #7
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by SG57
    wait, how can you simulate a 4d+ array? Make a 4dimensional world?!

    Isnt 4d time, 5d space, 6d amtter, 7d i have no clue... but im gonna google it.
    The fact that humans perceive only 3 physical dimensions is coincidental to the fact that arrays can be multi-dimensional. Each dimension of an array represents whatever you are needing to represent. If you want to use a 4 dimension array to keep track of x,y and z coordinates for each time interval a then you could use a 4 dimension array.
    Code:
    world[x][y][z][a]
    You could also use a larger dimension array something like
    Code:
    data[row][column][sheet][workbook][folder][company][city][state][country][etc...]
    where the row and column are what is stored in the row and column of a spreadsheet that is in a particular workbook that is located in a particular folder that resides within a particular company that is based out of a certain city in a certain state in a particular country... This could be accomplished much more efficiently using other methods of course, but is an example of a 9+ dimension array.
    Quote Originally Posted by SG57
    I know, but arrays can be just like dimensions in real life. Isnt there a way to simulate it somehow? How 1d is like a string (a line) while 2d is like a square (down adn across) and 3d is like a 3d cube(detph/z axis). So what would 4d be?
    What do you want 4d to be? There are widely differing opinions of what the 4th dimension is, whether it is actually time, or it is some measurement humans are unable to perceive naturally, or whatever. If you are the program writer, you decide what 4d is. I like to think of the 4th dimension as time because it is conceptually easy for me to understand, but I have heard compelling arguments otherwise.

    Either way, you should define your world (3d or 4d or 12d?) before you start trying to figure out how to represent it in a data structure.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you ready for a mess of uneccessary code?

    Code:
     int i = 3;
      int* pi = &i;
      int** p2i = π
      int*** p3i = &p2i;
      int**** p4i = &p3i;
      int***** p5i = &p4i;
      int****** p6i = &p5i;
      int******* p7i = &p6i;
      int******** p8i = &p7i;
      int********* p9i = &p8i;
      int********** p10i = &p9i;
      int*********** p11i = &p10i;
      int************ p12i = &p11i;
      int************* p13i = &p12i;
      int************** p14i = &p13i;
      int*************** p15i = &p14i;
      int**************** p16i = &p15i;
      int***************** p17i = &p16i;
      int****************** p18i = &p17i;
      int******************* p19i = &p18i;
      int******************** p20i = &p19i;
      int********************* p21i = &p20i;
      int********************** p22i = &p21i;
      int*********************** p23i = &p22i;
      int************************ p24i = &p23i;
      int************************* p25i = &p24i;
      int************************** p26i = &p25i;
      int*************************** p27i = &p26i;
      int**************************** p28i = &p27i;
      int***************************** p29i = &p28i;
      int****************************** p30i = &p29i;
      int******************************* p31i = &p30i;
      int******************************** p32i = &p31i;
      int********************************* p33i = &p32i;
      
      cout << *********************************p33i << endl;
    This is an attempt to find out if my compiler (MSVC 2003) put a limit on the levels of indirection I can have. Looking at the asm, she accounted for all 33. I don't want to go any further.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My compiler (MinGW) supports over 3000 levels of indirection before it runs out of stack space (it must be recursive).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    The impossibility of 4 dimensions in real life does not in any way affect the plausibility of having a multidimensional array in C. In any case, even a 2D array is stored one-dimensionally in your computer's RAM. In fact, most processors don't support 2+D arrays; they are only provided in C as a convenience.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A line of text. - 1 dimension.
    A page of lines. - 2 dimensions.
    A folder of papers. - 3 dimensions.
    A box of folders. - 4 dimensions.
    A truck of boxes. - 5 dimensions.
    A warehouse of trucks. - 6 dimensions.
    A field of warehouses. - 7 dimensions.

    We could go on, but I'll assume you've grasped the concept.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This thread is a clear example of the complete abuse of arrays and their usefulness.

    This should answer all your questions. Notice it's from webster.cs.ucr - the same site you can get Randall Hyde's Art of Assembly Language. Definitely not a coincedence.

    http://webster.cs.ucr.edu/AoA/Window.../Arraysa2.html

    Now you can see why using more than 1 dimension will severely impact the performance of the array, especially if does NOT have dimensions that are a power of 2. Even using bit shifting for multi-dimensional arrays with dimensions that are a power of 2 will still be slower than using a 1 dimensional array.

    So how do you solve the problem? In C you use structs, and in C++ you use classes.

    Code:
    struct Person
    {
      int iAge;
      int iHeightInches;
      int iShoeSize;
      int iYearBorn;
      int iMonthBorn;
      int iDayBorn
    };
    
    Person Crowd[1000];
    Much better than:

    Code:
    int Crowd[1000][6];
    To fill the array using structs watch:

    Code:
    for (int i=0;i<1000;i++)
    {
      Crowd[i].iAge=0;
      Crowd[i].iHeightInches=0;
      Crowd[i].iShoeSize=0;
      Crowd[i].iYearBorn=0;
      Crowd[i].iMonthBorn=0;
      Crowd[i].iDayBorn=0;
    }
    1000 iterations, 6 variables set per iteration.

    As opposed to the two dimensional method:

    Code:
    for (int i=0;i<1000;i++)
    {
      for (int j=0;j<6;j++)
      {
         Crowd[i][j]=0;
      }
    }
    6000 iterations.

    You could unroll the inner loop and still get 1000 iterations, but you still have the fact that in the array, none of the elements have any apparent meaning. They are just memory locations. With the structure you can tell exactly what each variable is representing.

    The only reason I would ever allocate a huge linear array is if I wanted to do my own memory management for something like a game. Then I would make functions and sort of a small memory management API to manage that huge chunk of memory. This has the benefit of, hopefully, being faster, easier to use, etc.

    Hope this helps.
    Last edited by VirtualAce; 06-24-2006 at 07:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM