2.9k views

Let $A$ be a two dimensional array declared as follows:

A: array [1 …. 10] [1 ….. 15] of integer;

Assuming that each integer takes one memory location, the array is stored in row-major order and the first element of the array is stored at location $100$, what is the address of the element $A[i][j]$?

1. $15i+j+84$

2. $15j+i+84$

3. $10i+j+89$

4. $10j+i+89$

in DS
edited | 2.9k views
+7

Row Major Order: $A[1.....n1][1.....n2]$
A[i][j] = Base Address + ( (i-1)*no._of_columns + (j-1))*element_Size

Column Major Order: $A[1.....n1][1.....n2]$
A[i][j] = Base Address + ((j-1)*no._of_rows + (i-1))*element_size

+1
Row-Major Order answer is $(A)$ and Column Major Order answer is $(D)$

$A [ LB_1\ldots UB_1,LB_2\ldots UB_2]$

$BA =$ Base address.

$C =$ size of each element.

Row major order.

$Loc(a[i][j]) = BA + [(i-LB_1) (UB_2-LB_2 + 1) + (j - LB_2)] * C$.

Column Major order

$Loc(a[i][j]) = BA + [ (j-LB_2) (UB_1-LB_1+ 1) + (i - LB_1) ] * C$.

Substituting the values.

by Boss (19.9k points)
edited
+10

Yes, in row-major addresses will be like below.

 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 159 160 100 + (i-1)15 + (j-1) 174 175 189 190 204 205 219 220 234 235 249
0

@Gate Keeda Sir will it be always true that in declaration like following, 10 is number of rows and 15 is number of columns

array [1 …. 10] [1 ….. 15]
0
0
What is LB1,LB2 and UB1 etc here ?
0
LB1 lower bound 1 array [1(LB1)....10(UB1)]

In this question, it is asked that A[i][j] is how many memory blocks far from memory location 100 (i.e base address of array). Now to reach 'ith' row we have to pass through i-1 rows each of which contains 15 elements. After that to reach jth column (of ith row) we have to pass through j-1 elements.

Address of A[i][j] = base_address of A + [(i-1)*15 + (j-1)]*1

= 100 + 15i -15 + j - 1

=15i + j + 84

A is the answer.

by Boss (15.9k points)

Let Array be : A[m][n]

Type 1 :

The array indices started with 1 and it is in Row major order :

Base Address + Size * [ (i-1)*n + (j-1) ]

The array indices started with 0 and it is in Row major order

Base Address + Size * [ i*n + j ]

Type 2 :

The array indices started with 1 and it is in Column major order :

Base Address + Size * [ (j-1)*m + (i-1) ]

The array indices started with 0 and it is in Column major order

Base Address + Size * [ j*m + i ]

by Junior (911 points)

1
2