What is the formula to calculate address in multi dimensional array ?
strongLet us consider your array be A[x][y][z] starting with 0 indices. We have to find address of A[p][q][r] : A[p][q][r] = ( (p*y + q) *z + r) * size + base . if you have array not starting with 0 index . lets say A[x1:x2][y1:y2][z1:z2] then A[p][q][r] = (((p-x1)*(y2-y1+1) +(q-y1))*(z2-z1+1) +(r-z1)) * size + base./strong
ustrongemTwo - Dimensional Array :/em/strong/ubr /A two dimensional Array A is the collection of 'm X n' elements. Programming language stores the two dimensional array in one dimensional memory in either of two ways -br /1) Row Major Order:br /First row of the array occupies the first set of memory locations reserved for the array; Second row occupies the next set, and so forth.br /To determine element address A[i,j]: br / Location ( A[ i,j ] ) =Base Address + ( N x ( I - 1 ) ) + ( j - 1 )br /For example :br /Given an array [1…5,1…7] of integers. Calculate address of element T[4,6], where BA=900.br /Solution:- I = 4 , J = 6 ,M= 5 , N= 7br / Location (T [4,6]) = BA + (7 x (4-1)) + (6-1)br / = 900+ (7 x 3) +5br / = 900+ 21 + 5br / = 926br /2) Column Major Order:br /Order elements of first column stored linearly and then comes elements of next column.br /To determine element address A[i,j]:br / Location ( A[ i,j ] ) =Base Address + ( M x ( j - 1 ) ) + ( i - 1 )br /For example :br /Given an array [1…6,1…8] of integers. Calculate address element T[5,7], where BA=300.br /Solution:- I = 5 , J = 7, M= 6 , N= 8br / Location (T [4,6]) = BA + (6 x (7-1)) + (5-1)br / = 300+ (6 x 6) +4br / = 300+ 36+4br / = 340br /br /strongemuThree - Dimensional Array :/u/em/strongbr /In three - dimensional array also address is calculated through two methods i.e; row-major order and column-major method.br /To calculate address of element X[ i,j,k] using row-major order :br / Location ( X[i,j,k] )=BA + MN (k-1) + N (i-1) + (j-1)br /To calculate address of element X[ i,j,k] using column-major orderbr / Location ( X[i,j,k] )=BA + MN (k-1) + M (j-1) + (i-1)br /For example :br /Given an array [ 1..8, 1..5, 1..7 ] of integers. Calculate address of element A[5,3,6], by using rows and columns methods, if BA=900?br /Solution:- The dimensions of A are :br / M=8 , N=5, R=7, i=5, j=3, k=6br / Rows - wise : br / Location (A[i,j,k]) = BA + MN(k-1) + N(i-1) + (j-1)br / Location(A[5,3,6])= 900 + 8x5(6-1) + 5(5-1) + (3-1)br / = 900 + 40 x 5 +5 x 4 + 2br / = 900 + 200 +20 +2br / = 1122br / Columns - wise : br / Location (A[i,j,k]) = BA + MN(k-1) + M(j-1) + (i-1)br / Location (A[5,3,6]) = 900 + 8x5(6-1) + 8(3-1) + (5-1)br / = 900 + 40 x 5 +8 x 2 + 4br / = 900 + 200 +16 +4br / = 1120br /