0% found this document useful (0 votes)
30 views26 pages

5.representation of Arrays in Memory

The document provides an overview of arrays, including their representation in memory through row major and column major orders, and the derivation of index formulae for 1-D, 2-D, and n-D arrays. It explains how to declare arrays, access their elements, and calculate the addresses of specific elements using given formulas. Additionally, it discusses applications of arrays in various data structures and mathematical implementations.

Uploaded by

lkbprakhar27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views26 pages

5.representation of Arrays in Memory

The document provides an overview of arrays, including their representation in memory through row major and column major orders, and the derivation of index formulae for 1-D, 2-D, and n-D arrays. It explains how to declare arrays, access their elements, and calculate the addresses of specific elements using given formulas. Additionally, it discusses applications of arrays in various data structures and mathematical implementations.

Uploaded by

lkbprakhar27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Representation of Arrays

Row Major Order and Column Major Order


Derivation of Index Formulae for 1-D, 2-D,
3-D and n-D Array
1-D Arrays
• Collection of elements of the same data type
• Elements of arrays are stored in consecutive memory
locations and are referenced by an index (also known
as the subscript).
• Declaring an array means specifying three things:
– Data type - what kind of values it can store. For example,
int, char, float
– Name - to identify the array
– Size - the maximum number of values that the array can
hold
• Arrays are declared using syntax:
– type name[size] (e.g. int marks[10];)
Accessing Elements of an Array
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]

• Elements are accessed using an index


– E.g. marks[3] accesses the 4th element
• Loop is used to access all the elements of the
arrays
int i, marks[10];
for(i=0;i<10;i++)
marks[i] = -1;
Address of an Array Element
• Address of data element, A[k] =
BA(A) + w( k – lower_bound), where
– A is the array
– k is the index of the element whose address we have to
calculate
– BA is the base address of the array A
– w is the word size of one element in memory. For example,
size of int is 2
99 67 78 56 88 90 34 85

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]


1000 1002 1004 1006 1008 1010 1012 1014

marks[4] = 1000 + 2(4 – 0)


= 1000 + 2(4) = 1008
Example:- Consider the array A(-5:10). Find the number of elements
and the address of A[2] if the array is stored at location 300 and the
word size is 4
ANSWER
Number of Elements = Upper Bound – Lower
Bound +1 = 16
Location of A[k]= Base(A) + w * (k – Lower
bound)
Location of A[2] = 300 + 4( 2 – (-5))
= 328
2-D Arrays
• Declared as:
– data_type array_name[row_size][column_size];
– E.g. int marks[3][5];
– 2-D m×n array is an array that contains m×n data
elements and each element is accessed using two
subscripts, i and j
– Logically viewed as a table

Col 0 Col 1 Col2 Col 3 Col 4


Rows/Columns
Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4]
Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4]
Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4]
Memory Representation of a 2D Array
• Physically the elements of the
array may be stored as either
– Row Major Order or
– Column Major Order
• Row Major Order: Elements are
stored row by row
• Column Major Order: Elements
are stored column by column
Row major and Column Major Order for 2-D Arrays
• E.g. for the array A[3, 4]
Finding the address of an element in a
2-D array
• Row major
– Address of A[i,j] = B + w * (M * (i-L1) + (j-L2))
• Column major
– Address of A[i,j] = B + w * (N * (j-L2) + (i-L1))
• B : Base address of the array
• w : Size of each element of the array
• N : Number of rows
• M : Number of columns
• How to find address using Row Major Order?
• Given an array, arr[1………10][1………15] with base value 100 and the size
of each element is 1 Byte in memory. Find the address of arr[8][6] with
the help of row-major order.
• Solution:
• Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound +
1
= 15 – 1 + 1
= 15
• Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J –
LC))
• Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6
– 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

