Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-dimensional array initialization

People also ask

What is multi-dimensional array?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

What is multi-dimensional array How is it initialized how the elements of multi-dimensional arrays are accessed?

Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.

What is multi-dimensional array syntax?

Default format is row-major. It is the simplest form of multidimensional array. Syntax:- datatype name[size_1][size_2]; datatype name[size_1][size_2];


You can initialize arrays in both ways, though the usage of curly inner braces is recommended as it improves the readability.

The easiest way to find the value of an element of a multi-dimensional array non-formatted with braces is by splitting the array. For example, your array's dimensions are 2x3x2:

First split the array into 2 sets (2x3x2)

{14,11,13,10,9,6,8,7,1,5,4,2} --> {{14,11,13,10,9,6}, {8,7,1,5,4,2}}

Then split each set into 3 sets (2x3x2)

{{14,11,13,10,9,6},{8,7,1,5,4,2}} --> {{{14,11}, {13,10} ,{9,6}}, {{8,7}, {1,5}, {4,2}}}

Now, as you see there are 2 elements left in every smaller set (2x3x2), so you have formatted your array with braces.

Now it's simpler to find the value of the element with the index of [1][1][0]. This element is the 2nd ([1][1][0]) bigger set's 2nd ([1][1][0]) smaller set's 1st ([1][1][0]) element, so the answer is 1.


That being said, such an exam question shows the lack of professionalism of your teacher, who's more interested in abusing the programming language syntax, rather than teaching fundamental initialization rules.


The correct answer that should yield full score would be: compile code with all warnings enabled and you won't end up writing crappy code like this.

gcc test.c -std=c11 -pedantic-errors -Wall -Wextra
test.c: In function 'main':
test.c:6:3: warning: missing braces around initializer [-Wmissing-braces]
   int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2};
   ^

However, I suspect that your teacher is not so much concerned about the code being crap, but is rather looking for the detail in the C language which allows arrays (and structures) to be initialized even though the brace list does not match the structure of what's being initialized.

As far as the C language is concerned, int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2} is completely equivalent to:

// properly written initialization list for a 3D array
int Multi[2][3][2] = 
{ 
  {
    {14, 11},
    {13, 10},
    { 9,  6}
  },
  {
    { 8,  7},
    { 1,  5},
    { 4,  2}
  }
};

The only rationale for why the first form is allowed, is because it allows you to write stuff like

int Multi[2][3][2] = {0};

which explicitly initializes the first element to 0 and the rest of the elements as if they had static storage duration (0 as well). Meaning all elements will be set to zero.

Writing things like int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2} is abusing the C language. It is very bad practice. Doing so is banned by MISRA-C and so on.

A good teacher would be concerned about teaching you how to enable all compiler warnings and how to properly initialize multi-dimensional arrays, rather than letting you interpret obfuscated nonsense code.


int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2};

memory map of Multi[2][3][2] is->

Multi[0][0][0]=14;

Multi[0][0][1]=11;

Multi[0][1][0]=13;

Multi[0][1][1]=10;

Multi[0][2][0]=9;

Multi[0][2][1]=6;

Multi[1][0][0]=8;

Multi[1][0][1]=7;

Multi[1][1][0]=1;

Multi[1][1][1]=5;

Multi[1][2][0]=42;

Multi[1][2][1]=2;

therefore the value of Multi[1][1][0]=1; this is so simple

and we can also initialize like this

int Multi[2][3][2] = {{{14,11},{13,10},{9,6}},{{8,7},{1,5},{4,2}}};