0

Find address of element of a 3D array stored row-major

If the following array is stored in row-major order and starts from memory address 300, what is the specification of element A[3,4,2] in memory? Var A: array [1..4,2..5,1..6] of integers

28th Jun 2024, 4:59 PM
safir safir1
safir safir1 - avatar
1 Réponse
+ 1
The language appears to be Pascal. Please add the language in the tags. In row-major order, memory is organized as A[plane, row, column]. The column changes fastest, then row, then plane. Note that the dimensions are offset by starting indices of [1, 2, 1] and top index is inclusive. The actual sizes of each dimension are [4-1+1, 5-2+1, 6-1+1] = [4, 4, 6]. A[3,4,2] starts at address 300, skips to the 3rd plane, finds row 4 (the 3rd row due to starting offset 2), and locates the 2nd column. In the calculation, each index must subtract the starting offset. The size of each plane and each row must also account for the starting offset. A[i, j, k] startAddr + ((i-1)*(rows*cols) + (j-2)*(cols) + (k-1))*SizeOf(Integer) A[3, 4, 2] 300 + ((3-1)*(5-2+1)*(6-1+1) + (4-2)*(6-1+1) + (2-1))*SizeOf(Integer) I recommend that you check and verify this. I could have made a mistake somewhere.
29th Jun 2024, 7:39 AM
Brian
Brian - avatar