You are on page 1of 29

1/29/2018 Row major and Column Major address calculations - CBSE

Row major and Column Major Address calculations


by Admin on October 1, 2012 in Programming

In this tutorial, we are going to learn about how to store elements in a two-dimensional array. For those who
love to learn through videos, jump over to the bottom of the page, there’s a video tutorial for you.

Before starting storing elements of a multi-dimensional array, let’s say I’ve an one dimensional array having
elements like this:

A1, A2, A3….An

These elements are stored in my linear memory space as follows:

A1 A2 A3 A4 ... An

One thing you need to remember is, no matter what type of array it is (1D or multi-dimensional), they will be
always stored linearly in the memory space as above.

Let’s jump to the case of multi-dimensional arrays now. Imagine I’ve a 3×3 matrix like this:

A11 A12 A13


A21 A22 A23
A31 A32 A33

The real problem of storing elements arises here, since elements have to be stored linearly in the memory space,
we have many possible ways to store them. Here are some of the possibilities I could’ve had for the matrix
above.

1. A11 A13 A12 A21 A23 A22 A31 A33 A32 A33

2. A11 A22 A33 A12 A32 A13 A23 A31 A32 A21

… and I could go on filling randomly depending on the no. of elements I’ve in my 2-D array.

Out of all these possible ways, there are two main ways of storing them, they are:

Row Major Ordering


Column Major Ordering

1. Row Major method

In this method, the elements of an array are filled up row-by-row such that the first row elements are stored first,
then the second row and so on.

Most of the high level programming languages like C/C++, Java, Pascal, etc use this method for storing
multidimensional arrays.

Offset for row major ordering

http://www.cbseguy.com/row-column-major-address-calculations-cbse/ 1/3
1/29/2018 Row major and Column Major address calculations - CBSE

As you can see in the 3×3 Matrix array we have above, I want to know at what position is my element A12
located in the linear memory space. This position is called the OFFSET.

One way to do it is to make up a linear diagram like the one below, count manually using 0 as the starting index
and go on till the element I need. But imagine you are given an array like A[20][40], definitely you can’t go over all
these elements if you wanted to know, say where A[15][20] is located.

For this, we resort to a fairly easy mathematical method of calculating the offset.

Here is the formula to calculate the offset for row major ordering

2. Column Major method

In Column Major ordering, all the elements of the first column are stored first, then the next column elements and
so on till we are left with no columns in our 2-D array.

Offset for column major method

Calculating address of a particular element

Depending on the ordering method the elements are stored in the memory, we will get different positions of an
element in the linear memory and consequently a different address for each method. To calculate the address we
use the following procedure:

Step 1: Get the offset value of the element under consideration, make sure you use the correct forumula.
Step 2: Multiply the offset with the size of the element’s datatype (Like int is of 4 bytes for 32-bit, Earlier

http://www.cbseguy.com/row-column-major-address-calculations-cbse/ 2/3
1/29/2018 Row major and Column Major address calculations - CBSE

compilers like TurboC worked on 16-bit platform and for them size of int is 2 bytes).
Step 3: Add this to the base address to get the final address

http://www.cbseguy.com/row-column-major-address-calculations-cbse/ 3/3
1/29/2018 PARWEZ'S BLOG: ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY

ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY

When lower bond is not given.


The computer keeps track of Base (BA),
the address of the first element A[0][0] of A[M][N],
and computes address Loc (A[I][J]) of A[M][N] using the formula

Address can be calculated as Column major order:-

Loc (A[I][J] ) = Base (BA) + W [M x J + I ]

Address can be calculated as Row major order:-

Loc (A [I][J]) = Base (BA) + W [N x I + J ]

W denotes the size, i.e; number of bytes per data element of the array A,
M is total numbers of rows, and N is total number of columns in the array.

When lower bond is given.


Address can be calculated as Row major order:-

Address of A[ I ][ J ]th element = BA + [ N * ( I - LBR) + ( J - LBC) ]


*W

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of columns

Address can be calculated as Column major order:-

Address of a[ I ][ J ]th element = BA + [ ( I - LBR) + M * ( J - LBC) ] * N

Where BA = Base address


W = Number of bytes occupied by each element
M = Number of rows

In case of C language
LBR (Lower bound row) = 0
LBC (Lower bound column) = 0

