Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

MODULE 4 LECTURE NOTES

Module 4: Array data structure


Unit 1: Definition of an array, need for arrays
Unit 2: Type and Dimension of an Array
Unit 2: Implementation of Array: Linear and 2-dimensional array
Unit 3: Basic operations in an array
Unit 4: Record and String Structure
Unit 5: Implementation and operations on Strings
Unit 6: Pattern Matching
Unit 7: methods of Pattern Matching

Array Data Structure

Array is a collection of items of the same data type, associated with indices
through which any of its components can be referenced. The array indices
are a set of consecutive integers and are used to compute the memory
addresses of the array components thereby making random access to an
array item possible. Array is a type of linear data structure that is defined as
a collection of elements with same data types. They exist in both single
dimension and multiple dimensions. These data structures come into picture
when there is a necessity to store multiple elements of similar nature
together at one place.

An array may be viewed as a group of variables of the same data type with a given
group name and with each variable accessible only through an index taken from an
index set associated with the array. The index set is a set of labels used to indicate
the components of an array for ease of reference.

Following are the important terms to understand the concept of Array.

. Index − Each location of an element in an array has a numerical index, which is


used to identify the element

 Element − Each item stored in an array is called an element.


Need for Arrays

Arrays are used as solutions to many problems from the small sorting problems to
more complex problems like travelling salesperson problem. There are many data
structures other than arrays that provide efficient time and space complexity for
these problems, so what makes using arrays better? The answer lies in the random-
access lookup time.

Arrays provide O(1) random access lookup time. That means, accessing the
1st index of the array and the 1000th index of the array will both take the same
time. This is due to the fact that array comes with a pointer and an offset value.
The pointer points to the right location of the memory and the offset value shows
how far to look in the said memory.

array_name[index]
| |
Pointer Offset

Therefore, in an array with 6 elements, to access the 1st element, array is pointed
towards the 0th index. Similarly, to access the 6 th element, array is pointed towards
the 5th index.

Array Representation

Arrays are represented as a collection of buckets where each bucket stores one
element. These buckets are indexed from ‘0’ to ‘n-1’, where n is the size of that
particular array. For example, an array with size 10 will have buckets indexed from
0 to 9.

This indexing will be similar for the multidimensional arrays as well. If it is a 2-


dimensional array, it will have sub-buckets in each bucket. Then it will be indexed
as array_name[m][n], where m and n are the sizes of each level in the array.

Types and Dimensions of an array

An array has a Type, Size, and Dimension


The Type of an array is the Type of its components

The size of an array is the number of items in the array

The Dimension of an array is the number of basic directions one may need to follow
to access an item of the array assuming we start from the first entry of the array

Each direction of an array is associated with an index set through which the position
of a given item in the array can be indicated in that direction.

As per the above illustration, following are the important points to be


considered.

• Index starts with 0.

• Array length is 9 which means it can store 9 elements.

• Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 23.
Irregular Array: Jagged and Non rectangular Array
Some applications require the use of irregular arrays to avoid memory wastage.

Implementation of an Array
An array is always allocated contiguous memory (arrays in programming languages
typically use contiguous memory, where elements are stored one after the other in
consecutive memory addresses. Accessing elements in such an array is efficient because
the processor can easily calculate the location of the next element based on the size of
each element.) storage for its items.
The size of the memory storage (number of bytes of memory) allocated for each
array item depends on the type of the array item and choice of programming
language.

Implementation of a Linear Array

Suppose each item of a linear array A of size N need B bytes to memory store and
that the address of the first item in the array is BaseAddress then the following
mapping hold

Memory Address=Base Address+(Index × Size of Each Element)

where:

• Memory Address: is the address of the desired element.

• Base Address: is the address of the first element in the array.

• Index: is the position of the desired element in the array. The index starts
from 0 for the first element.

• Size of Each Element: is the number of bytes occupied by each element in


the array.

