Professional Documents
Culture Documents
MODULE 4 LECTURE NOTES-CSC 317
MODULE 4 LECTURE NOTES-CSC 317
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.
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.
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.
• 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.
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
where:
• Index: is the position of the desired element in the array. The index starts
from 0 for the first element.
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
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:
Where:
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=4000+28
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 =
where:
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=6016
So, the memory address of the element at row 1 and column 2 is 6016.
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.
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
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
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
3. Stop
Display Operation
This operation displays all the elements in the entire array using a print statement.
Algorithm
1. Start
3. Stop
Record
Pattern Matching
This process is based on searching for a sub string in another string or text.
Either the Brute Force Algorithm or Knuth Morris Pratt Algorithm can be used.
Tutor Marked Assignment (TMA 2)