# Calculating the address of an element in an N-dimensional array

**N-Dimensional Arrays:** The N-Dimensional array is basically an array of arrays. As **1-D** arrays are identified as a single index, **2-D** arrays are identified using two indices, similarly, **N-Dimensional** arrays are identified using **N **indices. A multi-dimensional array is declared as follows:

int NDA[S

_{1}][S_{2}][S_{3}]……..[S_{N}];

__Explanation__:

- Here, NDA is the name of the N-Dimensional array. It can be any valid identifier name.
- In the above syntax,
**S**denotes the max sizes of the_{1}, S_{2}, S_{3}……S_{N}**N**dimensions. - The lower bounds are assumed to be zeroes for all the dimensions.
- The above array is declared as an integer array. It can be any valid data type other than integer as well.

__Address Calculation of N-Dimensional Arrays__:

__Assumptions:__

- The N-Dimensional array NDA with the maximum sizes of N dimensions are
**S**._{1}, S_{2}, S_{3}, ………, S_{N} - The element whose address needs to be calculated having indices
**l**respectively._{1}, l_{2}, l_{3}, …………..l_{N} - It is possible that the array indices do not have the lower bound as zero. For example, consider the following array T:
**T[-5…5][2……9][14…54][-9…-2].**

__Explanation__:

- In the above array
**T**, the lower bounds of indices are not zeroes. - So that the sizes of the indices of the array are different now and can be calculated by using the formula:

UpperBound – LowerBound +1

- So, here the S
_{1}= 5 – (-5) + 1 = 11. Similarly, S_{2}= 8, S_{3}= 41 and S_{4}= 8.

For address calculation, the lower bounds are **t _{1}, t_{2}, t_{3}…….t_{N}**. There exist two ways to store the array elements:

- Row Major
- Column Major

**The Column Major formula:**

Address of NDA[I

_{1}][I_{2}]. . . [I_{N}] = BAd + W*[((…E_{N}S_{N-1}+ E_{N-1})S_{N-2}+… E_{3})S_{2}+ E_{2})S_{1}+E_{1}]

- BAd represents the
base addressof the array.- W represents the
widthof the array i.e, the number of dimensions in the array.- Also, E
_{i }is given byE, where l_{i }= l_{i}– t_{i}_{i }and t_{i}are the calculated indexes (indices of array element which needs to be determined) and lower bounds respectively.

**The Row Major formula:**

Address of NDA[I

_{1}][I_{2}]. . . .[l_{N}] = BAd + W*[((E_{1}S_{2}+ E_{2})S_{3}+E_{3})S_{4}….. + E_{N-1})S_{N}+ E_{N}]

**The simplest way to learn the formulas:**

**For row-major:** If width = 5, the interior sequence is * E_{1}S_{2} + E_{2}S_{3} + E_{3}S_{4} + E_{4}S_{5} + E_{5}* and if width = 3, the interior sequence is

**E**. Figure out the pattern in the order and follow four basic steps for the formula of any width:

_{1}S_{2}+ E_{2}S_{3}+ E_{3}- Write the interior sequence.
- Put the closing bracket after each
**E**except the first term. So for width = 5, it becomes

E

_{1}S_{2}+ E_{2})S_{3}+ E_{3})S_{4}+ E_{4})S_{5}+ E_{5}).

- Put all the Opening brackets initially.

((((E

_{1}S_{2}+ E_{2})S_{3}+ E_{3})S_{4}+ E_{4})S_{5}+ E_{5}).

- Incorporate the base address and width in the formula.

The approach is similar for the **Column Major** but the pattern of the interior sequence is reverse of the row-major pattern.

**For column-major:** If width =5, the interior sequence is **E _{5}S_{4} + E_{4}S_{3} + E_{3}S_{2}+ E_{2}S_{1} + E_{1}**.

**Example:** Let’s take a multi-dimensional array **A[10][20][30][40]** with the base address **1200**. The task is to find the address of element **A[1][3][5][6]**.

Here, BAd = 1200 and width = 4.

S1 = 10, S2 = 20, S3 = 30, S4 = 40

Since the lower bounds are not given, so lower bounds are assumed to be zero.

E1 = 1 – 0 = 1;

E2 = 3 – 0 = 3;

E3 = 5 – 0 = 5;

E4 = 6 – 0 = 6.

Any of the techniques (Row-major or column-major) can be used for calculating the answer (unless specified).

By applying the formula for row-major, directly write the formula as:

A[1][3][5][6] = 1200 + 4(((1 × 20 + 3)30 +5)40 + 6)

=1200 +4((23 × 30 +5)40 +6)

=1200 + 4(695 × 40 + 6)

=1200 + (4 × 27806)

=112424.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.