Example 1: Calculate the memory address of element with index 3, with 4


bytes as size of elements and a base address of 1000.

Memory Address=Base Address+(Index × Size of Each Element)

Memory Address=1000+(3×4) = 1000 + 12 = 1012

Example 2. What is the memory address of the 6th item in the array below
if the BaseAddress is 2000 with 2 bytes as size of element?

Number

4 6 7 8 8 9 9 6 0 -1

BaseAddress = 2000; Size of each element = 2 bytes and the index is 5

Memory Address=Base Address + (Index × Size of Each Element)

Memory Address=1000+(5×2) = 2000 + 10 = 2010


Class Exercise: Consider a character array where each character takes 3 bytes. The
base address is 2000, and you want to find the memory address of the element at
index 5.

Memory Address=Base Address + (Index × Size of Each Element)

{Memory Address} = 2000 + (5 x3) = 2015

The memory address of the element at index 5 is 2015.

Class Exercise: For an array of floating-point numbers where each float occupies 8
bytes, with a base address of 3000, and you want the memory address of the
element at index 7:

{Memory Address} = 3000 + (7 x 8) = 3056

The memory address of the element at index 1 is 3008.

Implementation of a 2- dimensional array

A 2dimensional array could be represented using the following methods

1. Column Major 2. Row Major 3. Edge vector

In column-major order (also known as column-wise order or Fortran order), a two-


dimensional array is stored in memory column by column. To calculate the memory
address of an element in a two-dimensional array using column-major order, you
can use the following formula:

Memory Address = Base Address + ( Column × Row Size + Row) × Size of


Each Element

Where:

Memory Address: is the address of the desired element.

Base Address: is the address of the first element in the array.

Column: is the column index of the desired element.

Row Size: is the number of rows in the array.

Row: is the row index of the desired element.

Size of Each Element: is the number of bytes occupied by each element in the
array.
Example 1: Consider a 2D array of integers with 3 rows and 4 columns. Each
integer takes 4 bytes, and the base address of the array is 4000. Find the memory
address of the element at column 2 and row 1.

Memory Address = Base Address + (Column × Row Size + Row) × Size of


Each Element

Memory Address = 4000+ (2 × 3 + 1) × 4

Memory Address= 4000+(2×3+1)×4

Memory Address= 4000+(7)×4

Memory Address=4000+28

Memory Address = 4028

So, the memory address of the element at column 2 and row 1 in the 2D array is
4028.

Class exercise: Suppose you have a 2D array of integers with 3 rows and 2 columns. Each
integer occupies 4 bytes, and the base address is 5000. Find the memory address for the
element at column 1, row 2.

Memory Address=5000+(1×3+2)×4

Memory Address=5000+(5×4)

Memory Address=5000+20

Memory Address=5020

So, the memory address of the element at column 1 and row 2 is 5020.

Consider a 2D character array with 4 rows and 3 columns. Each character takes 1 byte,
and the base address is 6000. Find the memory address for the element at column 2,
row 0.

Memory Address=6000+(2×4+0)×1

Memory Address=6000+(8×1) Memory Address=6000+8

Memory Address=6008Memory Address=6008


Class exercise2: For a 2D array of floating-point numbers with 2 rows and 2 columns,
each float taking 8 bytes, and a base address of 7000. Find the memory address for the
element at column 1, row 1.

Row major technique

In row-major order, a two-dimensional array is stored in memory row by row. To


calculate the memory address of an element in a two-dimensional array using the row-
major order, the following formula can be used:

Memory Address =

Base Address + (Row×Column Size+Column) × Size of Each Element

where:

 Memory Address: is the address of the desired element.


 Base Address: is the address of the first element in the array.
 Row: is the row index of the desired element.
 Column Size: is the number of columns in the array.
 Column: is the column index of the desired element.
 Size of Each Element: is the number of bytes occupied by each element in
the array.

Example:

