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

Array

Uploaded by

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

Array

Uploaded by

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

Introduction

Suppose we want to store the marks of 10 students as shown in below figure.

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.

Method 1: Requires taking 10 different variables M1, M2, M3 … M10.


M1 = 12
M2 = 15
M3 = 10
.
.
.
M10 = 14

Method 2: Storing of marks under a single array variable sequentially.

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.

The properties are:


• It is a linear data structure type.
• It stores the elements of same data type. So, it comes under the category of
homogenous data structure.

• The elements are stored at contiguous memory locations in an array.

• 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

One Dimensional array:


A one–dimensional array (or single dimension array) is a type of linear array. Accessing its elements
involves a single subscript.
Syntax:
<Data Type> <Arrayname> [Size] Example: int
A[10]

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.

If the array is declared as A[3][3][3], it will have a total of 3 x 3 x 3 = 27 elements.

Memory Representation in Row Major Order

Here, the first dimension is considered as row.


In the similar way Column major order arrangement can be done. This is left to the readers to
prepare the memory representation in Column Major order.

4.3 Index Formula Derivation for Array


The programming languages do use the index formula computation to reach to the address of
desired element in the memory. In this section we are explaining, the index formula derivation
for 1–D, 2–D, 3–D and N–D arrays.

4.3.1 One Dimensional Array


In a given array if the Base Address (which is the address of first element in the memory) is known,
we can find the address of any other element in array.
Suppose there is a one–dimensional array A[L : U] where L is the first index and U is the last index in
the array.

Number of elements in 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) For
example,
1. A[0:9] has
N=9–0+1
= 10 elements

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.

Address of A[i]= α + (i – 1)*n


Replacing i with i – L + 1
Address of A[i]= α + (i – L + 1 – 1) *n
Address of A[i]= α + (i – L)*n

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

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

4.3.2.1 Row Major Order Arrangement


Suppose we have a 2–D array A[L1:U1, L2:U2] given as below:

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].

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

the base address of array is α then,


Address of A[1,1] = α
Address of A[1,2] = α + 1
Address of A[1,3] = α + 2
Address of A[1,4] = α + 3

Address of A[1, U2] = α + (U2 – 1)
Address of A[2,1] = α + ( U2 – 1) + 1 (When stored all elements of first row, elements of
second row will be stored in memory)
Address of A[2,1] = α + U2
Address of A[3,1] = α + U2 + U2 ( When stored all elements of second row, elements of third row will
be stored in memory)
Address of A[3,1] = α + 2 U2

Address of A[i, 1] = α + (i – 1)*U2
Address of A[i,2] = α + (i – 1)*U2 +1
Address of A[i,3] = α + (i – 1)*U2 +2

Similarly, A[i, j] = α + (i – 1)*U2 + (j – 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] = α +[(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

Address of A [i, j] = α + [(i – L1 + 1 – 1)*( U2 – L2 + 1) + (j – L2 + 1 – 1)] *n


Address of A [i, j] = α + [(i – L1)*( U2 – L2 + 1) + (j – L2)] *n

Address of A[i, j] = Base address + [(i – L1)*( U2 – L2 + 1) + (j – L2)]


*n

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

4.3.2.1 Column Major Order Arrangement


In this we will discuss about index–formula computation of 2–D array and we assume that the
elements are stored in Column–Major Order, which means the elements of first column are stored
first, followed by the elements of 2nd column and so on. Suppose we have a 2–D array A[L1:U1,
L2:U2] given as below:

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

Address of A [i, j] = α + [(j – L2 + 1 – 1)*( U1 – L1 + 1) + (i – L1 + 1 – 1)]*n


Address of A [i, j] = α + [(j – L2)*(U1 – L1 + 1) + (i – L1)] *n

Address of A[i, j] = Base address + [(j – L2)*( U1 – L1 + 1) + (I – L1)]


*n

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

4.3.3 Three–Dimensional Array

4.3.3.1 Row Major Order Arrangement

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

Address of 1st element of jth row


Address of A[i,j,1] = α + (i – 1)U2*U3 + (j – 1)U3
Address of A[i,j,2]= α + (i – 1)U2*U3 + (j – 1)U3 +1
Address of A[i,j,3] = α + (i – 1)U2*U3 + (j – 1)U3 +2
Similarly, for kth element of jth row
Address of A[i, j, k] = α + (i – 1)U2*U3 + (j – 1)U3 + (k – 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

A[i, j, k] = α + [(i – 1)U2*U3 + (j – 1)U3 + (k – 1)]*n

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

A[i, j, k] = α + [(i – L1)(U2 – L2 + 1)(U3 – L3 + 1) + (j – L2)(U3 – L3 + 1) + (k – L3)]*n

A[i, j, k] = Base Address + [(i–L1)(U2 –L2+1)(U3–L3+1) + (j–L2)(U3–L3+1) + (k–L3)]*n

4.3.3.2 Column Major Order Arrangement

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

Address of A[2,j,k]= α + (k – 1)U1*U2 + (j – 1)*U1 + 1 Address


of A[3,j,k] = α + (k – 1)U1*U2 + (j – 1)*U1 +2

Similarly, for kth element of jth column


Address of A[i,j,k] = α + (k – 1)U1*U2 + (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,k] = α + [(k – 1)U1*U2 + (j – 1)*U1 + (i – 1)]*n

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

A[i, j, k] = α + [(k – L3)(U1 – L1 + 1)(U2 – L2 + 1) + (j – L2) (U1 – L1 + 1) + (i – L1)]*n

A[i, j, k] = Base Address + [(k–L3)(U1–L1+1)(U2–L2+1) + (j–L2) (U1–L1+1) + (i–L1)]*n

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

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

You might also like