e.g. Suppose element of array A[4][5] occupies 4 bytes, and the address of the 1st element is 49.
Find the address of the element A(4,3) when the storage is row major.

= BA + [n * (i - LBR) + (j - LBC)] * w
= 49 + [5 * (4 – 0) + (3 - 0)] * 4
= 49 + [23] * 4
= 49 + 92
= 141

http://pakhtar09.blogspot.in/2012/09/address-calculation-for-two-dimensional_29.html 1/1
1/29/2018 programming centre: how to calculate the address of the 2D array

how to calculate the address of the 2D array


Address of a[ i ][ j ]th element (row major)
*********************************************
BA + [ n * ( i - LBR) + ( j - LBC) ] * w

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of columns

Address of a[ i ][ j ]th element (colum major)


*********************************************
BA + [ ( i - LBR) + m * ( j - LBC) ] * w

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of rows

WHAT IS A 2D ARRAY?

A matrix (two dimensional array) is a group of lists, or arrays that are organized into one data set. To understand them,
first look an example of the standard one dimensional array:

Array "A":
16
8
99
0
5

You can reference any point in the "array" by naming the array and following it with a number representing the position
of a piece of data on the list. The first data on the array is represented with a "0", the second with a "1", and so on. For
example, A[2] (in bold) represents 99 because it is the third figure of array A. You could imagine the references of the
above table as the following:

Array "A":
[0]
[1]
[2]
[3]
[4]

A matrix is a set of arrays. Visualize the following table:

Matrix "B":
16 17 9
8 88 74
99 12 21
0 6 40
5 19 18

There are 3 different arrays in a single data set, "B". In Array A, you could reference any data by naming the point on
the list, such as "1" or "3". However, with a matrix, you must give both a row and a column. You can reference data on
a matrix in the following format:

MatrixName[row][column]

http://programmingcentre.blogspot.in/2012/11/how-to-calculate-address-of-2d-array.html 1/2
1/29/2018 programming centre: how to calculate the address of the 2D array

For example, B[3][2] would represent 40 because it is the it is on the fourth row down and the third column. Remember,
matrix references also start at zero, not one! You can reference any of the data on this table with this chart:

Matrix "B":
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
[3][0] [3][1] [3][2]
[4][0] [4][1] [4][2]

Two-dimensional arrays are used everywhere, but are especially prevalent in computer programming and accounting

http://programmingcentre.blogspot.in/2012/11/how-to-calculate-address-of-2d-array.html 2/2
1/29/2018 Application of Arrays

Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. Many databases, small and large, consist of (or include) one-dimensional
arrays whose elements are records.

Arrays are used to implement other data structures, such as

1) Heaps :
We can implement heap as a trees or arrays.

2) Hash tables :
This is used to store information so the information in the hash table basically has two main
components- key and value.

3) Deques :
It is the short form of Double Ended Queue. In this we can add to either sides and remove from
the either sides.

4) Queues :
It is called FIFO(First In First Out) Data Structure. In this the first element added is the first
element that would come out.

5) Stacks :
It is based on LIFO(Last In First Out) Data Structure. In this the very last thing that is added to it
is the first thing which come out.

6) Strings :
String in C is an Array of characters.

7) Lists :
Arrays can be used with different types of lists we will study further in detail.

One or more large arrays are sometimes used to emulate in-program dynamic memory allocation,
particularly memory pool allocation. Historically, this has sometimes been the only way to allocate
"dynamic memory" portably.
Arrays can be used to determine partial or complete control flow in programs, with the help of
different control flow statements as a compact alternative to (otherwise repetitive) multiple IF
statements. They are known in this context as control tables and are used in conjunction with a
purpose built interpreter whose control flow is altered according to values contained in the array.

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Application-of-Arrays 1/1
1/29/2018 Character Strings and its Operations in C

Basically Strings in C is called the array of characters or sequence of characters. They are
terminated by a special character '\0' . For example :- Amit - my name is a String. Here, String is
a collection of the characters of my name like- A, M, I, T. So it is stored like characters in memory
like:
'A' , 'M' , 'I' , 'T' , '\0'.

So now we have the question how we would store the character string to the memory? To
represent multiple but same type of character type string we use arrays. Now to store this we
define an array of character datatype like -
char arrayname [ 10 ] ;
Here, arrayname is the name of an array and 10 is the size of the array.
So, now we have the array of 10 memory blocks and we could store our name in these blocks. The
leftover empty blocks will be null.
A M I T

