4 dimension array

This is a discussion on 4 dimension array within the C++ Programming forums, part of the General Programming Boards category; in a 4 dimensional array... Code: char BARRY[1][1][2][2]; how do I just intialize the last 2 blocks of the array? ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    4 dimension array

    in a 4 dimensional array...

    Code:
    char BARRY[1][1][2][2];
    how do I just intialize the last 2 blocks of the array?

    example...
    Code:
    BARRY[0][0]={{2,3},
                {4,5}};
    this is of course on a much lower scale then i need the array...but is there any way to do something similiar to that?


    (I hope i explained it well enough...)
    MSVC++~

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Magic.

    No really -- you can't. You'd have to just set the values individually for that "last block" or set all the values prior as well.

    Are you sure you really want a 4-dimensional array, anyways? I can't think of very many times that it would actually make any sense to use one. If some of the dimensions represent concepts (IE a vertex, or a string) then you are most-likely better off making those dimensions objects instead.

    P.S. This is my 400th post. Yay.

    EDIT: I'm retarded today, don't mind me
    Last edited by Polymorphic OOP; 12-29-2002 at 03:05 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char BARRY[1][1][2][2];
    What's the point of using an array with one element? As a simple example, here is the equivalent to your array (initialized all nice and pretty):
    Code:
    int a[1][1][2][2] = {
      {
        {
          {
            1,2
          },
          {
            1,2
          }
        }
      }
    };
    Seeing it like this you may notice that the first and second dimensions really aren't doing anything useful except making your code more reader unfriendly. If you remove the useless dimensions you have something much more manageable:
    Code:
    int a[2][2] = {
      {
        1,2
      },
      {
        1,2
      },
    };
    [Ben Stein Monotone]
    Wow.
    [/Ben Stein Monotone]

    -Prelude
    My best code is written with the delete key.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Prelude
    >char BARRY[1][1][2][2];
    What's the point of using an array with one element?
    He just made it that small for an example, he said he'd want it bigger later.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    reason im using the 4 dimension array is for a map...the first two blocks are the x and y coords of the map itself(over 100 maps)

    then the last 2 are for each indivudal maps x and y coords...

    I was originally trying to do this with a linked list...but i couldnt figure out a good way to link up all the maps...so it seems easier to connect them this way...but i donno...im always up for suggestions ;P
    MSVC++~

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Supar
    reason im using the 4 dimension array is for a map...the first two blocks are the x and y coords of the map itself(over 100 maps)
    There you go right there -- why not make it a class called vertex. Using things like 4-dimensional arrays where sections mean things like vertices makes it unclear what you are trying to model. You're best off notating that concept via objects.

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Polymorphic OOP
    There you go right there -- why not make it a class called vertex. Using things like 4-dimensional arrays where sections mean things like vertices makes it unclear what you are trying to model. You're best off notating that concept via objects.
    Exactly, in cases like this it's better to creat a new data type to hold your data.
    none...

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    Why make a new class? Sorry im rather new at programming...

    I mean why make some new class if Im going to have to go through and define out each map personally anyways?

    btw...I still cant seem to figure out how to add data to those end blocks without doing them all right at the beginning or one at a time...=(

    thx for all your help tho
    MSVC++~

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Supar
    Why make a new class? Sorry im rather new at programming...

    I mean why make some new class if Im going to have to go through and define out each map personally anyways?
    Because it makes logical sense to do so. Taking it down to simpler terms -- an array of vertices that have an x and a y coordinate makes more sense when you define a class that has an x and a y member and make a single dimensional array of those than if you made a 2-dimensional array of ints. It simply doesn't make as much logical sense to have a 2-dimensional array there because what it's modeling isn't as apparent. When you say you want "an array of vertices" then do just that in code -- make a single dimensional array of vertices, each vertex having an x and a y coordinate. Your example is slightly more complex than that but the logic is still the same. Also, defining a class for the vertex allows you to work with member functions that only affect the vertex and you can use it in places where a vertex makes sense and another datatype with 2-ints (ie a vector) wouldn't. All that an array with 2 elements tells you is that there are 2 elements, but there are multiple concepts that can be broken down into a similar fashion (IE point and vector are two different concepts but their makeups are similar).

    Originally posted by Supar
    btw...I still cant seem to figure out how to add data to those end blocks without doing them all right at the beginning or one at a time...=(

    thx for all your help tho
    Again, you simply can't.
    Last edited by Polymorphic OOP; 12-29-2002 at 08:07 PM.

  10. #10
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Well

    You could just create an array of pointers, then that array to another array of pointers, etc.. It wouldn't be as easily managed as a C++ 3rd dimensional array or anything, but you could create a 4th dimensional array that way.

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I agree with Polymorphic OOP...
    I think you should write the class, and post it here so we can help correct mistakes( if there were any ), and you get used to it.
    none...

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Post



    i cant seem to figure out this vertex stuff...i looked throughout the forums and google...no real explanations to what you mean...

    but anways...since you cant define the whole array except when it is first defined...I cant really make a struct or class for my map arrays...

    but i figure that i can just make 1 array for the overall map vector

    then make as many others as i need...for the maps....

    *shrugs*damn my nubiness ;P
    MSVC++~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating double dimension array.
    By apacz in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2005, 01:39 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. 2 dimension string array
    By djarian in forum C Programming
    Replies: 4
    Last Post: 05-05-2003, 01:47 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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