135k views
5 votes
Suppose a programming language PLm that supports multi-dimensional arrays. Your program written in PLm contains a two-dimensional array, say A[100,50] (100 rows x 50 columns). You learned that (a) each element of the array requires 2 bytes of memory, and (b) A[0,0] and A[10,1] are stored at memory addresses 1000 and 2002, respectively, on a byte-addressable machine. Does PLm use row-major order or column-major order? Justify your answer by stating the reason. Write a formula that, given valid indexes and j, gives the memory address where A[i,j] is stored. Calculate the memory address of A[10,10].

1 Answer

5 votes

Final answer:

PLm uses row-major order to store multi-dimensional arrays. The address of any element in the array can be calculated using the formula: Address = Base Address + (i * number of columns + j) * size of each element. For A[10,10], the memory address is 2020.

Step-by-step explanation:

The programming language PLm uses row-major order for storing multi-dimensional arrays. We can deduce this because the memory address of A[0,0] is 1000 and the memory address of A[10,1] (which is 10 rows down the first row) is 2002. Since each element takes 2 bytes, the next element in the same row (A[0,1]) would be at address 1002. Thus, the address increases by 2 for each column increment within the same row, indicating row-major order storage.

To compute the memory address where A[i,j] is stored using row-major order, we use the formula: Address of A[i,j] = Base Address + (i * number of columns + j) * size of each element. Here, the base address is 1000, the number of columns is 50, the size of each element is 2 bytes, i is the row index, and j is the column index.

For A[10,10]:

  • Address of A[10,10] = 1000 + (10 * 50 + 10) * 2
  • Address of A[10,10] = 1000 + (510) * 2
  • Address of A[10,10] = 1000 + 1020
  • Address of A[10,10] = 2020
User Caroline Beltran
by
7.8k points