Array - for Computer Science and MCA students
Describe the process of address calculation of Multi-Dimensional array.
You can calculate address of Multi-Dimensional by using the following formula.
- row-major order
- column-major order
In row-major order, elements of the rows of the array are stored contiguous and in column-major order, consecutive elements of the columns are contiguous in memory. To be able to use the above formula you have to consider some base address by yourself.
Suppose, the given array is
arr1 [s1] [s2] [s3][s4].....[sn] and you have to find the address of cell
arr [i1] [i2] [i3] [i4].....[in]
Row-major order:
Base address + (i1 * s2 * s3 * s4..... * sn + i2 * s3 * s4 * s5..... * sn + i3 * s4 * s5..... * sn + in-1 * sn + In) * Size of(Data_Type)
Column-major order:
Base address + (in * sn-2 * sn-3 * sn-4..... * s1 + in-1 * sn-2 * sn-3 * sn-4..... * s1 + in-2 * sn-3 * sn-4..... * s1 + I2 * s1 + s1) * Size of (Data_Type)
A character array is defined as char array [200][150][50]. Find the address of cell array [125][80][20].
Solution:
Let us consider base address as 1000.
Row major order formula
= Base address + (i1 * s2 * s3 + i2 * s3 + i3) * Size of (Data_Type)
= 1000 + (125 * 150 * 50 + 80 * 50 + 20) * 1
= 1000 + 937500 + 4000 + 20
= 942520
Column major order formula
= Base Address + (i1 + i2 * s1 + i3 * s2 * s1) * Size of (Data_Type)
= 1000 + (125 + 80 * 200 + 20 * 200 * 150) * 1
= 1000 + (125+16000+600000)
= 617125
A float array is defined as float array [100][150][50]. Find the address of cell array [75][100][45].
Let us consider base address as 3000.
Row major order formula
= Base address + (i1 * s2 * s3 + i2 * s3 + i3) * Size of (Data_Type)
= 3000 + (75 * 150 * 50 + 100 * 50 + 45) * 4
= 3000 + (562500 + 5000 + 45) * 4
= 2273180
Column major order formula
= Base Address+ (i1 + i2 * s1 + i3 * s2 * s1) * Size of (Data_Type)
= 3000 + (75 + 100 * 100 + 45 * 150 * 100) * 4
= 3000 + (10075 + 45 * 15000) * 4
= 2743300