Now let's see a simple program to show the string in C :-


#include < stdio.h >
void main ( )
{
printf( " This is a simple program to print a string.");
getchar ( ) ;
}

Now, let's learn about operations of strings :

1) Concatenating Strings :-
Concatenation refers to the combining of two strings. Following example demonstrates the use of
concatenation operation :
#include < stdio.h >
#include < string.h > // This contains all the string manipulating functions
void main ( )
{
char string1[] = "Hello ! ";
char string2[] = "This is a program to show the concatenation operation of the string.\n";
strcat(string1 , string2); // Concatenate string1 and string2 in string1
printf("%s", string1);
getchar();
}

2) Copying Strings :-
Copying refers to the copying two strings. Following example demonstrates the use of copying
operation :
#include < stdio.h >
#include < string.h >
void main ( )
{
char string1[] = "This is a program to show the comparing operation of the string.\n";
char string2[] = "";
strcpy(string2,string1);
printf("%s", string2);
getchar();
}

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Character-Strings-and-its-Operations-in-C 1/2
1/29/2018 Character Strings and its Operations in C

3) Getting the length of the String :-


This operations refers to knowing the length of the strings. Following example demonstrates the
use of this operation :
#include < stdio.h >
#include < string.h >
void main ( )
{
char string1[] = "This is a program to show the length of the string.\n";
int length = strlen(string1);
printf("%d", length);
getchar();
}

4) Comparing String :-
Comparing refers to the comparing two strings if they are equal or not. Following example
demonstrates the use of comparing operation :
Here, in this program we will get the output 0 , since both the string are same and of same size.
#include < stdio.h >
#include < string.h >
void main ( )
{
char string1[] = "This is a program to show the comparing operation of the string.\n";
char string2[] = "This is a program to show the comparing operation of the string.\n";
int compare = strcmp(string1,string2);
printf("%d", compare);
getchar();
}

Here, in this program we will get the output -1 , since both the strings are not same and of
different size.
#include < stdio.h >
#include < string.h >
void main ( )
{
char string1[] = "This is a program to show the comparing operation of the string.\n";
char string2[] = "This is a program.\n";
int compare = strcmp(string1,string2);
printf("%d", compare);
getchar();
}

Here, in this program we will get the output 1 , since both the strings are not same but of same
size.
#include < stdio.h >
#include < string.h >
void main ( )
{
char string1[] = "This is a program to show the comparing operation of the string.\n";
char string2[] = "THIS IS A PROGRAM TO SHOW THE COMPARING OPERATION OF THE
STRING.\n";
int compare = strcmp(string1,string2);
printf("%d", compare);
getchar();
}

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Character-Strings-and-its-Operations-in-C 2/2
1/29/2018 Describe the formula of finding the location (address) of particular element in 2D array using example.

Describe the formula of finding the location (address) of particular


element in 2D array using example.

Answer:

1. In case of Column Major Order:

The formula is:

LOC (A [J, K]) = Base (A) + w [M (K-1) + (J-1)]

Here

LOC (A [J, K]) : is the location of the element in the Jth row and Kth column.

Base (A) : is the base address of the array A.

w : is the number of bytes required to store single element of the array A.

M : is the total number of rows in the array.

J : is the row number of the element.

K : is the column number of the element.

E.g.

A 3 x 4 integer array A is as below:

Subscript Elements Address

10 (1,1) 1000

20 (2,1) 1002

50 (3,1) 1004

60 (1,2) 1006

90 (2,2) 1008
Suppose we have to find the location of A [3, 2]. The
required values are:
40 (3,2) 1010
Base (A) : 1000

30 (1,3) 1012 w : 2 (because an integer takes 2


bytes in memory)
(2,3) 1014 M : 3
80
J : 3
(3,3) 1016
75
K : 2
(1,4) 1018 Now put these values in the given formula as below:
55
LOC (A [3, 2]) = 1000 + 2 [3 (2-1) + (3-1)]
(2,4) 1020
http://www.xpode.com/ShowArticle.aspx?ArticleId=500 1/3
1/29/2018 Describe the formula of finding the location (address) of particular element in 2D array using example.

65 = 1000 + 2 [3 (1) + 2]
(3,4) 1022 = 1000 + 2 [3 + 2]
79
= 1000 + 2 [5]

= 1000 + 10

