Thread: Differennce between one and two dimensinal arrays

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    46

    Differennce between one and two dimensinal arrays

    Hi
    I am studying arrays... So i have a simple question.. I better to use one dimensional arrays or 2 dimensinal arrays? And why? I mean who is faster and why? Wich is better to use for compare the valus of 2 equal arrays??

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    It depends on the data. If you need to locate the X-Y location of a pixel in a window, that's a 2-d array. Bitmaps are composed of 3 colors per pixel, so bitmaps are 3-d arrays.

    AFAIK, there is no speed difference. Physical memory is one-dimensional and multi-dimensional arrays are a conceptual abstraction.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    1D arrays can be usefull for holding filenames for image files for a game. You can than use a 'for' loop to search through the array and load the images into memory. 2D arrays can be used for displaying a chess/checker game in a console app. They can also be used for graphical games, such as 'Breakout', to display a map. An array of type 'int' will be filled with numbers, which correspond to certain color tiles. There are many uses for both types, and again, depends on the situation.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    They are really the same neither is faster. It's just a style difference. 2D arrays are stored in continuous memmory, just like 1D arrays.

    You want your code to be easy to read, so use which ever makes sence.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It is however of note to mention that there is no such thing as a multidimensional array in C++. A 2D array is in fact an array of arrays. It is still a unidimensional array, only each element is storing an array. A 3D array is in fact an array storing arrays each of them storing an array. A 4D array is... ad aeternum.

    Code:
    /* An array called myarray with 2 elements. Each element is an array of 3 ints. */
    int myarray[2][3];
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    They are really the same neither is faster. It's just a style difference. 2D arrays are stored in continuous memmory, just like 1D arrays.
    Not true.

    How do you suppose indexing into a 2D array at an arbitrary offset is done without using a multiply?

    ??

Popular pages Recent additions subscribe to a feed