0% found this document useful (0 votes)
51 views32 pages

3. Index Formula Derivation of 1D Array

The document provides a comprehensive overview of data structures, focusing on the derivation of index formulas for one-dimensional, two-dimensional, and three-dimensional arrays. It includes examples of address calculations and memory allocation for various array configurations, emphasizing the differences between row-major and column-major order storage. Additionally, it explains the rationale behind zero-based indexing in programming languages.
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)
51 views32 pages

3. Index Formula Derivation of 1D Array

The document provides a comprehensive overview of data structures, focusing on the derivation of index formulas for one-dimensional, two-dimensional, and three-dimensional arrays. It includes examples of address calculations and memory allocation for various array configurations, emphasizing the differences between row-major and column-major order storage. Additionally, it explains the rationale behind zero-based indexing in programming languages.
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/ 32

DATA STRUCTURE

Quality Content for Outcome based Learning

Index Formula Derivation for Array

One Dimensional Array

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Number of elements

Suppose there is a one-dimensional array A[L : U]

Number of elements /lengths of the array can be found by the formula

N=U–L+1

Where U = Upper Bound of the array (Last index)


L = Lower Bound of the array (First index)

A[0:9] will contain 9-0+1 =10 elements

Dr. Amrita Jyoti 2


Copyright © 2021, ABES Engineering College
Address calculation in One Dimensional Array

Address of A[i] = Base Address +w*(i – Lower Bound)

Address of A[i]= BA + W(i-LB)

where w= size of the each element

Dr. Amrita Jyoti 3


Copyright © 2021, ABES Engineering College
Example- 1D
Given A [-1:10], bytes per cell = 4, base address = 2000 find the number
of elements and the address of A [7].
Solution:

Dr. Amrita Jyoti 4


Copyright © 2021, ABES Engineering College
Index Formula Derivation in One Dimensional Array

Question 1: Given A [-1:10], bytes per cell = 4, base address = 2000 find
the address of A [7].
Solution:
Here, i = 7
w= 4
Lower Bound = -1
Upper Bound = 10
Address of A [i] = Base Address +w*(i-Lower Bound)
Address of A [7] = 2000 + 4*(7-(-1)) = 2032

Dr. Amrita Jyoti 5


Copyright © 2021, ABES Engineering College
Example- 1D
Given A [1:15], bytes per cell = 3, base address = 1000 find
he address of A [9].

Solution:

Dr. Amrita Jyoti 6


Copyright © 2021, ABES Engineering College
Index Formula Derivation in One Dimensional Array

Question 2:Given A [1:15], bytes per cell = 3, base address = 1000 find
the address of A [9].
Solution:
Here, i = 9
w= 3
Lower Bound = 1
Upper Bound = 15
Address of A [i] = Base Address +n*(i-Lower Bound)
Address of A [9] = 1000 + 3*(9 - 1) = 1024

Dr. Amrita Jyoti 7


Copyright © 2021, ABES Engineering College
Example -1D
For A[5:55], Base address= 300, w=4
(i)Find the number of elements?
(ii) Total memory allocated.
(iii) Find the address of A[15], A[40] and A[60]?

Dr. Amrita Jyoti 8


Copyright © 2021, ABES Engineering College
Index Formula Derivation in One Dimensional Array

For A[5:55], Base address= 300, w=4


Find the number of elements- 51
address of A[15]- 340
A[40]- 440
A[60] exceeded the size of array hence address does not exist

Dr. Amrita Jyoti 9


Copyright © 2021, ABES Engineering College
Index Formula Derivation in One Dimensional Array

For A[1:7], Base address= 400, w=4


Find the number of elements?
Total memory allocation?
Find the address of A[2], A[3] and A[0]?

Dr. Amrita Jyoti 10


Copyright © 2021, ABES Engineering College
Index Formula Derivation in One Dimensional Array

For A[1:7], Base address= 400, w=4


number of elements-7
Total memory allocation=28
address of A[2]- 404
A[3] -408
A[0]-not exist

Dr. Amrita Jyoti 11


Copyright © 2021, ABES Engineering College
Question 3: Why does indexing in most of the languages start with 0?
Solution:
In address calculation we can skip the offset value. E.g. A[10] =
{1,2,3,4,5,6,7,8,9,10}. If we start the indexing with 1, calculation of address of
A[5] = 1000+(5-1)*2
If we start the indexing with 0, calculation of address of A[5] = 1000+(5-0) *2.
This can directly be written as 1000+5*2. In this case, we do not need to
perform the additional arithmetic operation i.e. subtraction.
(5-1) in the above computations is known as extra subtraction or offset value.

Dr. Amrita Jyoti 12


Copyright © 2021, ABES Engineering College
Row Major Order Arrangement

 Suppose we have a 2–D array


A[L1:U1, L2:U2] given as shown:

 Row side indices are L1, L1+1,


L1+2, …, U1–1, U1.
 Column side indices are L2,
L2+1, L2+2, …, U2–1, U2.

Dr. Amrita Jyoti 13


Copyright © 2021, ABES Engineering College
2 D-Row Major Order Arrangement

 In memory it will look like 1-D array as it will be stored row-wise.

Dr. Amrita Jyoti 14


Copyright © 2021, ABES Engineering College
2 D-Address calculations

Row major

Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]


R= UB1-LB1 +1
C= UB2- LB2 + 1
Total elements= R X C

Dr. Amrita Jyoti 15


Copyright © 2021, ABES Engineering College
2 D-Address calculations

Row major

Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]


R= UB1-LB1 +1
C= UB2- LB2 + 1
Total elements= R X C
Column major

Address of A[i][j]= BA + w [(j-LB2 ) X R + (i-LB1)]

