Suppose that a C array has been declared as follows: int a[5][8][4]; (a) Give th
ID: 3804345 • Letter: S
Question
Suppose that a C array has been declared as follows:
int a[5][8][4];
(a) Give the access function for this array, assuming that the address of a is 1000 and
int values occupy four bytes.
(b) What is the address of a[2][4][3]?
Here are the notes:
Implementation of Array Types The code to allow accessing of array elements must be generated at compile time. At run time, this code is executed to produce element addresses Suppose that list is a one-dimensional array whose lower subscript bound is 0. The following access function will compute the address of list [k] address (list [k]) address list [0 k element size The first operand of the addition is the constant part and the second is the vari- able part. If the element type is statically bound and the array is statically bound to storage, then the value of the constant part can be computed before mn time. However, the addition and multiplication operations must be done at run time. The access function can be generalized to allow an arbitrary lower bound address (list [k]) address (list [lower bound] (k-lower bound) element size) Next, it is rewritten so that it consists of a constant part and a variable part: address(list [k]) address(list [lower bound] lower bound element size) (k element size If the address of the anray is not known until run time, the subtraction must be done when the array is allocated. A one-dimensional array will require acompile-time descriptor. A mn-time descriptor may be also be needed if mn-time checking of index ranges is performed or if any attributes of the array are dynamic If the subscript ranges are static, the ranges can be incorporated into the code that does the checking, eliminating the need for the run-time descriptor.Explanation / Answer
Given Array is A[5][8][4]. Consider it as 2D array of 5 rows and 7 columns arranged in 4 layers thereby making it a 3D array.
As given in the notes in multidimensional array address of the element is base address plus element size times the number of elements preceding it.
Considering the 3D array in row major form:
Location(a[i][j][k])= base address of a[0][0][0] +(((number of rows above ith row)*row size) + ((number of columns above jth column)*column size) + (number of elements left of kth level)) * element size
For array of size L*M*N
Location(a[i][j][k])=base address of a[0][0][0]+(M*N *(i)+N*(j)+(k))* element size
Generalizing to arbitary lower bounds (x,y and z):
Location(a[i][j][k])=base address of a[x][y][z]+(M*N *(i-x)+N*(j-y)+(k-z))* element size
Similarly for Column Major Array:
Location(a[i][j][k])=base address of a[x][y][z]+(M*N*(i-x)+M*(k-z)+(j-y))* element size
Now Considering the given question where L=5, M=8, N=4, i=2,j=4,k=3, base_address=1000 and element_size=4. Assuming base to be [0][0][0]
If Array is row major(Arrays are row major by default):
Location(a[2][4][3])= 1000+(8*4 *(2)+4*(4)+(3))* 4 =1332
If Array is column major:
Location(a[2][4][3])= 1000+(8*4*(2)+8*(3)+(4))* 4 = 1368
In C Arrays are by default stored in row major form, Hence answer is 1332.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.