Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Describe the formula of finding the location (address) of particular element in 2D array using example.

Answer:

1.       In case of Column Major Order:

 The formula is:

LOC (A [J, K]) = Base (A) + w [M (K-1) + (J-1)]

 Here

LOC (A [J, K])       :           is the location of the element in the Jth row and Kth column.

Base (A)                :           is the base address of the array A.

w                           :           is the number of bytes required to store single element of the array A.

M                           :           is the total number of rows in the array.

J                             :           is the row number of the element.

K                           :           is the column number of the element.

 E.g.

A 3 x 4 integer array A is as below:

            Subscript           Elements           Address                       

10

20

50

60

90

40

30

80

75

55

65

79

(1,1)

(2,1)

(3,1)

(1,2)

(2,2)

(3,2)

(1,3)

(2,3)

(3,3)

(1,4)

(2,4)

(3,4)

1000

1002

1004

1006

1008

1010

1012

1014

1016

1018

1020

1022

 

 

 

 

 

 

 

 Suppose we have to find the location of A [3, 2]. The required values are:

Base (A)                :           1000

w                           :           2 (because an integer takes 2 bytes in memory)

M                           :           3

J                             :           3

K                           :           2

Now put these values in the given formula as below:

LOC (A [3, 2]) = 1000 + 2 [3 (2-1) + (3-1)]

= 1000 + 2 [3 (1) + 2]

= 1000 + 2 [3 + 2]

= 1000 + 2 [5]

= 1000 + 10

= 1010

 2.       In case of Row Major Order:

 The formula is:

LOC (A [J, K]) = Base (A) + w [N (J-1) + (K-1)]

 Here

LOC (A [J, K])       :           is the location of the element in the Jth row and Kth column.

Base (A)                :           is the base address of the array A.

w                           :           is the number of bytes required to store single element of the array A.

N                           :           is the total number of columns in the array.

J                             :           is the row number of the element.

K                           :           is the column number of the element.

 E.g.

A 3 x 4 integer array A is as below:

            Subscript           Elements           Address                       

10

60

30

55

20

90

80

65

50

40

75

79

(1,1)

(1,2)

(1,3)

(1,4)

(2,1)

(2,2)

(2,3)

(2,4)

(3,1)

(3,2)

(3,3)

(3,4)

1000

1002

1004

1006

1008

1010

1012

1014

1016

1018

1020

1022

 

 

 

 

 

 

 

 Suppose we have to find the location of A [3, 2]. The required values are:

Base (A)                :           1000

w                           :           2 (because an integer takes 2 bytes in memory)

N                           :           4

J                             :           3

K                           :           2

Now put these values in the given formula as below:

LOC (A [3, 2]) = 1000 + 2 [4 (3-1) + (2-1)]

= 1000 + 2 [4 (2) + 1]

= 1000 + 2 [8 + 1]

= 1000 + 2 [9]

= 1000 + 18

= 1018

Share this article   |    Print    |    Article read by 94181 times
Author:
Rohit kakria
I am software developer
Related Articles: No related article
Related Interview Questions: No related interview question