0% found this document useful (0 votes)
171 views3 pages

Address Calculation 2-D Matrix

There are two common ways to calculate the address of an element in a 2D array: row-major order and column-major order. Row-major order assigns addresses moving across rows then down columns, while column-major order moves down columns then across rows. Formulas are given to calculate addresses based on the base address, element size, and row and column indexes using each ordering scheme. The document also discusses how addresses may differ between the two methods and extends the concepts to 3D arrays and sparse matrices.

Uploaded by

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

Address Calculation 2-D Matrix

There are two common ways to calculate the address of an element in a 2D array: row-major order and column-major order. Row-major order assigns addresses moving across rows then down columns, while column-major order moves down columns then across rows. Formulas are given to calculate addresses based on the base address, element size, and row and column indexes using each ordering scheme. The document also discusses how addresses may differ between the two methods and extends the concepts to 3D arrays and sparse matrices.

Uploaded by

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

To find the address of any element in a 2-Dimensional array there are the

following two ways-


 Row Major Order
 Column Major Order

Row Major Order: Row major ordering assigns successive elements,
moving across the rows and then down the next row, to successive memory
locations. In simple language, the elements of an array are being stored in a
Row-Wise fashion.
To find the address of the element using row-major order use the following
formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))   
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it
as zero),
LC = Lower Limit of column/start column index of the matrix(If not given
assume it as zero),
N = Number of column given in the matrix.
Example: 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
Column Major Order: If elements of an array are stored in column-major
fashion means moving across the column and then to the next column then
it’s in column-major order. To find the address of the element using column-
major order uses the following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))  
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of matrix(If not given assume
it as zero),
M = Number of rows given in the matrix.
Example: 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.
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 M = Upper Bound – Lower Bound + 1
                                                                            = 10 – 1 + 1
                                                                           = 10
Formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
                                  = 100 + 1 * ((5) * 10 + (7))
                                  = 100 + 1 * (57)
Address of A[I][J] = 157 
From the above examples, it can be observed that for the same position two
different address locations are obtained that’s because in row-major order
movement is done across the rows and then down to the next row, and in
column-major order, first move down to the first column and then next
column. So both the answers are right.
So it’s all based on the position of the element whose address to be found
for some cases the same answers is also obtained with row-major order and
column-major order and for some cases, different answers are obtained.

Calculate the address of any element in the 3-D Array: A 3-Dimensional


array is a collection of 2-Dimensional arrays. It is specified by using three
subscripts:
 Block size
 Row size
 Column size
More dimensions in an array mean more data can be stored in that array. 

Sparse Matrix

00304

00570

00000

02600

A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x
n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.

Why to use Sparse Matrix instead of simple matrix ?

Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store
only those elements.

Computing time: Computing time can be saved by logically designing a data structure traversing only
non-zero elements..

You might also like