CHAPTER 7

DATA STRUCTURES

 

PROBLEMS

 

7.1       

a)      Elements are characters. 

Element 6 => array[5]  => Memory location  (40 + 5) = 45

b)      Elements are integers.

Each integer is stored in 4 bytes.

Hence, Memory location  40 + ( 4 * 5 ) = 60

        c)   Elements are single precision floating point numbers.

  A single precision floating point number is stored in 32 bits or 4 bytes.

  Hence, Memory location  40 + (  4 * 5 ) = 60

c)      Elements are 7 bytes in length.

  Memory location  40 + (  7 * 5 ) = 75

 

 

7.2       

Element 26 =>  ar[25] = m[ar + 25] = m[54]

Hence, base address of the array = 54 – 25 = 29.

 

 

7.3      Base address of the array:  1000

Formula for Row major order =

 

        Ar_2D[y, x] =

m[ar_2D + size_of_element . ( y – first_row ) . (number_of_cols)

                                       + size_of_element . ( x – first_col )  ]

 

Since first element of each row and each column are numbered 0, in the above formula, first_row = 0 and

               First_col = 0.

 

number_of_rows = 12

number_of_cols = 11

 

Each element of the array is a word means, size_of_element = 4 (bytes).

 

Ar_2D[8,10] = m[1000 + (4 . 8 . 11) + (4 . 10)] = m[1392]

 

The required byte address is 1392.

 

 

 

 

 

7.5   Formula for Column major order =

 

        Ar_2D[y, x] =

m[ar_2D + size_of_element . ( x – first_col )  (number_of_rows)

                                       + size_of_element . ( y – first_row ) ]

 

                        Ar_2D[8,10] = m[1000 + (4 . 10 . 12) + (4 . 8) ] = m[ 1512 ]

The required byte address is 1512.

 

       

        7.6 (a)            MyArray         .byte            0:80

 

              (b)            MyArray         .word            0:80

 

                                    or

 

MyArray             .byte            0:320

 

        7.10 (a)   

                        MyStack          .byte                 0:100

                        Sp                    .word               MyStack

 

                (b) 

MyStack            .word               0:100

                        Sp                    .word               MyStack

           

                        Or

 

            MyStack            .byte                 0:400

                        Sp                    .word               MyStack