= 1010

2. In case of Row Major Order:

The formula is:

LOC (A [J, K]) = Base (A) + w [N (J-1) + (K-1)]

Here

LOC (A [J, K]) : is the location of the element in the Jth row and Kth column.

Base (A) : is the base address of the array A.

w : is the number of bytes required to store single element of the array A.

N : is the total number of columns in the array.

J : is the row number of the element.

K : is the column number of the element.

E.g.

A 3 x 4 integer array A is as below:

Subscript Elements Address

10 (1,1) 1000

60 (1,2) 1002

30 (1,3) 1004

55 (1,4) 1006

20 (2,1) 1008
Suppose we have to find the location of A [3, 2]. The
required values are:
90 (2,2) 1010
Base (A) : 1000

80 (2,3) 1012 w : 2 (because an integer takes 2


bytes in memory)
(2,4) 1014 N : 4
65
J : 3
(3,1) 1016
50
K : 2
(3,2) 1018 Now put these values in the given formula as below:
40
LOC (A [3, 2]) = 1000 + 2 [4 (3-1) + (2-1)]
http://www.xpode.com/ShowArticle.aspx?ArticleId=500 2/3
1/29/2018 Describe the formula of finding the location (address) of particular element in 2D array using example.

75 (3,3) 1020 = 1000 + 2 [4 (2) + 1]

= 1000 + 2 [8 + 1]
79 (3,4) 1022
= 1000 + 2 [9]

= 1000 + 18

= 1018

http://www.xpode.com/ShowArticle.aspx?ArticleId=500 3/3
1/29/2018 Memory Address Calculation in an Array – Guide For School

Memory Address Calculation in an Array

Address Calculation in single (one) Dimension 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)

Example:

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

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 1/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

= 1020 + 800
= 1820 [Ans]

Address Calculation in Double (Two) Dimensional Array:


While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations.
Therefore, a 2-D array must be linearized so as to enable their storage. There are two alternatives to achieve
linearization: Row-Major and Column-Major.

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given:
(1) Row Major System (2) Column Major System

Row Major System:

The address of a location in Row Major System is calculated using the following formula:

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 2/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

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

Column Major System:

The address of a location in Column Major System is calculated using the following formula:

Address of A [ I ][ J ] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc )]

Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix

Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] ) but if it is
given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and columns are calculated using the
following methods:

Number of rows (M) will be calculated as = (Ur – Lr) + 1


Number of columns (N) will be calculated as = (Uc – Lc) + 1

And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).

Examples:

Q 1. An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500
determine the location of X [15][20].

Solution:

As you see here the number of rows and columns are not given in the question. So they are calculated as:

Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26


Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

(i) Column Major Wise Calculation of above equation

The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26

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

= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160] = 1660 [Ans]

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 3/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

(ii) Row Major Wise Calculation of above equation

The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26

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

= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] = 1500 + 785
= 2285 [Ans]

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 4/4
1/29/2018 Sparse Matrices

Sparse Matrices :
Matrix with maximum zero entries is termed as sparse matrix.

Sparse Matrix is of following types :


1) Lower triangular matrix :- It has non-zero entries on or below diagonal.
2) Upper Triangular matrix :- It has non-zero entries on or above diagonal.
3) Tri-diagonal matrix :- It has non-zero entries on diagonal and at the places immediately
above or below diagonal.

Let us consider a sparse matrix from storage point of view. Suppose that the entire sparse matrix
is stored. Then, a considerable amount of memory which stores the matrix consists of zeroes. This
is nothing but wastage of memory. In real life applications, such wastage may count to
megabytes. So, an efficient method of storing sparse matrices has to be looked into.
A common way of representing non zero elements of a sparse matrix is the 3-tuple form. The
first row of sparse matrix always specifies the number of rows, number of columns and number of
non zero elements in the matrix.

