Lecture 6 Multidimensional Array
Lecture 6 Multidimensional Array
Multi-dimensional
array
• A multi-dimensional array is an array of
arrays. 2-dimensional arrays are the
most commonly used. They are used to
store data in a tabular manner.
• Consider following 2D array, which is of
the size 3×5. For an array of size N×M,
the rows and columns are numbered
from 0 to N−1 and columns are
numbered from 0 to M−1, respectively.
Any element of the array can be
accessed by arr[i]
[j] where 0≤i<N and 0≤j<M. For
example, in the following array, the value
stored at arr[1][3] is 14.
Declaration and
Initialization
• To declare a 2D array, you must specify the
following:
Row-size: Defines the number of rows
Column-size: Defines the number of columns
type arr[row_size][column_size];
int array[3][5];
• An array can either be initialized during or
after declaration. The format of initializing an
array during declaration is as follows:
type arr[row_size][column_size]={{element1},{element2}…};
int array[3][5]={{5,12,17,9,3}, {13,4,8,14,1},{9,6,3,7,21}};
• The size of a two dimensional array is equal to the
multiplication of number of rows and the number of
columns present in the array. We do need to map two
dimensional array to the one dimensional array in order
to store them in the memory.
• There are two main techniques of storing 2D array
elements into memory:
• Row major
• Column Major
Calculating the Address of the
random element of a 1D array
• Array of an element of an array say “A[ I ]” is calculated
using the following formula:
Address of A [ I ] = B + W * ( I – LB )
Where,
B = Base address
W = Storage Size of one element stored in the array (in
byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not
specified assume 0 (zero)
• Given the base address of an array B[1300…..1900] as
1020 and size of each element is 2 bytes in the memory.
Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I =
1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
Address of
element in 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?
Address of
element in
column 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
column-major order.
1. A[10…30, 55…75], Base address = 0, Size of element = 4 bytes, Find
the location of A[15][68] by row major order.