Array
Array
Prepared By,
Dr. Nikita Bhatt
Array
It is a collection of similar data elements.
It is a static data structure, which has a fixed size- we should know or determine array size
at compile time itself - No modifications can be done to array size during runtime.
The elements are stored in consecutive or contiguous blocks of memory.
Random access is allowed.
Inserting and deleting elements from an array is a tedious task, as it would involve shifting
of some or all the elements of the array which would also involve managing memory space
for it as well.
2
One Dimensional Array
Notation: A [ l : u ]
where, A =Array Name
l=lower bound, u=upper bound
Example:
int A[6] declared in C program means
A[0:5]
In general, A[-3 : 2] it has 6 elements
where, l =-3
u=2
3
One Dimensional Array(continue)
No of elements:
(u – l + 1)
where,
i = index
B.A.= Base Address
l = lower bound of an array
c = size of each element in bytes
4
One Dimensional Array(continue)
Example-One dimensional Array
A[ -2 : 3 ] (array of int with BA=2001)
Find Address of A[2].
No of elements: (u – l + 1) = (3 - ( - 2 )+1) = 6
l = -2, i =2
u = 3,
c = 4, BA= 2001
A[i] = B.A.+(i - l) × c
= 2001+(2 –(-2)) × 2
= 2001+(4 × 2)
= 2009
5
Two Dimensional Array
Notation: A[ lr : ur , lc : uc ]
where,
A=Array Name
lr =lower bound for row, ur =upper bound for row
lc =lower bound for col, uc =upper bound for col
6
Two Dimensional Array (Continue)
Calculate elements(Two Dimensional Array)
No of rows: r = (ur – lr + 1)
No of columns: c = (uc – lc + 1)
No of elements: r×c = (ur – lr + 1)×(uc – lc + 1)
7
Two Dimensional Array (Continue)
Example (Row-Major Order)
If D[-13:1, 4:9] is an array of float find the address of D[-2, 8]. Address of D[-13,4] is
3000.
Here, lr =-13, ur =1, BA=3000, c=4(∵ 𝑓𝑙𝑜𝑎𝑡)
lc =4, uc =9, i=-2, j=8
8
Two Dimensional Array (Continue)
Example (Column-Major Order)
If D[-13:1, 4:9] is an array of float find the address of D[-2, 8]. Address of D[-
13,4]=3000.
Here, lr =-13, ur =1, BA=3000, c=4(∵ 𝑓𝑙𝑜𝑎𝑡)
lc =4, uc =9, i=-2, j=8
D[-2,8] = B.A.+[(j– lc)(ur – lr + 1)+(i – lr)]×c
= 3000 + [(8-4)(1-(-13)+1)+(-2-(-13)] ×4
= 3284
9
Two Dimensional Array (Continue)
Example (Row-Major Order)
For given array of float A [-100:52, 60:89] find the total number of elements. Assume
that Base address is 5001. Find address of A [0, 71] element, if it is stored in row
major order.
10
Self Assessment-I
1. Given a two dimensional array Zl (3:l0, 10:20) stored in row-major order with base
address of 200 and size of each element of 4 bytes, find address of element Z1(5, 15).
2. Given 2D array M[-12:15, 2:22] with base address 1001 and type of the element is float.
Find the address of element M[-7,20] using row major and column major order.
11
Three Dimensional Array
A three dimensional array is a collection of two dimensional arrays (planes). Each two
dimensional array contains rows and columns.
For example,
12
Three Dimensional Array (continue)
Notation: A[ lP : uP, lr : ur , lc : uc]
where, A =Array Name
lP =lower bound for plane, uP =upper bound for plane
lr =lower bound for row, ur =upper bound for row
lc =lower bound for col, uc =upper bound for col
13
Three Dimensional Array(continue)
No of Planes: p = (uP – lP + 1)
No of rows: r = (ur – lr + 1)
No of columns: c = (uc – lc + 1)
No of elements: p×r×c
= (uP – lP + 1)×(ur – lr + 1)×(uc – lc + 1)
No of Planes: (1)-(-1)+1=3
No of rows: (4)-(2)+1=3
No of columns: (-6)-(-10)+1=5
No of elements: 3×3×5=45
14
Three Dimensional Array(continue)
Memory Representation
For multi-dimensional, two types of memory representation is possible.
1. Row major:
2. Column major:
15
Three Dimensional Array(continue)
Find the address of an element in 3-D
Stored in Row major:
No of elements in all
previous rows=
( j – lr)(uc – lc + 1)
No of elements in
current row= (k – lc)
16
Three Dimensional Array(continue)
For Row Major & Column Major
17
Three Dimensional Array(continue)
Example
Find address of B[0,-2,2] if array contains integer with initial address 140. B[-2:0, -4:-1, 1:3]
Here,
B.A.=140
i =0, j =(-2), k=2
lP =(-2), uP = 0
lr =(-4), ur =(-1)
lc =1, uc =3
c=2
18
Three Dimensional Array(continue)
Example
Row Major:
A[i,j,k] = B.A.+[(i – lP)(ur – lr + 1)(uc – lc + 1)+ ( j – lr)(uc – lc + 1)+(k – lc)]×c
= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+((-2)-(-4))(3-1+1)+(2-1)]×2
= 140+[(2)(4)(3)+(2)(3)+1]×2
= 140+[31]×2 = 202
Column Major:
A[i,j,k] = B.A.+[(i – lP)(ur – lr + 1)(uc – lc + 1)+ (k – lc)(ur – lr + 1)+( j – lr)]×c
= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+(2-1))((-1)-(-4)+1)+((-2)-(-4)]×2
= 140+[(2)(4)(3)+(1)(4)+2] ×2
= 140+[30] ×2 = 200
19
Self Assessment-II
Consider 3 dimensional Array A[90] [30] [40] stored in linear array in column major order. If
the base address starts at 100. The location of A[20] [20] [30] is __________ .
[Assume the first element is stored at A[1][1][1] and each element take 1 B].
20
20
Gate 1998
Let A be a two-dimensional array declared as follows: A: array [1 …. 10] [1 …… 15] of
integer; Assuming that each integer takes one memory locations the array is stored in row-
major order and the first element of the array is stored at location 100, what is the address of
the element A[i][j]?
(a) 15i + j + 84
(b) 15j + i + 84
(c) 10i + j + 89
(d) 10j + i + 89
Ai j = i − 1 ∗ 15 + (j − 1) + Base Address
A i j = 15i − 16 + j + 100 = 15i + j + 84
Answer : (a)
21
21
Question from geeksforgeeks
Which of the following operations is not O(1) for an array of sorted data. You may assume
that array elements are distinct.
(A) Find the ith largest element
(B) Delete an ith element
(C) Find the ith smallest element
(D) All of the above
Answer: (B)
22
Self Assessment-IV
Suppose A is a 3-D array given as A (1:9, -4:1, 5:10). The array is
stored in memory in row-major order with base address 100 and w=2
words per memory cell. Find the address of element A (5, -1,8).
23
Self Assessment-V
A 2D array is defined as A[20][5] requires 4 words per memory cycle.
First element is stored in A[0][0]. Find the location of A[12,3] if array
is implemented in column major order. Base address is 200.
24
Self Assessment-VI
Consider an array ar= {25,50,52,54,55,58,60,62,65,70,79,90} and
element to search is 25. What are the middle values in first, second
and third iterations if we apply binary search to find the element 25?
(a) 58,52,25
(b)60,54,25
(c) 58,54,50
(d)60,52,25
Answer: (a)
25
Self Assessment-VII
Consider an Array with elements :{ 10, 15, 20, 23, 33, 49, 59, 69, 77,
85, 91} Find out number of comparisons performed by binary search
to search 20 ?
Consider an Array with elements :{13, 15, 19, 21, 26, 31, 36, 39, 44,
48, 52} Find out number of comparisons performed by binary search
to search 52 ?
26
Applications of Array
To store elements of the same data type-useful for sorting of elements.
To implement other data structures like stack, queue, heap, etc.
To implement CPU scheduling algorithms [waiting state, ready-to-run state].
To implement matrix using arrays, which are an important part of the mathematical library
in any programming languages and useful in
• Databases like MySQL and MongoDB where values are compared and processed using array.
• Image Processing- filters are used to remove noise and extract features use array.
• Signal Processing, where every speech signal is an array of signal amplitudes.
To implement search algorithms like linear search and binary search.
To implement lookup table, which stores pre-calculated values that save the computation
time or fetching time.
To implement encryption algorithm like play fair cipher and hill cipher use array.
Playlist is array, contact list is array.
27
Thank You
28