Initializing a 3D array w/o a loop.

This is a discussion on Initializing a 3D array w/o a loop. within the C++ Programming forums, part of the General Programming Boards category; What's the syntax? For example, a 2d array: Code: int[][] array = { {2, 5} , {7, 4}, }; I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Initializing a 3D array w/o a loop.

    What's the syntax? For example, a 2d array:
    Code:
    int[][] array = {
    	       {2, 5} , {7, 4}, 			
    	  };
    I'm looking for a similar approach with a 3D array but can't seem to find it anywhere.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    same way, but it starts to get messy and hard to follow...
    Code:
    int array [2][2][2]=
    {
      {
        {1,1},
        {1,2}
      },{
        {2,1},
        {2,2}
      }
    };
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    yeah 3d arrays are pain in the ars. What are you trying to do with them funkydude?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Originally posted by axon
    yeah 3d arrays are pain in the ars. What are you trying to do with them funkydude?
    I'm making Tetris, I need a 3d array to represent each block and it's rotations. I realize I could of just calculated the rotation myself using just a 2d array also, but I decided to go this way.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    a 3d array is the hard way of going about it dude. Do it in 2d and figure out the rotation there. If you're having troubles do a search....this has been done many times here before.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Also, remember that in memory, the data is laid out just as it would be if it were a one-dimensional array. You can see this for yourself by simply casting the base of the array to a pointer of the appropriate type:


    Code:
     const int X = 4, Y = 2, Z = 5;
    
     int abc[X][Y][Z] =
       { { { 1,  2,  3,  4,  5 }, { 6,  7,  8,  9, 10 } },  
         { {11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20 } }, 
         { {21, 22, 23, 24, 25 }, {26, 27, 28, 29, 30 } },
         { {31, 32, 33, 34, 35 }, {36, 37, 38, 39, 40 } } };
       
     int main()
    {
         for(int x = 0; x < X; ++x)
           for(int y = 0; y < Y; ++y)
             for(int z = 0; z < Z; ++z)
               printf("%d...", abc[x][y][z]);    
    
    
     const int U = X * Y * Z;
    
     int * r = (int*)abc;
     
     printf("\n\n - again - \n\n");    
     
     for(int u = 0; u < U; ++u)
       printf("%d...", r[u]);    
     
     return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  3. Initializing an Array to Zero
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-06-2002, 11:49 AM
  4. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 10:05 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21