Dr. Amrita Jyoti 16


Copyright © 2021, ABES Engineering College
Example- 2 D
Given A[ 2:5 :: -3:11], base address= 200, W=2 bytes
i) Find total number of elements?
ii)Total memory allocated?
iii) Address of A[3][0] and A[1][5] [Row Major]

Dr. Amrita Jyoti 17


Copyright © 2021, ABES Engineering College
2 D-Address calculations

Given A[ 2:5 :: -3:11]


B[A]= 200, W=2 bytes

R=5-2+1=4
C=11+3=1=15
total number of elements= 60
Total memory allocated= 120 bytes
Address of A[3][0] = 236
and A[1][5]= Not exist

Dr. Amrita Jyoti 18


Copyright © 2021, ABES Engineering College
2 D-Address calculations
Given A[ -2:2 :: 2:22], Base address= 400, W=2 bytes
i)Find total number of elements?
ii)Total memory allocated?
iii) Address of A[1][20] (column major order)

Dr. Amrita Jyoti 19


Copyright © 2021, ABES Engineering College
Three Dimensional Array
• An array represented in the form of 3
different dimensions, is called 3-D Array

Declaration of three-dimensional array:


Syntax: <Data Type> <Arrayname> [m][n][o]
Where,
m  1st Dimension
n  2nd Dimension
o  3rd Dimension

• A[3][4][5], it will have a total of 3 x 4 x 5 = 60


elements.
Dr. Amrita Jyoti 20
Copyright © 2021, ABES Engineering College
Three Dimensional Array
Memory Representation
Row Major Representation

• The first dimension is considered as row.

Column Major Representation

Dr. Amrita Jyoti 21


Copyright © 2021, ABES Engineering College
Three Dimensional Array

1D
Row Major
Address of A[i]= BA + W(i-LB)
2D
Row Major
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]

3D
Row major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]

Dr. Amrita Jyoti 22


Copyright © 2021, ABES Engineering College
Three Dimensional Array
1D
Row Major
Address of A[i]= BA + W(i-LB)

2D
Row Major
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]
Column Major
Address of A[i][j]= BA +w [(j-LB2) X R + (i-LB1)]
3D
Row Major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]
Column Major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i3-L3)r2 + (i2-L2)]
Dr. Amrita Jyoti 23
Copyright © 2021, ABES Engineering College
Example-3D
Example: Given an array, arr[1:9, -4:1, 5:10] with a base value
of 400 and the size of each element is 2 Bytes in memory find the
address of element arr[5][-1][8] with the help of row-major
order?

Dr. Amrita Jyoti 24


Copyright © 2021, ABES Engineering College
Example-3D
Answer:730

Dr. Amrita Jyoti 25


Copyright © 2021, ABES Engineering College
Example-3D
example: Given an array arr[1:8, -5:5, -10:5] with a base value
of 400 and the size of each element is 4 Bytes in memory find the
address of element arr[3][3][3] with the help of column-major
order?

Dr. Amrita Jyoti 26


Copyright © 2021, ABES Engineering College
Example-3D
Answer :2412

Dr. Amrita Jyoti 27


Copyright © 2021, ABES Engineering College
Example-3D
Given a 3D array A[2:8, –4:1, 6:10] with Base(A)= 200. Number of words per cell =4. Calculate
address of A[5,–1,8] if elements are stored in
- Row major order fashion
- Column major order.

Dr. Amrita Jyoti 28


Copyright © 2021, ABES Engineering College
Index Formula Derivation in Three Dimensional Array

Solution: Given:
Lower Bound of dimension 1(L1) = 2

Upper Bound of dimension 1 (U1) = 8

Lower Bound of dimension 2(L2) = -4

Upper Bound of dimension 2(U2) = 1

Lower Bound of dimension 3(L3) = 6

Upper Bound of dimension 3(U3) = 10

Base Address = 200


n=4

Dr. Amrita Jyoti 29


Copyright © 2021, ABES Engineering College
Index Formula Derivation in Three Dimensional Array

Solution: By formula (Row major order):


Address of A[i, j, k] = Base Address + [(i–L1)(U2 –L2+1)(U3–L3+1) + (j–L2)(U3–L3+1) + (k–L3)]*n
Address of A[5,-1,8] = 200 + [(5 – 2) * (1 – (-4) + 1)* (10 – 6+1) + (-1 –(-4)*(10-6 +1) + (8 -6) ] * 4
= 200 + [ 3 * 6* 5 + 3*5 + 2]*4
= 200 + 107*4 = 628

By formula (Column major order):


Address of A[i, j, k] = Base Address + [(k–L3)(U1–L1+1)(U2–L2+1) + (j–L2) (U1–L1+1) + (i–L1)]*n
Address of A[5,-1,8] = 200 + [(8 – 6) * (8 – 2 + 1)* (1 – (–4)+1) + (-1 –(-4)*(8 -2 +1) + (5 -2) ] * 4
= 200 + [ 2 * 7 *6 + 3*7 + 3]*4
= 200 + 432 = 632
Dr. Amrita Jyoti 30
Copyright © 2021, ABES Engineering College
Three Dimensional Array

1D
Address of A[i]= BA + W(i-LB)

2D
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]
3D
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]

N-D
Address of A[i1][i2]..[in]= BA + w[(in-Ln)+ (in-1-Ln-1)(rn) +(in-2 –Ln-2)(rn.rn-1)+…
…(i1-L1)(r2.r3… rn)

Dr. Amrita Jyoti 31


Copyright © 2021, ABES Engineering College
THANK YOU

Dr. Amrita Jyoti 32


Copyright © 2021, ABES Engineering College

You might also like