1. ## 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. 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.

3. 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.

4. 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. 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.

6. 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. 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

8. Originally Posted by matsp
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