Calculate the address of a random element present in a 2D array, given base address as BA.

Category: Data StructureCalculate the address of a random element present in a 2D array, given base address as BA.
1 Answers
Editor">Editor Staff answered 2 years ago

Row-Major Order: If array is declared as a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array stored in row major order is calculated as,
Address(a[i][j]) = B. A. + (i * n + j) * size
Column-Major Order: If array is declared as a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array stored in column major order is calculated as
Address(a[i][j]) = ((j*m)+i)*Size + BA.