Aryan poonacha - 5 months ago 24

C Question

I first want to clarify if my logic in the following is correct:

In a 2 dimensional array, say A[x][y], x represents the number of braces and y the number of elements in each brace. So int A[2][3] is initialized as:

`{`

{1,2,3}, //from A[0][0] to A[0][2];

{4,5,6} //from A[1]{0] to A[1][2];

};

Second, I want to know what the similar correlations are in a 3 dimensional array, four dimensional, and so on, and how to know which dimension number correlates to what level for any multi-dimensional array.

For example, in A[3][4][2], does the 3 denote the number of 2d tables, or rows/columns in each table? And in A[2][3][4][5], does 2 represent the number of 2d tables, no. of 3d tables, no. of 1d tables, or no. of rows/columns per 1d table? Note:I'm getting my head around multi dimensional arrays for the first time, please explain as simplistically as possible.

Answer

Yes what you say it's correct. You can think that ** recursively**.

Start from a 1D array (let's assume that it has 3 elements):

```
int 1darray[] = {0, 1, 2};
```

Now producing a 2D array simply says go inside every element of `1darray`

and put another 1D array, to produce a 2D one, like this:

```
int 2darray[] = {1darray_0, 1darray_1, 1darray_2};
```

where the `1darray_0`

, `1darray_1`

, `1darray_2`

are 1D arrays, just like the `1darray`

we created in the start. So now this will form a 3x3 2D array.

Now the 3D array can be formed like this:

```
int 3darray[] = {2darray_0, 2darray_1, 2darray_2};
```

where the `2darray_0`

, `2darray_1`

, `2darray_2`

are 2D arrays, just like the `2darray`

we created above. So now this will form a 3x3x3 3D array.

Your example:

```
A[3][4][2]
```

says that `A`

has:

- 3 rows
- 4 columns
- 2 z-columns

In general however, I would advice you to have in mind the picture I have in my 2D dynamic array (C):

which describes in a nutshell what I tried to explain in the start.

As you increase your dimensions, you replace every element of the previous array with an array of the next dimension, while you reach the end.