Could you please show me an example how to compute the location of an element in a multi-dimensional array.
It is illustrated in Robert Sebesta's book Concept of Programming Languages that location of an element is given by the formula
location(a[i,j]) = address_of(a[1,1]) + ((i - 1)*n + (j - 1))*element_size
where n
is the number of elements per row and a[1,1]
is the base address (1-based indexing).
I tried to find the location of [5,5]
in a 7×7-matrix, assuming element_size=2
. By calculating manually I got the value as 66. But while calculating by using the formula I got the value as 64:
location(a[5,5]) = 0 + ((4*7) + 4)*2 = 64
Is this correct or not? Could you tell me how the difference of two occurred.