Consider a 2D array of integers with 3 rows and 4 columns. Each integer takes 4 bytes,
and the base address of the array is 4000. Find the memory address of the element at
row 2 and column 1.

Memory Address=4000+(2×4+1)×4

Memory Address=4000+(9×4)

Memory Address=4000+36

Memory Address=4036

So, the memory address of the element at row 2 and column 1 in the 2D array is 4036.
Class exercise : Suppose you have a 2D array of integers with 3 rows and 2 columns.
Each integer occupies 4 bytes, and the base address is 6000. Find the memory address
for the element at row 1, column 2.

Memory Address=6000+(1×2+2)×4

Memory Address=6000+(4×4) Memory Address=6000+16

Memory Address=6016

So, the memory address of the element at row 1 and column 2 is 6016.

Basic Operations in the Arrays

The basic operations in the Arrays are insertion, deletion, searching, display,
traverse, and update. These operations are usually performed to either modify the
data in the array or to report the status of the array.

Following are the basic operations supported by an array.

 Traverse − print all the array elements one by one.


 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.
 Display − Displays the contents of the array.

Insertion Operation

In the insertion operation, we are adding one or more elements to the array. Based
on the requirement, a new element can be added at the beginning, end, or any
given index of array. This is done using input statements of the programming
languages.

Algorithm

Following is an algorithm to insert elements into a Linear Array until we reach the
end of the array −

1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop

Example

C C++JavaPython
#include <stdio.h>
int main(){
int LA[3] = {}, i;
printf("Array Before Insertion:\n");
for(i = 0; i < 3; i++)
printf("LA[%d] = %d \n", i, LA[i]);
printf("Inserting Elements.. \n");
printf("The array elements after insertion :\n"); // prints array values
for(i = 0; i < 3; i++) {
LA[i] = i + 2;
printf("LA[%d] = %d \n", i, LA[i]);
}
return 0;
}
Output
Array Before Insertion:
LA[0] = 0
LA[1] = 0
LA[2] = 0
Inserting Elements..
The array elements after insertion :
LA[0] = 2
LA[1] = 3
LA[2] = 4

Deletion Operation
In this array operation, we delete an element from the particular index of an array.
This deletion operation takes place as we assign the value in the consequent index
to the current index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to delete an element available at the K th position
of LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Search Operation

Searching an element in the array using a key; The key element sequentially
compares every value in the array to check if the key is present in the array or not.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to find an element with a value of ITEM using
sequential search.

1. Start

2. Set J = 0

3. Repeat steps 4 and 5 while J < N

4. IF LA[J] is equal ITEM THEN GOTO STEP 6

5. Set J = J +1

6. PRINT J, ITEM

7. Stop

Traversal Operation

This operation traverses through all the elements of an array. We use loop
statements to carry this out.

Algorithm
Following is the algorithm to traverse through all the elements present in a Linear
Array −

1 Start

2. Initialize an Array of certain size and datatype.

3. Initialize another variable ‘i’ with 0.

4. Print the ith value in the array and increment i.

5. Repeat Step 4 until the end of the array is reached.

6. End

Update Operation

Update operation refers to updating an existing element from the array at a given
index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to update an element available at the Kth position
of LA.

1. Start

2. Set LA[K-1] = ITEM

3. Stop

Display Operation

This operation displays all the elements in the entire array using a print statement.

Algorithm

1. Start

2. Print all the elements in the Array

3. Stop
Record

Pattern Matching

This process is based on searching for a sub string in another string or text.

Consider searching for a substring S in Text T.

Either the Brute Force Algorithm or Knuth Morris Pratt Algorithm can be used.
Tutor Marked Assignment (TMA 2)

Differentiate between an Array, Record and String

Submission date: Tuesday 23/01/24 2:00pm till Wednesday 24/01/24 2:00pm

Submission link: https://forms.gle/qXSivcRd4fVNsJpg7

You might also like