Important Links: Array Addressing , Array Sorting Algorithms, Searching an Array
Address Calculation for Linear Array
To calculate the location/address of an element (say p) of one-dimensional array A the formula is:-
Location of A[p] = Base + (p - LB) * S
Where Base = address of the first element of the array also known as the Base address.
S = number of bytes occupied by each element.
e.g. Suppose each element of array Marks occupies 4 bytes, and the address of the 1st element is 493 then the address of the 3rd element will be:-
Location of Marks[2] = 493 + (2-0) *4
= 493 + 8
= 501
ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY
We have already known about the base address of the array, we can calculate the address of any element in the m X n array having m rows and n columns.
Formula for address calculation in C language:-
1. 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
2. Address can be calculated as Column major order:-
Address of a[ i ][ j ]th element = BA + [ ( i - LBR) + m * ( j - LBC) ] * w
Where BA = Base address
W = Number of bytes occupied by each element
N = Number of rows
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
Exercises For Practice
1. Assume that each element of an array a stored in row major order occupies 4 units of storage. If a is declared as by each of the following and the address of the first element of a is 100, find the address of the element array element:-
(i) int a[10][5] address of a[2][1]
(ii) int a[5][20] address of a[5][10]
2. An array A[10][10] is stored in the memory with each element occupying 4 bytes of space. Assuming the base address of A to be 1000, compute the address of A[2][4] when the array is sorted (i) row wise (ii) column wise.
3. A two-dimensional array A defined as A[2..6, 1..4] is stored in row major order. Calculate the address of A[5, 3] if each element requires two words of storage space. Base address is 1000.
---------------
Email-Id : vinay.kr.saini@gmail.com
Connect with me



Visit My Blog: http://vinay-kumar-saini.blogspot.in/
Address Calculation for Linear Array
To calculate the location/address of an element (say p) of one-dimensional array A the formula is:-
Location of A[p] = Base + (p - LB) * S
Where Base = address of the first element of the array also known as the Base address.
S = number of bytes occupied by each element.
e.g. Suppose each element of array Marks occupies 4 bytes, and the address of the 1st element is 493 then the address of the 3rd element will be:-
Location of Marks[2] = 493 + (2-0) *4
= 493 + 8
= 501
ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY
We have already known about the base address of the array, we can calculate the address of any element in the m X n array having m rows and n columns.
Formula for address calculation in C language:-
1. 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
2. Address can be calculated as Column major order:-
Address of a[ i ][ j ]th element = BA + [ ( i - LBR) + m * ( j - LBC) ] * w
Where BA = Base address
W = Number of bytes occupied by each element
N = Number of rows
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
Exercises For Practice
1. Assume that each element of an array a stored in row major order occupies 4 units of storage. If a is declared as by each of the following and the address of the first element of a is 100, find the address of the element array element:-
(i) int a[10][5] address of a[2][1]
(ii) int a[5][20] address of a[5][10]
2. An array A[10][10] is stored in the memory with each element occupying 4 bytes of space. Assuming the base address of A to be 1000, compute the address of A[2][4] when the array is sorted (i) row wise (ii) column wise.
3. A two-dimensional array A defined as A[2..6, 1..4] is stored in row major order. Calculate the address of A[5, 3] if each element requires two words of storage space. Base address is 1000.
---------------
Vinay Kumar Saini
Assistant Professor (I.T Department)
Maharaja Agrasen Institute of Technology (MAIT)
Sector-22, Rohini, Delhi-110086, IndiaEmail-Id : vinay.kr.saini@gmail.com
Connect with me



Visit My Blog: http://vinay-kumar-saini.blogspot.in/
No comments:
Post a Comment