The following program 1.1 accepts a matrix as input, which is sparse and prints the
corresponding 3-tuple representations.
/* The program accepts a matrix as input and prints the 3-tuple representation of it */
#include< stdio.h >
void main()
{
int a[5][5], rows, columns, i, j;
printf("enter the order of the matrix. The order should be less than 5 × 5:\n");
scanf("%d %d",&rows,&columns);
printf("Enter the elements of the matrix:\n");
for(i=0; i < rows; i++)
{
for(j=0; j< columns; j++)
{ scanf("%d",&a[i][j]);
}
printf(“The 3-tuple representation of the matrix is:\n”);
}
for(i=0;i< rows;i++)
{
for(j=0;j< columns;j++)
{ if (a[i][j]!=0) { printf("%d %d %d\n", (i+1),(j+1),a[i][j]);
}
}
}

Output :
Enter the order of the matrix. The order should be less than 5 × 5: 3 3
Enter the elements of the matrix: 1 2 3 0 1 0 0 0 4
The 3-tuple representation of the matrix is:
1 1 1
1 2 2
1 3 3
2 2 1
3 3 4

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Sparse-Matrices 1/1
1/29/2018 Address Calculation

1) One - Dimensional Array :


By the previous definition of 1-Dimensional array, we can say that the compiler limits the storage
region to storing set of element, and the first location is individual element of array , and this
called the Base Address.
For example :
let’s be as 500. Base Address (501) and like for the all elements and used the index I, by its value
are range 1<= I => N according to Base Index (500), by using this relation:
Location ( X[I] ) = Base Address + (I-1)
For example :
When the requirement is to bound the forth element, I = 4 :
Location(X[4])= 500+(4-1)
= 500 +3
= 503
So the address of forth element is 503 because the first element in 500.
When the program indicate or dealing with element of array in any instruction like (write (X [I]),
read (X [I] ) ), the compiler depend on going relation to bounding the requirement address.

2) Two - Dimensional Array :


A two dimensional Array A is the collection of 'm X n' elements. Programming language stores the
two dimensional array in one dimensional memory in either of two ways -

1) Row Major Order:


First row of the array occupies the first set of memory locations reserved for the array; Second
row occupies the next set, and so forth.
To determine element address A[i,j]:
Location ( A[ i,j ] ) =Base Address + ( N x ( I - 1 ) ) + ( j - 1 )
For example :
Given an array [1…5,1…7] of integers. Calculate address of element T[4,6], where BA=900.
Solution:- I = 4 , J = 6 ,M= 5 , N= 7
Location (T [4,6]) = BA + (7 x (4-1)) + (6-1)
= 900+ (7 x 3) +5
= 900+ 21 + 5
= 926

2) Column Major Order:


Order elements of first column stored linearly and then comes elements of next column.
To determine element address A[i,j]:
Location ( A[ i,j ] ) =Base Address + ( M x ( j - 1 ) ) + ( i - 1 )
For example :
Given an array [1…6,1…8] of integers. Calculate address element T[5,7], where BA=300.
Solution:- I = 5 , J = 7, M= 6 , N= 8
Location (T [4,6]) = BA + (6 x (7-1)) + (5-1)
= 300+ (6 x 6) +4
= 300+ 36+4
= 340

2) Three - Dimensional Array :


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

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Address-Calculation 1/2
1/29/2018 Address Calculation

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

http://www.techoschool.com/MCA/Data-Structures-using-C/UNIT-1-Arrays_Address-Calculation 2/2
1/29/2018 PARWEZ'S BLOG: ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY

ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY

When lower bond is not given.


The computer keeps track of Base (BA),
the address of the first element A[0][0] of A[M][N],
and computes address Loc (A[I][J]) of A[M][N] using the formula

Address can be calculated as Column major order:-

Loc (A[I][J] ) = Base (BA) + W [M x J + I ]

Address can be calculated as Row major order:-

Loc (A [I][J]) = Base (BA) + W [N x I + J ]

W denotes the size, i.e; number of bytes per data element of the array A,
M is total numbers of rows, and N is total number of columns in the array.

When lower bond is given.


Address can be calculated as Row major order:-

Address of A[ I ][ J ]th element = BA + [ N * ( I - LBR) + ( J - LBC) ]


*W

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of columns

Address can be calculated as Column major order:-

Address of a[ I ][ J ]th element = BA + [ ( I - LBR) + M * ( J - LBC) ] * N

Where BA = Base address


W = Number of bytes occupied by each element
M = Number of rows

In case of C language
LBR (Lower bound row) = 0
LBC (Lower bound column) = 0

e.g. Suppose element of array A[4][5] occupies 4 bytes, and the address of the 1st element is 49.
Find the address of the element A(4,3) when the storage is row major.

= BA + [n * (i - LBR) + (j - LBC)] * w
= 49 + [5 * (4 – 0) + (3 - 0)] * 4
= 49 + [23] * 4
= 49 + 92
= 141

