Page 307 - Computer science 868 Class 12
P. 307

The output of the preceding program is as follows:
                 Enter array elements
                 Enter Row 1 :
                 1
                 2
                 Enter Row 2 :
                 3
                 4
                 Input Array:
                 1      2      0
                 3      4      0
                 0      0      0
                 Array with Sum:
                 1      2      3
                 3      4      7
                 4      6      0

                     9.6  ADDRESS CALCULATION IN ONE-DIMENSIONAL ARRAY AND
                         TWO-DIMENSIONAL ARRAY
                 Arrays are the contiguous memory locations of the same data type. So, if we know the base address of an array, we
                 can easily calculate the position of the subscript element in the array. According to our syllabus, only calculation of
                 one-dimensional array and two-dimensional arrays.

                 9.6.1 Address Calculation in One-Dimensional Array
                 Let us see the following diagram:

                    int ar[]={10,12,94,87,40};
                 We can calculate the address of an element in an array if we know the base address of the array. Suppose an array is
                 A[], we can find the address of the Ith element using the following formula:

                                                           A[I] = B + W * ( I - LB)

                 where, B is the base address, W is the width of the elements and LB is the lower bound, i.e., the position of the 1st
                 element in an array.

                                                 Lower bound


                                    Subscript        0           1           2           3          4

                                     Value           10          22         94          87          40
                                Memory Address      100         104         108         112        116



                                                    Base Address
                 From the above array, we can derive the following:

                    Address of A[I] = B + W * ( I - LB )
                 B   :   Base Address: 100
                 W  :   Width of each element: 4 bytes (as the array is an integer array).



                                                                                                                       305
                                                                                                              Arrays   305
   302   303   304   305   306   307   308   309   310   311   312