UNIT II - Arrays
UNIT II - Arrays
ARRAYS
Array is a container which can hold a fix number of items and these items should be of the same
type. Most of the data structures make use of arrays to implement their algorithms. Following are
the important terms to understand the concept of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
As per the above illustration, following are the important points to be considered.
Basic Operations
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.
In C, when an array is initialized with size, then it assigns defaults values to its elements in
following order.
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Traverse Operation
This operation is to traverse through the elements of an array.
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of
the array −
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
For other variations of array insertion operation click here
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
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
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
Search Operation
You can perform a search for an array element based on its value or its 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 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
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
j = j + 1;
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
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 K th position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are :\n");
LA[k-1] = item;
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8
Types of Arrays
Types of arrays depend upon the number of dimensions of an array. The count of indices or subscripts
required to access one element of an array define the dimensions of an array.
Types of arrays
One-dimensional Array
Two-dimensional Array
Three-dimensional Array
Two dimensional and three dimensional arrays are also called multi-dimensional arrays. in types
of arrays, multi-dimensional arrays also include arrays with four and higher dimensions.
One-dimensional Array
In a one-dimensional array the elements are stored in contiguous memory locations where each
element is accessed by using a single index value. It is a linear data structure storing all the
elements in sequence.
The elements are stored in memory in continuation and the variable declared as an array is
actually a pointer to the address of first element of the array. This address is called the base
address. The address of any other element can be calculated with the following formula
Where
ADDRESS -memory location of the Kth element of the array (To be calculated)
ARRAY – name of the ARRAY
WORDLENGTH – number of bytes required to store one element depending upon its data type
like a character value needs 1 byte and an integer value needs 2 bytes.
LOWERBOUND – index of the first element of the array.
BASEADDRESS– Address of first element of the array
K=6
BASEADDRESS = 1001
ADDRESS(ARRAY[6])= 1001+ 2*( 6-1) =1011 this is the address of memory location where
6th element (34) is stored as visible in the figure above
Two-dimensional Array
In types of arrays, a two dimensional array is a tabular representation of data where elements are
stored in rows and columns. A two dimensional array is actually a collection of M X N elements
which has M rows and N columns. To access any element in a two-dimensional array two
subscripts are required for defining position of an element in a specific row and column. The first
index is for row number and second is for column index. In the example shown the first index
values row=2 column=3 is used to access element 56.
To find the Address of any element located at Ith row and Jth column is calculated by using the
formula
Where
ADDRESS – memory location of the element at Ith row and J th columnof the array (To be calculated)
ARRAY – name of the ARRAY
WORDLENGTH – number of bytes required to store one element depending upon its data type
like a character values needs 1 byte and an integer value needs 2 bytes.
N – number of columns of the array.
BASEADDRESS – Address of first element of the 2-D array
In this example to calculate memory address of a given element (44) says in 1st row and
3rd column you will put the values in the formula.
I=1, J=3
N=4
WORDLENGTH=1 (assuming only one byte is required to store these small ints )
BASEADDRESS = 1001
ADDRESS(ARRAY(6))= 1001+ 1*( 4*(1-1) + (3-1)) =1003 this is the address of memory
location where 44 is stored as visible in the previous figure
ADDRESS – memory location of the Ith and Jth element of the array (To be calculated)
ARRAY – name of the ARRAY
WORDLENGTH – number of bytes required to store one element depending upon its data type
like a character values needs 1 byte and an integer value needs 2 bytes.
M – number of rows of the array.
BASEADDRESS -Address of first element of the array
In this example to calculate memory address of a given element (44) say in 1st row and
3rd column you will put the values in the formula.
I=1, J=3
M=3
WORDLENGTH=1 (assuming only one byte is required to store these small ints )
BASEADDRESS = 1001
=1007 this is the address of memory location where 44 is stored as visible in the previous figure