Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Data Structure - Address Calculation of Multi Dimensional Array Question of Pune University (MCA)

---------------------------------- " Data Structure "-------------------------------------
Array

 Address Calculation of Multi Dimensional Array
§ Addressing formula for the element A[i1] [i2] [i3]….. [in] in an N-Dimensional array declared as   A[u1] [u2] [u3]….. [un] is 
(1)   Row Major Form
A[i1] [i2] [i3]….. [in] = B + W * [ ( i1 * u2* u3*….. *un ) + ( i2 * u3*….. *un ) +…. + ( in-1 *un  ) + ( in ) ]           Where    B  = Base Address of an array A[u1] [u2] [u3]….. [un]
W  = Element Size
(2)   Column Major Form
A[i1] [i2] [i3]….. [in] = B + W * [ ( u1 * i2* i3*….. *in ) + ( u2 * i3*….. *in ) +…. + ( un *i2  ) + ( i1 ) ]
                                      Where    B  = Base Address of an array A[u1] [u2] [u3]….. [un]
W  = Element Size

Example : A integer array is defined as X[100][40][40].Find the address of cell X[80][30][20].

Here Form of storage is not specified therefore always consider Row Major form and Base address is not given so assume suitable data.

Solution :  Given ,

                              X is an array of integers so element size (W)  = 2
                              Let  Base Address (B) = 1000
                                                                    To find address of  cell  X[80][30][20]
                 Therefore
                                i1 = 80 , i2 = 30 , i3 = 20
                                                                u1 = 100 , u2 = 40 , u3 = 40          
                  As We Know 
                                                        Formula Of Address Calculation in Row Major Form is -
       A[i1] [i2] [i3]….. [in] = B + W * [ ( i1 * u2* u3*….. *un ) + ( i2 * u3*….. *un ) +…. + ( in-1 *un  ) + ( in ) ]
                                              Where    B  = Base Address of an array A[u1] [u2] [u3]….. [un]                                                     W = Element Size So
Address of  cell  X[80][30][20] =  1000 + 2 * [ (80*40*40) + (30*40) + (20) ]
                                                                                   =  1000 + 2 * [ 128000 + 1200 + 20 ]
                                                                                   =  1000 + 2 * [ 129220 ]
                                                                                   =  1000 + 258440
                                                                                   =  259440
Therefore Address of  cell  X[80][30][20] is 259440 .


No comments:

Post a Comment