Array
Array
This can be done in two ways: by declaring different 10 variables to store marks of individual student
or we can think of defining an array to store all these elements under one name.
We can see that using method 1 we need to remember the names of the 10 variables but In
method 2 we need to remember only one variable name to access the elements.
The data structure that stores elements of same type and access using one name is known as Array.
We will learn the concept of array as data structure in detail, terminologies, address calculation,
various algorithms around arrays etc. in this chapter.
4.1.1 Definition
An array is a data structure that is used collect multiple variables of the same data type
together into one variable. The idea is to store multiple items of the same type together.
• Arrays have a fixed size where the size of the array is defined when the array is
initialized.
• Array can also be termed as subscripted variable.
4.2 Types of the Arrays
Memory Representation
In the above diagram A[0], A[1], A[2],. . . , A[9] are the array elements. The address mentioned
for these elements represent the physical location of data elements in Main Memory. It is
assumed that each element requires 4 bytes for storage.
(In most of the programming languages the index is considered to be starting from 0)
Two–Dimensional Array
Consider a matrix that involves rows and columns for storage of the elements.
If the number of rows in the matrix are m and columns are n, then there will be m x n elements
in the matrix. A matrix is an example of a 2–dimensional array. When an array is represented in the
form of rows and columns, it is called 2D array. It can also be termed as an array of one- Dimensional
arrays which is a collection of data elements of same nature.
Syntax:
<Data Type> <Arrayname> [m][n]
Where,
m → Number of rows
n → Number of columns
Memory Representation: There are two–ways by which the 2D array elements can be
represented in Memory.
a) Row–major Representation
b) Column–major Representation
There are some of the programming languages which support row–major order representation
for example, C/C++/Objective–C (for C–style arrays), PL/I, Pascal, Speakeasy, SAS, and Rasdaman.
Similarly, languages which support row–major order representation are: Fortran, MATLAB, GNU
Octave, S–Plus, R, Julia, and Scilab.
Row–major Representation: In row major order, storage of the elements in the memory is row–
wise i.e. storage of elements of first row is followed by the storage of elements of second row
and so on so forth.
Column–major Representation: In column major order, elements are stored column wise
i.e. storage of elements of first column followed by storage of second column elements and so on so
forth.
Three–Dimensional Array
When an array is represented in the form of 3 different dimensions, it is called 3–D Array. It can also
be called as an array of 2–dimensional arrays.
Below given diagram shows a 3–Dimensional array which has 3 dimensions named U1, U2, and U3.
2. A[2:18] has
N = 18 – 2 + 1
= 17 elements
Suppose we have been given the base address of an array, say α. So,
address of, A[L] = α.
We are interested in finding the address of ith index element. To find this, let us assume that first
element is at index 1 and each element requires 1 byte for storage. Then,
address of A[1]= α address
of A[2]= α + 1 address of
A[3]= α + 2 address of A[4]=
α + 3 And so on…
address of A[i]= α + (i–1)
Now let us remove the assumptions that we took at the beginning. First, we remove the assumption that
each element requires 1 byte with n bytes for storage per element.
Then,
Address of A[i]= α + (i – 1)*n
We now remove the second assumption that first index of array is 1. If index starts from L then, for
some ith index, distance from first index will be i–L+1.
Example:
Given A [–1:10], bytes per cell = 4, base address = 2000. Find the address of A [7].
Solution:
Here, i = 7 n=
4
Lower Bound = –1
Upper Bound = 10
Address of A [i] = Base Address + n*(i – Lower Bound) Address
of A [7] = 2000 + 4*(7 – (–1))
= 2032
Example:
Given A [ 1 :15], bytes per cell = 3, base address = 1000 find the address of A [9].
Solution:
Here, i = 9 n=
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
4.3.2 Two – Dimensional Array
Row side the indices are L1, L1+1, L1+2, …, U1–1, U1. Column
side the indices are L2, L2+1, L2+2, …, U2–1, U2.
For simplicity we will assume that the first index is 1 and each element requires 1 byte for
storage. The array becomes A[1:U1, 1:U2].
Now, let us remove the assumption that every element takes 1 byte of storage with n bytes for
storage. So, formula will change to
Address of A[i, j] = α +[(i – 1)*U2 + (j – 1)]*n
Now, remove the assumption that first index is 1 in row and column with L1 and L2
respectively.
Replacing U2 as U2 – L2 +1 (length formula), i with i – L1 + 1 and j with j – L2 + 1
Example: Suppose a 2D array A is declared as A[–2:2 , 2:6], words per cell = 4, base address
= 200. Consider Row major order arrangement.
a) Find out length of each dimension and number of elements in array.
b) Find the location of A[1,2]
Solution:
Here Lower Bound of row(L1) = –2 Here
Upper Bound of row(U1) = 2 Here
Lower Bound of column(L2) = 2 Here
Upper Bound of column(U2) = 6 n = 4
a) Length of row = U1 – L1 + 1
= 2 – (–2) + 1 = 5
Length of column = U2 – L2 + 1
= 6 – 2 + 1 =5
No. of elements = 5*5 = 25
b) By formula:
A[i, j] = Base address + [(i – L1)*( U2 – L2 + 1) + (j – L2)] *n
A[1, 2]= 200 + [(1 – (–2) * (6 – 2 + 1) + (2 – 2)] * 4
= 200 + 15* 4
= 260
Row side the indices are L1, L1+1, L1+2, …, U1–1, U1. Column
side the indices are L2, L2+1, L2+2, …, U2–1, U2.
For simplicity we will assume that the first index is 1 and each element requires 1 byte for
storage. The Array becomes A[1:U1, 1:U2].
Another assumption: every element requiring 1 byte for storage. So that, address of first element
say:
If the base address of array is α then,
Address of A[1,1] = α
Address of A[2,1] = α + 1
Address of A[3,1] = α + 2
Address of A[4,1] = α + 3
…
Address of A[U1,1] = α + (U1 – 1)
Address of A[1,2] = α + ( U1 – 1)+1 (When stored all elements of first column, elements of
second column will be stored in memory)
Address of A[1,2] = α + U1
Address of A[1,3] = α + U1 + U1 ( When stored all elements of second column, elements of
third column will be stored in memory)
Address of A[1,3] = α + 2 U1
…
Address of A[1,j] = α + (j – 1)*U1
Address of A[2,j] = α + (j – 1)* U1 +1
Address of A[3,j] = α +(j – 1)* U1 +2
…
Similarly, A[i, j] = α +(j – 1)*U1 + (i – 1)
Now, let us remove the assumption that every element takes 1 byte of storage with n bytes for
storage. So, formula will change to
Address of A[i, j] = α +[(j – 1)*U1 + (i – 1)]*n
Now, remove the assumption that first index is 1 in row and column with L1 and L2 respectively.
Replacing U1 as U1 – L1 +1 (length formula), i with i – L1 + 1 and j with j – L2 + 1
Example: Suppose a 2D array A is declared as A[–2:2, 2:6], words per cell = 4, base address = 1024.
Consider Column Major order arrangement.
a) Find the length of each dimension and number of elements in array.
b) Find the location of A[2,5]
Solution:
Here Lower Bound of row(L1) = –2 Here
Upper Bound of row(U1) = 2 Here
Lower Bound of column(L2) = 2 Here
Upper Bound of column(U2) = 6 n = 4
a) Length of row = U1 – L1 + 1
= 2 – (–2) + 1 = 5
Length of column = U2 – L2 + 1
= 6 – 2 + 1 =5
No. of elements = 5*5 = 25
b) By formula:
Address of A[i, j] = Base address + [(j – L2)*( U1 – L1 + 1) + (i – L1)] *n Address
of A[2,5] = 1024 + [(5 – 2) * (2 – (–2) + 1) + (2 – (–2))] * 4
= 1024 + [15 + 4]*4
= 1024 + 76
= 1100
Let’s take a 3-dimensional Array in which the first dimension represents U1, Second
dimension U2 and third dimension U3. We can imagine this as a cuboid of size U1 x U2 x U3.
The 3-D Array can be represented as: A[L1:U1, L2:U2, L3:U3]
L1, L2 and L3 are the lower bound of first, second and third dimension respectively.
For simplicity we will assume that the first index is 1 and each element requires 1 byte for
storage. The Array becomes A[1:U1, 1:U2, 1:U3].
For making it even simpler, let us take different slices by cutting the cuboid horizontally across
the first dimension. The slices obtained are of size U 2xU3. There will be U1 such slices.
The above diagram can be viewed as U1 2-D arrays of size U2xU3. If the
base address of array is α then,
Address of A[1,1,1] = α
Address of A[2,1,1] = α + U2xU3 (There are U2xU3 elements in the first slice)
Address of A[3,1,1] = α + U2xU3 + U2xU3
= α + 2U2xU3
Address of A[4,1,1] = α + 3U2xU3
…
Address of A[i, 1, 1] = α + (i – 1) U2xU3
Now let us expand ith array. This will be a 2-D array of size U2xU3 . The diagram given below shows
the storage pattern
Address of A[i,2,1] = α + (i – 1)U2*U3 + U3 (There are U3 elements in the first row of this 2-D array)
Address of A[i,3,1] = α + (i – 1)U2*U3 + U3 + U3 Address
of A[i,3,1] = α + (i – 1)U2*U3 + 2 U3
Now, let us remove the assumption that every element takes 1 byte of storage with n bytes for
storage. So, formula will change to
Now, remove the assumption that first index is 1 in each dimension with L1, L2 and L3 respectively.
i will be replaced by i–L1+1, j by j–L2+1 and k by k–L3+1
Let’s take a 3-dimensional Array in which the first dimension represents U1, Second
dimension U2 and third dimension U3. We can imagine this as a cuboid of size U1 x U2 x U3.
The 3-D Array can be represented as: A[L1:U1, L2:U2, L3:U3]
L1, L2 and L3 are the lower bound of first, second and third dimension respectively.
For simplicity we will assume that the first index is 1 and each element requires 1 byte for
storage. The Array becomes A[1:U1, 1:U2, 1:U3].
For making it even simpler, let us take different slices by cutting the cuboid horizontally across
the first dimension. The slices obtained are of size U 1xU2. There will be U3 such slices.
The above diagram can be viewed as U3 2-D arrays of size U1xU2. If the
base address of array is α then,
Address of A[1,1,1] = α
Address of A[1,1,2] = α + U1xU2 (There are U1xU2 elements in the first slice)
Address of A[1,1,3] = α + U1xU2 + U1xU2
= α + 2U1xU2
Address of A[1,1,4] = α + 3U1xU2
…
Address of A[1, 1, k]= α + (k – 1) U1xU2
Address of A[1,2,k] = α + (k – 1)U1*U2 + U1 (There are U1 elements in the first column of
this 2-D array)
Address of A[1,3,k] = α + (k – 1)U1*U2 + U1 + U1
Address of A[1,3,k] = α + (k – 1)U1*U2 + 2*U1 Address
of 1st element of jth column
Address of A[1,j,k] = α + (k – 1)U1*U2 + (j – 1)*U1
Now, let us remove the assumption that every element takes 1 byte of storage with n bytes for
storage. So, formula will change to
Now, remove the assumption that first index is 1 in each dimension with L1, L2 and L3 respectively.
i will be replaced by i–L1+1, j by j–L2+1 and k by k–L3+1
Example: 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.
Solution:
Here Lower Bound of dimension
1(L1) = 2 Here Upper Bound of
dimension 1 (U1) = 8 Here Lower
Bound of dimension 2(L2) = -4
Here Upper Bound of dimension
2(U2) = 1 Here Lower Bound of
dimension 3(L3) = 6 Here Upper
Bound of dimension 3(U3) = 10
Base Address = 200
n=4