Docsity

Memory Storage and Array Address Computation, Summaries of Data Structures and Algorithms

Partial preview of the text

Download Memory Storage and Array Address Computation and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Array

Array ADT

• The simplest but useful data structure.

• Assign single name to a homogeneous collection of

instances of one abstract data type.

– All array elements are of same type, so that a

pre−defined equal amount of memory is

allocated to each one of them.

• Individual elements in the collection have an

associated index value that depends on array

dimension.

float marks[10];

Memory Storage

Memory Storage – One Dimensional Array

int student[4];

float marks[3];

student[1]

student[0] student[2]

student[3]

marks[1]

marks[0] marks[2]

Contd…

• Row-major order

Row0 Row1 Row

• Column-major order

Col0 Col1 Col2 Col3 Col

(0,0) (0,1) (0,2) (0,3) (0,4) (1,0) (1,1) (1,2) (1,3) (1,4) (2,0) (2,1) (2,2) (2,3) (2,4)

(0,0) (^) (1,0) (2,0) (0,1) (^) (1,1) (2,1) (0,2) (^) (1,2) (2,2) (0,3) (^) (1,3) (2,3) (0,4) (^) (1,4) (2,4)

Array Address Computation

1D array – address calculation

• Let A be a one dimensional array.

• Formula to compute the address of the

Ith^ element of an array (A[I]) is:

Address of A[I] = B + W * ( I – LB )

[0] 1

[1] 2

[2] 3

[3] 4

[4] 5

[5] 6

B

W

Given: B = 100, W = 4, and LB = 0

A[0] = 100 + 4 * (0 – 0) = 100

1D array – address calculation

• Let A be a one dimensional array.

• Formula to compute the address of the

Ith^ element of an array (A[I]) is:

Address of A[I] = B + W * ( I – LB )

[0] 1

[1] 2

[2] 3

[3] 4

[4] 5

[5] 6

B

Given:

B = 100, W = 4, and LB = 0 A[1] = 100 + 4 * (1 – 0) = 104 A[2] = 100 + 4 * (2 – 0) = 108 A[3] = 100 + 4 * (3 – 0) = 112 A[4] = 100 + 4 * (4 – 0) = 116 A[5] = 100 + 4 * (5 – 0) = 120

Example – 2

• If LB = 5, Loc(A[LB]) = 1200, and W = 4.

• Find Loc(A[8]).

Address of A[I] = B + W * ( I – LB )

Loc(A[8]) = Loc(A[5]) + 4 * (8 – 5)

Example – 3

• Base address of an array B[1300…..1900] is 1020

and size of each element is 2 bytes in the memory.

Find the address of B[1700].

Address of A[I] = B + W * ( I – LB )

• Given: B = 1020, W = 2, I = 1700, LB = 1300

Address of B[1700] = 1020 + 2 * (1700 – 1300)

2D Array – Address Calculation

  • If A be a two dimensional array with M rows and N

columns. We can compute the address of an element at Ith

row and Jth^ column of an array ( A[I][J] ).

B = Base address/address of first element, i.e. A[LBR][LBC] I = Row subscript of element whose address is to be found J = Column subscript of element whose address is to be found W = Number of bytes used to store a single array element LBR = Lower limit of row/start row index of matrix, if not given assume 0 LBC = Lower limit of column/start column index of matrix, if not given assume 0 N = Number of column of the given matrix M = Number of row of the given matrix

[0] [1] [2] [3] [0] (^1 2 3 ) [1] (^5 6 7 ) [2] (^9 10 11 )

Row-major

Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

Address of A[2][1] = B + W * (4 * (2 – 0) + (1 – 0))

M = 3 N = 4

Address of A[2][1] = B + W * (4 * (2 – 0) + (1 – 0))

Address of A[2][1] = B + W * (4 * (2 – 0) + (1 – 0))

Address of A[2][1] = B + W * (4 * (2 – 0) + (1 – 0))

Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

Contd…

• Row Major

Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

• Column Major

Address of A [I][J] = B + W * (( I – LBR ) + M * ( J – LBC ))

• Note: A[LBR…UBR, LBC…UBC]

M = (UBR – LBR) + 1

N = (UBC – LBC) + 1

Example – 4

  • Suppose elements of array A[5][5] occupies 4 bytes, and

the address of the first element is 49. Find the address of the element A[4][3] when the storage is row major. Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))

  • Given: B = 49, W = 4, M = 5, N = 5, I = 4, J = 3, LBR = 0,

LBC = 0. Address of A[4][3] = 49 + 4 * (5 * (4 – 0) + (3 - 0)) = 49 + 4 * (23) = 49 + 92 = 141