0% found this document useful (0 votes)
0 views12 pages

Lecture 6 Multidimensional Array

Multi-dimensional arrays, particularly 2D arrays, are arrays of arrays used to store data in a tabular format, with elements accessed via their row and column indices. The document explains how to declare, initialize, and calculate the address of elements in both row-major and column-major order for 2D and 3D arrays. It provides examples and formulas for calculating memory addresses based on base address, element size, and indices.

Uploaded by

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

Lecture 6 Multidimensional Array

Multi-dimensional arrays, particularly 2D arrays, are arrays of arrays used to store data in a tabular format, with elements accessed via their row and column indices. The document explains how to declare, initialize, and calculate the address of elements in both row-major and column-major order for 2D and 3D arrays. It provides examples and formulas for calculating memory addresses based on base address, element size, and indices.

Uploaded by

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

Multi Dimensional Arrays

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.

2. A[-5…20, 20…70], Base address = 1020, Size of element = 8 bytes,


Find the location of A[0][30] by column major order.
In three - dimensional array also address is calculated through two methods i.e; row-major order and
column-major method.
To calculate address of element X[ i,j,k] using row-major order :
Location ( X[i,j,k] )=BA + MN (k-1) + N (i-1) + (j-1)
To calculate address of element X[ i,j,k] using column-major order
Location ( X[i,j,k] )=BA + MN (k-1) + M (j-1) + (i-1)
For example :
Given an array [ 1..8, 1..5, 1..7 ] of integers. Calculate address of element A[5,3,6], by using rows and
columns methods, if BA=900?
Solution:- The dimensions of A are :
M=8 , N=5, R=7, i=5, j=3, k=6
Rows - wise :
Location (A[i,j,k]) = BA + MN(k-1) + N(i-1) + (j-1)
Location(A[5,3,6])= 900 + 8x5(6-1) + 5(5-1) + (3-1)
= 900 + 40 x 5 +5 x 4 + 2
= 900 + 200 +20 +2
= 1122
Columns - wise :
Location (A[i,j,k]) = BA + MN(k-1) + M(j-1) + (i-1)
Location (A[5,3,6]) = 900 + 8x5(6-1) + 8(3-1) + (5-1)
= 900 + 40 x 5 +8 x 2 + 4
= 900 + 200 +16 +4
= 1120

You might also like