Multi dimensional array

This is a discussion on Multi dimensional array within the C Programming forums, part of the General Programming Boards category; I have read something about single dimensional array and multi dimensional array - 2 dimensional array to be specific. I ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    Multi dimensional array

    I have read something about single dimensional array and multi dimensional array - 2 dimensional array to be specific. I understood the concept and how to use those feature in C/C++. The 3 and 4 dimensional arrays are mentioned in the book but they did not provide complete information about those 2 type of multi dimensional array. Somebody told me that those 2 types of multidimensional array are not widely used now, but failed to provide any fact that could substantiate what he stated about multidimensional array. I'm curious on how to use 3 and 4 dimensional arrays and how they work. Is there any good tutorials that you could provide to learn this? I've searched over the internet but they mostly provide 2 dimensional array. Are 3 and 4 dimensional arrays not widely used now?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    If you've already figured out that you need
    int arr[X][Y];
    and two nested for loops to generate subscripts arr[x][y]

    what's so hard about getting to
    int arr[X][Y][Z];
    and three nested for loops to generate subscripts arr[x][y][z]

    > Are 3 and 4 dimensional arrays not widely used now?
    Well you could do
    char library[BOOKS][CHAPTERS][PARAGRAPHS][SENTENCES][WORDS][CHARS];
    but it's likely to be a massive waste of space.

    3D arrays are perfect, if you're into say 3D modelling of weather.

    It's not that they're less used, more that no-one would ever bother to use a 3D array in a text editor to represent the text paragraphs, words and chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    498
    The 3 and 4 dimensional arrays are mentioned in the book but they did not provide complete information about those 2 type of multi dimensional array. Somebody told me that those 2 types of multidimensional array are not widely used now
    The data structure being used totally depends on the application you are trying to create.
    They might have told you with a specific application on mind.
    An application may be created by using multiple methods and using different data structures.
    Sometimes it may be efficient to use one data structure over the other.
    For example to solve SUDOKU one method that i know of uses 3-d array.
    But there are other efficient datastructures which can be used to solve the same problem with a different method.
    In short, which data structure depends on the problem at hand IMHO.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Are 3 and 4 dimensional arrays not widely used now?
    Well you could do
    char library[BOOKS][CHAPTERS][PARAGRAPHS][SENTENCES][WORDS][CHARS];
    but it's likely to be a massive waste of space.
    I see, since it's occupying large amount of memory storage then most programmers choose not to use 3 or more dimensional array, unless, necessary.

    ok.. let's say it's going to waste large amount of memory, but you as a programmer needs to use that feature to finish your program ( I can't think of any that could use, just assuming a programmer needs to use it ) but since you know that it's going to waste memory storage is there any feature in C that could supersede that feature?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    It's a question of using the right data structures for the task at hand.

    If 3D arrays were commonly used for some kind of application in the past, then that would still be the case today as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    It's a question of using the right data structures for the task at hand. If 3D arrays were commonly used for some kind of application in the past, then that would still be the case today as well.
    I see, thanks for the information, this is a completely additional knowledge for me......

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What generally happens when you have complex models that require many dimensions is that the problem gets split into other data structures.

    if we take Salem's example:
    Code:
    char library[BOOKS][CHAPTERS][PARAGRAPHS][SENTENCES][WORDS][CHARS];
    Not only does this cause problems with the fact that ALL books will have CHARS characters, representing the longest word used in any book of the library. The number of words in a sentence in a law-book may be quite different from that of a childrens book, for example.

    This all means that it's probably not a feasible structure.

    A more dynamic structure, where data is stored "as needed" will solve this problem:
    Code:
    struct word
    {
       char *chars;
    };
    
    struct sentence
    {
        int num_words;
        struct word *words;
    };
    
    struct paragraph
    {
        int num_sentences;
        struct sentence *sentences;
    };
    Then we allocate dynamically the NECESSARY content, setting the num_words according to the current length of the sentences, so "Bob plays with a toy." takes up 5 words, whilst "The hirer of aforementioned vehicle will at all times ensure that the necessary and required maintenance and service is performed according to the manufacturers instructions, and any cost for such maintenance or servicing is to be paid by the hirer." takes up 39 words in it's sentence.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by matsp View Post
    What generally happens when you have complex models that require many dimensions is that the problem gets split into other data structures.

    if we take Salem's example:
    Code:
    char library[BOOKS][CHAPTERS][PARAGRAPHS][SENTENCES][WORDS][CHARS];
    Not only does this cause problems with the fact that ALL books will have CHARS characters, representing the longest word used in any book of the library. The number of words in a sentence in a law-book may be quite different from that of a childrens book, for example.

    This all means that it's probably not a feasible structure.

    A more dynamic structure, where data is stored "as needed" will solve this problem:
    Code:
    struct word
    {
       char *chars;
    };
    
    struct sentence
    {
        int num_words;
        struct word *words;
    };
    
    struct paragraph
    {
        int num_sentences;
        struct sentence *sentences;
    };
    Then we allocate dynamically the NECESSARY content, setting the num_words according to the current length of the sentences, so "Bob plays with a toy." takes up 5 words, whilst "The hirer of aforementioned vehicle will at all times ensure that the necessary and required maintenance and service is performed according to the manufacturers instructions, and any cost for such maintenance or servicing is to be paid by the hirer." takes up 39 words in it's sentence.

    --
    Mats
    thanks for the additional information..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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