http://pakhtar09.blogspot.in/2012/09/address-calculation-for-two-dimensional_29.html 1/1
1/29/2018 Memory Address Calculation in an Array – Guide For School

Memory Address Calculation in an Array

Address Calculation in single (one) Dimension 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)

Example:

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

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 1/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

= 1020 + 800
= 1820 [Ans]

Address Calculation in Double (Two) Dimensional Array:


While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations.
Therefore, a 2-D array must be linearized so as to enable their storage. There are two alternatives to achieve
linearization: Row-Major and Column-Major.

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given:
(1) Row Major System (2) Column Major System

Row Major System:

The address of a location in Row Major System is calculated using the following formula:

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 2/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

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

Column Major System:

The address of a location in Column Major System is calculated using the following formula:

Address of A [ I ][ J ] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc )]

Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix

Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] ) but if it is
given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and columns are calculated using the
following methods:

Number of rows (M) will be calculated as = (Ur – Lr) + 1


Number of columns (N) will be calculated as = (Uc – Lc) + 1

And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).

Examples:

Q 1. An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500
determine the location of X [15][20].

Solution:

As you see here the number of rows and columns are not given in the question. So they are calculated as:

Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26


Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

(i) Column Major Wise Calculation of above equation

The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26

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

= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160] = 1660 [Ans]

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 3/4
1/29/2018 Memory Address Calculation in an Array – Guide For School

(ii) Row Major Wise Calculation of above equation

The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26

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

= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] = 1500 + 785
= 2285 [Ans]

http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/ 4/4
1/29/2018 Multidimensional Array: Address Calculation – UNCOVER YOUR SYLLABUS

Multidimensional Array: Address Calculation


My goal: To make you visualize how a multi-dimensional array is stored in computer’s memory and how you
can calculate the address of any random element.

Note: This article is pretty basic and I’ve used 2D array references.
The same goes for other multidimensional arrays too. If you’re not aware
of how we go about referring to particular elements in an array, I’d
recommend you brush your basics up. It’s pretty much like Mathematical
matrices. I have an example beside.

Okay, so I have before me an array and I’ve been given the base address
and I’m being asked to give the address of a particular element.

Most of the books give you formulae for calculating it without explaining what it means and how easy it is. The
formula is my method 1. In our Method 2,we’ll uncover it.

Our multidimensional array is nothing but just another linear array for
the computer.

There’s where the terms, which may sound complicated,


‘Row major’ and ‘Column Major’ come into play.

As the name suggests, in Row major, consecutive elements of a row are stored
in contiguous locations in the memory; in column-major order, consecutive
elements of the columns are in contiguous locations.

So basically your just creating another linear array


with a particular arrangement in mind. And the
address can thus be easily calculated.

I’ve seen them being asked in two forms in


examinations.

Type 1: The number of rows, number of columns and the base address is known to you.Example: A 2-d
array[7×9] is given. Each element requires 2 bytes of space. Address of [1,1] is 1258(I made that up, it can be
anything maybe 1000 or something like that).

1. Give the address of [5,8]

Method 1:(Boring) Use the formula,

The address of [i,j]

Address= Base Address+S(n(i-r)+(j-c))

where S=size of the element, n=number of columns, r=lower limit of rows,c=lower limit of columns.

https://uncoveryoursyllabus.wordpress.com/2015/12/08/multidimensional-array-address-calculation/ 1/2
1/29/2018 Multidimensional Array: Address Calculation – UNCOVER YOUR SYLLABUS

Plug in your values.

Address=1258+2(9(5-1)+(8-1))=1258+2(9×4+7)=1344.

Method 2: (Actual understanding)

It’s stored row major wise so basically to reach [5,8] I have to travel 4 complete rows and then stop at the eighth
element in the third row. My one row had 9 columns. So altogether I skipped 9×4+7 elements to reach my
destination.

Address=1258+2(9×4+7)=1344.

Notice that my form is exactly the same as the formula. The reason is that the second method is actually a
proof of first method. The reason I asked you to do it the second way is because its more intuitive; where you
were being asked to arrange consecutive elements in a particular order and then perform simple arithmetic.

Type 2: An array is given in the form, [-15…20,15…40]. If base address is 100, calculate the address row major
wise of [15,20].

