Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Arrays

An Array is a Data Structure for storing more than one data


item/element having Similar Data Type (i.e. Homogeneous Data).

The elements of an Array are allocated at adjacent memory locations.

The total number of elements in an array is called Length of an Array.

Properties of an Array

o Each element in an array is of the same data type and carries the
same size.
o Elements in the array are stored at contiguous memory locations
from which the first element is stored at the smallest memory
location.
o Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
Base Address and the Size of the data element.

Representation of an Array
We can represent an array in various ways in different programming
languages.

For example, Array declaration in C Programming Language.

As per the above illustration, there are some of the following important
points:

o Array Index starts with 0.


o The array's length is 10, which means it can store 10 elements.
o Each element in an Array can be accessed through its Index.
Memory Allocation in Arrays

 All the data elements of an array are stored at contiguous


locations in the main memory.
 The Name of the Array represents the Base Address or the
address of the first element in the Main Memory.
 Each element of the array is represented by proper indexing.

Here, the memory allocation of an array arr of size 5. The Base Address
of the array is 100. It is the address of arr[0].

Here, the size of the data type used is of 4 bytes; therefore, each
element will take 4 bytes in the memory.

The Basic operations supported in the Array are:

o Traversal - This operation is used to visit the elements of the array.


o Insertion - It is used to add an element at a particular index.
o Deletion - It is used to delete an element from a particular index.
o Search - It is used to search an element using the given index or by
the value.
o Update - It updates an element at a particular index.
How to find number of elements in an Array ?

Number of elements in an Array, N can be found out by the formula:

N = [(Upper Bound – Lower Bound) + 1]

Q. An Array A[-25,…………,+75]. Find the total number of elements is A ?

Sol.: No. of elements, A = (UB-LB) + 1 = [(75-(-25))+1] = [100+1] = 101

How to access an element from the Array?

The formula to calculate the address to access an Array element:

Byte Address of element A[i] = Base Address + Size * (i - First Index)

OR

Address of A[I] = BA + W * (I – LB)

I = Subset of element whose address to be found,


BA = Base Address,
W = Storage Size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified assume zero).

Question 1:

An Array, A[-10 ..... +2] having Base Address (BA) = 999 and Size of an
element = 2 bytes, find the location of A[-1].

Solution:

On putting the values in the formula, we have:

Address of A[I] = BA + W * (I – LB)

L(A[-1]) = 999 + 2 x [(-1) - (-10)] = 999 + 18 = 1017


Question 2:

Given the Base Address of an array A[1300 …… 1900] as 1020 and the
size of each element is 2 bytes in the memory, find the address
of A[1700].

Solution:

Given:
Base Address, BA = 1020
Lower Limit/Lower Bound of subscript, LB = 1300
Storage Size of one element store in any Array, W = 2 Byte
Subset of element whose address to be found, I = 1700

Formula used:
Address of A[I] = BA + W * (I – LB)

Calculation:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800

Address of A[1700] = 1820

Types of Arrays:

 One-Dimensional Array (1-D Array): The 1-D Array is represented


as a Row, where elements are stored one after another.
 Two-Dimensional Array (2-D Array): The 2-D Array is an example of
Multidimensional Arrays. They can be considered as an Array of
Arrays or as a Matrix consisting of Rows and Columns.

The 2-Dimensional Arrays can be represented as the collection of Rows


and Columns as array[M][N] where M is the number of rows and N is
the number of columns.

To find the address of any element in a 2-Dimensional array there are


the following two ways-
1. Row Major Order
2. Column Major Order

Row Major Order:


Row Major Ordering assigns successive elements, moving across the
Rows and then down the next row, to successive memory locations.

In simple language, the elements of an array are stored in a Row-Wise


manner.
Formula to find the address of the element using Row-Major Order:

Address of A[I][J] = BA + W * ((I – LR) * N + (J – LC))

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
BA = Base Address,
W = Storage Size of one element store in an Array (in byte),
LR = Lower Limit of Row/Start Row Index of the Matrix (If not given assume it as
zero),
LC = Lower Limit of Column/Start Column Index of the Matrix (If not given assume
it as zero),
N = Number of Column(s) given in the Matrix.

Question:

