Arrays
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.
A linear array, is a list of finite numbers of elements stored in the memory. In a linear array,
we can store only homogeneous data elements. Elements of the array form a sequence or
linear list that can have the same type of data. Each element of the array, is referred by an
index set.
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.
Index starts with 0.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
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.
Page |2
Traverse Operation:
This operation is to traverse through the elements of an array.
Algorithm: consider a linear array LA with lower bound LB to upper bound UB
Step 1: repeat for k=LB to UB
Apply PROCESS to LA [K]
[End of Loop]
Step 2: Exit
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int A[10],K=0,UB;
printf(“Enter the Array size less than 10 elements: “);
scanf(“%d”,&UB);
printf(“Enter the elements in array: \n”);
for(K=0;K<UB;K++)
{
scanf(“%d”,&A[K]);
}
printf(“The Traverse of array is:\n”);
for(K=0;K<UB;K++)
{
printf(“%d\n”,A[K]);
}
getch();
return 0;
}
Output:
Enter the Array size less than 10 elements: 5
Enter the elements in array: 1 2 3 4 5
The Traverse of array is:
12345
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.
Algorithm:
Step 1: [Initialize counter] Set J = N
Step 2: Repeat steps 3 and 4 while J>=K
Page |3
Example:
#include <stdio.h>
int main()
{
int array[50], position, c, n, value;
printf("Enter number of elements in the array\n");
scanf("%d", &n);//6
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);//1 5 6 7 8 9 index- 0 1 2 3 4 5
printf("Please enter the location where you want to insert an new element\n");
scanf("%d", &position);//3
printf("Please enter the value\n");
scanf("%d", &value);//85
for (c = n - 1; c >= position - 1; c--) // c=6-1;c>=3-1//c=5;5>=2 true
array[c+1] = array[c]; array[5+1]=6//0 1 2 3 4 5 6
//array[5+1]= array[c]è array[6] = array[c]
array[position-1] = value;//3-1=2 è 85
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);// 1 5 85 6 7 8 9
return 0;
}
Output:
Enter number of elements in array:
5
Enter 5 elements:
12345
Please enter the location where you want to insert a new element:
4
Please enter the value:
10
Resultant Array is:
1 2 3 10 4 5
Deletion Operation:
Deletion refers to removing an existing element from the array and re-organizing all elements
of an array.
Algorithm:
Page |4
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 Kth 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:
#include<stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);//1 3 5 7 8
}
j = k;j=3
while( j < n) 3<5{
LA[j-1] = LA[j];// 3-1=2//index value
j = j + 1;//actual
}
n = n -1;//5-1=4;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);//1 5 7 8
}
}
Or
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);//6
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);//
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
Page |5
Output:
Enter number of elements in array:
5
Enter 5 elements:
5 10 15 20 25
Enter the location where you wish to delete element:
2
Resultant array is:
5 15 20 25
Example:
A =| A1 A2 A3|
|A4 A5 A6|
Page |6
On the other hand, in arrays are stored in column-major order, while the array indexes are
still written row-first (colexicographic access order):
Fortran: column-major order
(colexicographic access order), one-
based indexing
Address Access Value
1 A(1,1) A1
2 A(2,1) A2
3 A(1,2) A3
4 A(2,2) A4
5 A(1,3) A5
6 A(2,3) A6
Advantages & Disadvantages of Arrays:
Advantages:
It is better and convenient way of storing the data of same datatype with same size.
Page |7
Disadvantages:
Array size is fixed
Array is homogeneous
Array is Contiguous blocks of memory.
Insertion and deletion are not easy in Array
Multidimensional Arrays :
Multidimensional arrays in simple words as array of arrays. Data in multidimensional arrays
are stored in tabular form (in row major order).
General form of declaring N-dimensional arrays:
data_type array_name[size1][size2]....[ sizeN];
data_type: Type of data to be stored in the array.
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions
Examples:
Two dimensional array:
int two_d[10][20];
Three dimensional array:
int three_d[10][20][30];
Matrices:
Matrix is a two dimensional array. A rectangular array of m × n numbers (real or complex) in
the form of m horizontal lines (called rows) and n vertical lines (called columns), is called a
Page |8
Sparse Matrices:
Sparse matrices are matrices with a relatively high proportion of zero entries.
The natural method of representing matrices as 2d array is not suitable for sparse matrices. It
is possible to save by storing only non-zero entries.
|0 0 1 0 0|
|0 2 0 0 0|
|0 0 0 0 0|
|0 0 0 3 0|
|4 0 0 0 0|