Note: The difference here is that my first element is not [0,0] but [-15,15]. This changes my lower limits. Last
element is [20,40]. So, it’s a pretty big array.

Method 1: (Direct formula)

n=(no. of columns)=26(as it is 40-15+1)

Address=100+2(26(15-(-15))+(20-15))=100+2(30×26+5)=1670

Method 2: We need to skip a total of 30 rows to reach row 15.(Wait, did you guess 29. Think a little. -15 row
also has to be skipped. It’s a total of 30.) That would mean skipping 30×26+5 elements.

So, address=100+2(30×26+5)=1670.

That’s it. You’ve finally learnt how to do it. Best of luck with your problem sets. It’s elementary stuff. Try out the
column major yourself.

I’ll give you the formula in case you want to check your answers.

Address= Base Address+S((i-r)+n(j-c))

where S=size of the element, n=number of rows, r=lower limit of rows, c=lower limit of columns.

https://uncoveryoursyllabus.wordpress.com/2015/12/08/multidimensional-array-address-calculation/ 2/2
1/29/2018 Data Structures: ADDRESS CALCULATION OF ARRAY ELEMENT A [J][K]

Sunday, October 11, 2009


ADDRESS CALCULATION OF ARRAY ELEMENT A [J][K]
The computer keeps track of Base (A), the address of the first element

A[0][0] of A[M][N], and computes address Loc (A[J][K]) of A[M][N] using the formula

Column–Major Order:

Loc (A[J][K] ) = Base (A) + W [M x K + J ]

OR

ROW MAJOR ORDER:

Loc (A [J][K]) = Base (A) + W [N x J + K ]

W denotes the size, i.e; number of bytes per data element of the array A, M is total numbers of rows, and N is total
number of columns in the array.

Posted by Yasir Aziz at 9:00 PM 

5 comments:

piyush devikar December 22, 2012 at 9:06 PM


thx for post...
Reply

Kartikeya Gulati January 16, 2014 at 8:21 AM


Row Major Order:-
Base Address + Element Size[no. of column(i-lower bound of row)+(j-lower bound of column)]

Column Major Order:-


Base Address + Element Size[no. of row(j-lower bound of column)+(i-lower bound of row)]
Reply

http://news401.blogspot.in/2009/10/address-calculation-of-array-element-jk.html 1/1
1/29/2018 programming centre: how to calculate the address of the 2D array

how to calculate the address of the 2D array


Address of a[ i ][ j ]th element (row major)
*********************************************
BA + [ n * ( i - LBR) + ( j - LBC) ] * w

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of columns

Address of a[ i ][ j ]th element (colum major)


*********************************************
BA + [ ( i - LBR) + m * ( j - LBC) ] * w

Where BA = Base address


W = Number of bytes occupied by each element
N = Number of rows

WHAT IS A 2D ARRAY?

A matrix (two dimensional array) is a group of lists, or arrays that are organized into one data set. To understand them,
first look an example of the standard one dimensional array:

Array "A":
16
8
99
0
5

You can reference any point in the "array" by naming the array and following it with a number representing the position
of a piece of data on the list. The first data on the array is represented with a "0", the second with a "1", and so on. For
example, A[2] (in bold) represents 99 because it is the third figure of array A. You could imagine the references of the
above table as the following:

Array "A":
[0]
[1]
[2]
[3]
[4]

A matrix is a set of arrays. Visualize the following table:

Matrix "B":
16 17 9
8 88 74
99 12 21
0 6 40
5 19 18

There are 3 different arrays in a single data set, "B". In Array A, you could reference any data by naming the point on
the list, such as "1" or "3". However, with a matrix, you must give both a row and a column. You can reference data on
a matrix in the following format:

MatrixName[row][column]

http://programmingcentre.blogspot.in/2012/11/how-to-calculate-address-of-2d-array.html 1/2
1/29/2018 programming centre: how to calculate the address of the 2D array

For example, B[3][2] would represent 40 because it is the it is on the fourth row down and the third column. Remember,
matrix references also start at zero, not one! You can reference any of the data on this table with this chart:

Matrix "B":
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
[3][0] [3][1] [3][2]
[4][0] [4][1] [4][2]

Two-dimensional arrays are used everywhere, but are especially prevalent in computer programming and accounting

http://programmingcentre.blogspot.in/2012/11/how-to-calculate-address-of-2d-array.html 2/2

You might also like