Given an array, arr[1………10][1………15] with base value 100 and the


size of each element is 1 Byte in memory. Find the address
of arr[8][6] by using Row-Major Order.

Solution:

Given:
Base Address, BA = 100
Storage Size of one element store in any Array, W = 1 Bytes
Row Subset of an element whose address to be found, I = 8
Column Subset of an element whose address to be found, J = 6
Lower Limit of Row/Start Row Index of Matrix, LR = 1
Lower Limit of Column/Start Column Index of Matrix, LC = 1
Number of Column(s) given in the Matrix, N = [(Upper Bound – Lower Bound) + 1]
= [(15 – 1) + 1]
= 15
Formula:
Address of A[I][J] = BA + W * ((I – LR) * N + (J – LC))

Calculation:

Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))


= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)

Address of A[I][J] = 210

Column Major Order:


If elements of an Array are stored in a Column-Major manner means
moving across the Columns and then to the next column, then it is in
Column-Major Order.

Formula to find the address of the element using Column-Major Order:

Address of A[I][J] = BA + W * ((I – LR) + (J – LC) * M)

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
BA = Base Address,
W = Storage Size of one element store in any Array (in byte),
LR = Lower Limit of Row/Start Row Index of Matrix (If not given assume it as zero),
LC = Lower Limit of Column/Start Column Index of Matrix (If not given assume it as
zero),
M = Number of Rows given in the Matrix.
Question:

Given an array arr[1………10][1………15] with a base value of 100 and


the size of each element is 1 Byte in memory find the address of
arr[8][6] by using Column-Major Order.

Solution:

Given:
Base address, BA = 100
Storage size of one element store in any array, W = 1 Bytes
Row Subset of an element whose address to be found, I = 8
Column Subset of an element whose address to be found, J = 6
Lower Limit of Row/Start Row Index of matrix, LR = 1
Lower Limit of Column/Start Column Index of matrix, = 1
Number of Row(s) given in the matrix, M = [(Upper Bound – Lower Bound) + 1]
= [(10 – 1) + 1]
= 10

Formula used:
Address of A[I][J] = BA + W * ((I – LR) + (J – LC) * M)

Calculation:

Address of A [8][6] = 100 + 1 * ((8 – 1) + (6 – 1) * 10)


= 100 + 1 * (7 + 5 * 10)
= 100 + 1 * (57)

Address of A[I][J] = 157

For the same position two different address locations are obtained
that’s because in Row-Major Order, movement is done across the Rows
and then down to the next Rows. Whereas in the Column-Major Order,
movement is done across the Columns and then move to the next
Columns.
Advantages of Arrays:

 Arrays allow Random Access to elements. This makes accessing


elements by position faster.
 Any element in the Array can be directly accessed by using its
Index.
 Arrays have better Cache Locality which makes a pretty big
difference in performance.
 Arrays represent multiple data elements of the same type using a
single name. Therefore, it is easy to remember the name of all
the elements of an array.
 Array Data Structure are used to implement the other Data
Structures like Stacks, Queues, Trees, Graphs, etc.

Disadvantages of Arrays:

 Arrays store Homogenous data elements. It means that the


elements with similar data type can only be stored in it.
 In Arrays, there is Static Memory Allocation (i.e. Fixed Size), once
the memory is allocated to them, it cannot be increased or
decreased, making it impossible to store extra data element(s) if
required.
 There will be wastage of memory if we store lesser number of
elements than the declared size of an Array.
 Arrays store data in contiguous memory locations, which makes
deletion and insertion very difficult to implement.
Applications of Arrays:
 Arrays are used to implement other Data Structures like Stack,
Queue, Tree, Graph etc.
 Arrays are used for Matrices and other mathematical
implementations.
 Arrays are used in Lookup tables in Computers.
 Arrays can be used in CPU Scheduling.
 It is used for different Sorting Algorithms such as Bubble Sort,
Insertion Sort, Merge Sort, and Quick Sort etc.

Programs in Arrays:

1. Write a program to search a data element in an Array.

2. Write a program to Insert an element in an Array at the following


positions:
(i) Beginning (ii) End (iii) Middle

3. Write a program to Delete an element from an Array at the


following positions:
(i) Beginning (ii) End (iii) Middle

You might also like