Multi-Dimensional Array Initialization

Multidimensional array initialization in C

With the inner braces the array looks like this:

10  5 -3  0  0
9 0 0 0 0
32 20 1 0 0
0 0 8 0 0

So in every line the last 2 values are zero (because you didn't set a value for them.
Without the inner braces the array would look like this:

10  5 -3  9  0
0 32 20 1 0
0 8 0 0 0
0 0 0 0 0

Only the first 12 elements would become the given values and the rest will be 0.

Multi-dimensional array initialization

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.

Typescript - multidimensional array initialization

You only need [] to instantiate an array - this is true regardless of its type. The fact that the array is of an array type is immaterial.

The same thing applies at the first level in your loop. It is merely an array and [] is a new empty array - job done.

As for the second level, if Thing is a class then new Thing() will be just fine. Otherwise, depending on the type, you may need a factory function or other expression to create one.

class Something {
private things: Thing[][];

constructor() {
this.things = [];

for(var i: number = 0; i < 10; i++) {
this.things[i] = [];
for(var j: number = 0; j< 10; j++) {
this.things[i][j] = new Thing();
}
}
}
}

How to declare initialize and use 2 dimensional arrays in Java Script Using a text from html page

There is no "matrix" structure natively in the language. But you can create them without major problem as far as you "book" the required memory.

Let's say you would like to create a 3x3 matrix, first you have to create an Array which will store references to each row/column (depending of your point of view, of course).

function createMatrix(N, M) {
var matrix = new Array(N); // Array with initial size of N, not fixed!

for (var i = 0; i < N; ++i) {
matrix[i] = new Array(M);
}

return matrix;
}

C# Initializing a Multi-dimensional array with multiple 1D arrays

You'll need to initialize the array yourself. You can get a performance improvement and shorter code than looping by using Buffer.BlockCopy, but can't do an inline initialization directly:

float[] a = {1,2};
float[] b = {3,4};
float[,] my2DArray = new float[a.Length, 2];

int len = a.Length * sizeof(float);
Buffer.BlockCopy(a, 0, my2DArray, 0, len);
Buffer.BlockCopy(b, 0, my2DArray, len, len);

Note that you'll have to guarantee that the source arrays are the same length for this to work.

Initialize multidimensional array with zeros

You do it exactly the same way

int marr[10][10] = {0};

Edit:

This is a C solution. For a C++ solution you can go for:

int marr[10][10] = {};

These 2 solutions do not work for arrays that have size defined via variables. e.g.:

int i, j = 10;
int marr[i][j];

To initialize such an array in C++ use std::fill.

String initialization in multidimensional array

After declaration you can not assign, but use strcpy()

char a[250][250][250];

strcpy(a[0][1],"0");

or assign at the time of declaration:

char a[250][250][250] = {"0","2"};  
char a[][250][250] = {"0","2"};

or if you want to assign a single char.

a[i][j][k] = '0'; 

Where i, j, k are any value less than 250


How to Declaration and Initialization 3D Array IN C

In-general a[3][4][2] is a three-dimension array, that can be view as

a[3][4][2] : Consist of 3 two-dimension arrays, where each 2-D array are consist of 4-rows and 2-colunms. can be declare as:

char a[3][4][2] =  { 
{ //oth 2-D array
{"0"},
{"1"},
{"2"},
{"4"}
},
{ //1th 2-D Array
{"0"},
{"1"},
{"2"},
{"4"}
},
{ //2nd 2-D array
{"0"},
{"1"},
{"2"},
{"4"}
},
};

Note: "1" means two chars, one additional fro null ('\0') char.

If integer array:

int a[3][2][3]=  
{
{ //oth 2-D array, consists of 2-rows and 3-cols
{11, 12, 13},
{17, 18, 19}
},
{//1th 2-D array, consists of 2-rows and 3-cols
{21, 22, 23},
{27, 28, 29}
},
{//2th 2-D array, consists of 2-rows and 3-cols
{31, 32, 33},
{37, 38, 39}
},
};

Link to understand


Second error:

to this a[i][j][max] a char can assign not string so,

a[i][j][max] = '0' ; // is correct  expression 

but

a[i][j][max] = "0";  // is not correct, wrong   

Please read WhozCraig comment. you are declaring huge memory in stack!


According to your comment :

function declaration:

char add_func(char a[250][250][250], int i, int j); // function definition  

try like this:

  char a[250][250][250];
a[i][j][max] = add_func(a, i, j );

Omitting Sizes while Initializing C/C++ Multidimensional Arrays

The following is from section A8.7 of "The C Programming Language" by K&R, 2nd edition, pages 219,220:

An aggregate is a structure or array. If an aggregate contains
members of aggregate type, the initialization rules apply recursively.
Braces may be elided in the initialization as follows: if the
initializer for an aggregate's member that is itself an aggregate
begins with a left brace, then the succeeding comma-separated list of
initializers initialize the members of the sub aggregate; it is
erroneous for there to be more initializers than members. If,
however, the initializer for a subaggregate does not begin with a left
brace, then only enough elements from the list are taken to account of
the members of the subaggregate; any remaining members are left to
initialize the next member of the aggregate of which the subaggregate
is a part. For example,

 int x[] = { 1, 3, 5 }; 

declares and initializes x as a 1-dimensional array with three members, since no size was specified and

there are three initializers.

Therefore, given this line

int myArray[][2] = { { 2 }, { 4, 5 }, { 4, 1 } };

the compiler will recursively initialize the array, noting that each subarray starts with a left brace and has no more than the required number of initializers, and will count the number of subarrays to determine the first dimension of the array.

The following is from section A8.7 of "The C Programming Language" by K&R, 2nd edition, page 220:

float y[4][3] = {
{ 1, 3, 5 },
{ 2, 4, 6 },
{ 3, 5, 7 }
};

is a completely-bracketed initialization: 1,3 and 5 initialize the first row of the array y[0], namely y[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and y[2]. The initializer ends early, and therefore the elements of y[3] are initialized with 0. Precisely the same effect could have been achieved by

float y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};

Note that in both cases, the fourth row of the array will be initialized
with zero, since not enough initializers were specified.

float y[4][3] = { 
{ 1 }, { 2 }, { 3 }, { 4 }
};

initializes the first column of y and leaves the rest 0.

So the compiler doesn't ignore the inner braces. However, the inner braces are optional if you specify all of the initializers in order with no gaps. Using the inner braces gives you more control over the initialization, if you don't want to specify a full set of initializers.



Related Topics



Leave a reply



Submit