The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+20 votes
2.3k 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$

asked in DS by Veteran (59.7k points)
edited by | 2.3k views
+3

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

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

3 Answers

+28 votes
Best answer

$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.

Answer is A.

answered by Boss (19.9k points)
edited by
+9

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
+12 votes

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. 

answered by Boss (14.2k points)
+9 votes

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 ]

answered by Junior (999 points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

44,253 questions
49,750 answers
164,093 comments
65,845 users