Finding the address of an element in a
2-D array
• Location of A[2,3] for a 3 X 4 array with base
address as 300 and word size as 2 will be given
as follows
– Row Major Address = 300 + 2* (4*1+2) = 312
– Column Major Address = 300 + 2*(3*2+1) =314
• It can be seen that
– i-1 is the effective address in 1st dimension (E1)
– j-1 is the effective address in 2nd dimension (E2)
• In general
– K1 & K2 are the indices in in 1st and 2nd dimension
– Length of ith dimension:
Li = upper bound – lower bound +1
– For a given subscript Ki effective index Ei of Li is the number of
indices preceding Ki in the index set
Ei = Ki – lower bound
– RM - loc (A[K1, K2]) = base(A) + w(E1L2 + E2 )
– CM - loc (A[K1, K2]) = base(A) + w(E2L1 + E1)
Memory Representation of a 3D Array
in Row Major Order
Memory Representation of a 3D Array – A[2,4,3]

(1,1,1) (1,1,1)
(1,1,2) (2,1,1)
(1,1,3) (1,2,1)
(1,2,1) (2,2,1) Column Major
(1,2,2) Row Major (1,3,1) Order
(1,2,3) Order (2,3,1) (First index is
(Last index is the fastest
… (1,4,1)
the fastest changing)
… changing) (2,4,1)
(1,4,3) (1,1,2)
(2,1,1) (2,1,2)
… (1,2,2)
… …
… …
(2,4,2) (1,4,3)
(2,4,3) (2,4,3)
Row Major:
A[i][j][k]= [L1….U1] [L2….U2] [L3….U3]

A[i][j][k]=BA+W((i-L1)(U2-L2+1)(U3-L3+1)+
(j-L2)(U3-L3+1)+
(k-L3)

Column Major: interchange L1,U1 L3,U3 and


L3,U3  L1,U1
A[i][j][k]=BA+W((i-L3)(U2-L2+1)(U1-L1+1)+
(j-L2)(U1-L1+1)+
(k-L1)
Multi-dimensional Arrays
• An n dimensional m1 x m2 x m3 x ….. mn array
is a collection of m1×m2×m3× ….. ×mn
elements.
• An element is specified by using n subscripts
as A[K1][K2][K3]…[Kn], where
• K1<=m1 K2<=m2 K3 <= m3……… Kn <= mn
• In general, lower and upper bounds may be
defined for all dimensions (instead of 1 (or 0)
and mi)
Some terms used
• Length of ith dimension:
– Li = upper bound – lower bound +1
• For a given subscript Ki effective index Ei of Li
is the number of indices preceding Ki in the
index set
– Ei = Ki – lower bound
N-D Array Address Calculation
Example
Consider A[-5:10, 5:15], w=2 and base(A) = 100.
Find loc (A[5,10]) using RM and CM
Solution
L1 = 10+5+1 = 16
L2 = 15 -5 +1 = 11
E1 = K1 – lower bound = 5 – (-5) = 10
E2 = K2 – lower bound = 10 – 5 = 5
RM - loc (A[5,10]) = base(A) + w(E1L2 + E2 )
= 100 + 2*(10*11 + 5) = 330
CM - loc (A[5,10]) = base(A) + w(E2L1 + E1)
= 100 + 2*(5*16 + 10) = 280
Example
• Suppose multidimensional arrays A is declared
as A(2:8,-4:5,6:10)
– Find the length of each dimension A
– The number of elements in A
– Assuming base address (A) = 200, W = 4. Find the
effective indices E1, E2, E3 and address of the
element A[5,-1,8]
Solution
L1 = 7, L2 = 10, L3 = 5, No of elements = 7*10*5 = 350
E1 = K1 – lower bound = 5 – 2 = 3
E2 = K2 – lower bound = -1+4 = 3
E3 = K3 – lower bound = 8 – 6 = 2
RM - loc = base(A) + w(E1L2L3+E2L3+E3)
= 200 + 4*(3*10*5+3*5+2) = 868
CM - loc = base(A) + w(E3L2L1+E2L1+ E1)
= 200 + 4*(2*10*7 + 3*7 + 3) = 856
Applications of Arrays
• Arrays are widely used to implement mathematical
vectors, matrices and other kinds of rectangular
tables.
• Many databases include one-dimensional arrays
whose elements are records.
• Arrays are also used to implement other data
structures like heaps, hash tables, deques, queues,
stacks and string. We will read about these data
structures in the subsequent chapters.
• Arrays can be used for dynamic memory allocation.

You might also like