0% found this document useful (0 votes)
651 views

Address Calculation

The document discusses address calculation for linear and multi-dimensional arrays. It provides formulas to calculate the address of an element based on its indices, the base address, and data type. For single dimensional arrays, the address is the base address plus the width times the index minus the lower bound. For two dimensional arrays, the address can be calculated row-major or column-major using similar formulas involving the number of rows and columns, indices, and data width. Examples are provided to demonstrate calculating addresses for elements in single and multi-dimensional arrays stored in memory.

Uploaded by

Madhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
651 views

Address Calculation

The document discusses address calculation for linear and multi-dimensional arrays. It provides formulas to calculate the address of an element based on its indices, the base address, and data type. For single dimensional arrays, the address is the base address plus the width times the index minus the lower bound. For two dimensional arrays, the address can be calculated row-major or column-major using similar formulas involving the number of rows and columns, indices, and data width. Examples are provided to demonstrate calculating addresses for elements in single and multi-dimensional arrays stored in memory.

Uploaded by

Madhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Address Calculation

Linear data structure is the logical structure created in memory, to contain


data linearly or sequentially. A well known structure is an array

The number of bytes occupied in the memory by an array element depends


upon its data type. Hence, to access appropriate data we must know its
physical address in the memory.

This is the reason why, address calculation is essential to work with data
values.

Types of Array
1.Single Dimensional (1-D)
2.Double dimensional(2-D)
Number of bytes occupied by an element in the memory as per its data typ
is below:

Byte=8 bits
boolean : 1 byte
short: 2 bytes
char type: 2 bytes
int type: 4 bytes
float type: 4 bytes
double : 8 bytes
long type: 8 bytes
Address Calculation in Single Dimensional Array

Let the array be Arr. The address of an element Arr[I] can be calculated by using the following formula:
Address= B + W(I - LB)
where.
W = Word width (Number of bytes an element occupies in the memory).
I = index/Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Example1:
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 B [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
Example 2:
A single dimensional array Arr contains 10 integer numbers.If the base address of the array is 1000, find the address o
sixth and eighth elements of the array.
Solution:B =1000
I= 5 (Subscript of the 6th element is 5 because lowest cell number of the array is 0)
W = 4 (Word width of integer number is 4)
L = 0 (Lowest cell number is taken to be 0 by default)

Address of 6th element = B + W (I-L) Subscript of the eighth element (i) = 7


=1000+4(5-0) Address of the eighth element=B+W (I-L)
=1000+20=1020 = 1000 + 4(7 - 0)= 1000 + 28=1028
AddressCalculation 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-
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:
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 t
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).
Example1: A double dimensional array Arr[4][10] contains characters. If Arr[0][0] IS stored at the
memory location 200, find the address of matrix Arr [3|[7].If matrix is
Row major form
Column major form
Solution:
B = 200
W=2
Lr = 0
Lc= 0
I=3
J=7
M=4
N = 10
Row-wise calculation:
Address of A [ 3 ][ 7 ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 200 + 2[10(3 - 0) + (7 - 0)] Column-wise calculation:
= 200 + 2(30 + 7) Address of A [ 3 ][ 7] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc
= 200 + 2 * 37 = 200 + 2[(3 - 0) + 4(7 - 0)]
= 274 = 200 + 2(3 + 28)
= 200 + 2 * 31
= 200 + 62
= 262
Example 2: A double dimensional array Arr[- 10…12, - 15…..20} contains elements in such that each element occupie
bytes memory space. If the base address of Array is 2000,find the address of the array element Arr[- 6][8] .Perform the
calculations assuming the elements are row major oriented Column major oriented
Solution
B = 2000
I=-6
J= 8
Lr= - 10
Lc= - 15
Number of rows (M) = [12 - (- 10)] + 1 = 23
Number of columns (N)= [20-(-15) ]+ 1 = 36
W=4
Row wise calculation:
Address of array element Arr[- 6][8] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 2000 + 4[36(- 6 - (- 10)) + (8 - (- 15))] Column wise calculation:
= 2000 + 4[36(- 6 + 10) + (8 + 15)] Address of array Arr[-6][8] = B + W * [( I – Lr ) + M * ( J – L
2000 + 4[(36 * 4) + 23] =2000 + 4[(- 6 - (- 10)) + 23(8 - (- 15))]
2000 + 4(144 + 23) = 2000 + 4[(- 6 + 10) + 23(8 + 15)]
= 2000 + 4 * 167 = 2000 + 4[4 + 23 * 23)
= 2000 + 668 = 2000 + 4(4 + 529)
= 2668 = 2000 + 4 * 533
= 2000 + 2132
= 4132
Example 3: A double dimensional array Arr[4][4] contains floating type elements. If Arr[2][2] is stored at memory
location 2000, find the base address of the array. Assume that the array is row major oriented.

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

Solution:I=2
J=2
Lr=0
Lc=0
N=4
B=?

2000=B+4[4(2-0)+(2-0)]
2000= B+4[4*2+2]
2000=B+40
B=2000-40
=1960
A matrix N[11][8] is stored in the memory with each element requiring 2 bytes of storage. If the base
address at N[2][3] is 2140, find the address of N[7][5] when the matrix is stored in Row Major Wise.
Given B = 2140
W = 2 bytes
i = 7, j = 5, Lr = 2, Lc = 3, Ur = 11, Uc = 8
n = Uc – Lc + 1 = 8 – 3 +1 = 6
N [i][j] = B + W [n (i – Lr) + (j – Lc)]
N[7][5] = 2140 + 2 [6 (7 – 2) + (5 – 3)]
= 2140 + 2 [6 (5) + 2]
= 2140 + 2 [30 + 2]
= 2140 + 2 (32)
= 2140 + 64
N[7][5] = 2204

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy