Array operations in C – Part 2
Written by
Array operations in C – Part 2
The next set of array operations are:
- Insertion
- Deletion
- Searching
- Merging
- Insertion of an element into the array:
- Insertion of an element in the array, could either be at the start , at the end or anywhere in between as well.
- We take the location at which the user wants to insert the element into the array.
- Next, we check if the position entered is valid or not. For the user the position would start from number 1. Thus in terms of array index, actual array index position is position – 1
- If the position is invalid same is communicated to the user and program is terminated.
- The position is invalid if position is < 1 i.e. less than starting of array and position > n+1 , i.e. if your array has 4 elements; the user might want to insert element at 4th position which is nth position, else also insert it as the n+1th element i.e. 5th element or after the current arrays end.
- If position is valid, the element is inserted at required location and resultant array is displayed.
Code:
#include <stdio.h>
int main()
{
int array[100], position, i, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter array elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
if(position > n+1 || position < 1)
{
printf("The position entered is invalid\n");
}
else
{
printf("Enter the value to insert\n");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i--)
array[i+1] = array[i];
array[position-1] = value; //inserting value at the required location
printf("Resultant array is:\n");
for (i = 0; i <= n; i++)
printf("%d\n", array[i]);
}
return 0;
}
Output:
Case 1: Insertion location valid
Enter number of elements in array
6
Enter array elements:
12
13
14
15
16
17
Enter the location where you wish to insert an element
3
Enter the value to insert
20
Resultant array is:
12
13
20
14
15
16
17
Case 2: Insertion location is invalid
Enter number of elements in array
4
Enter array elements:
10
11
12
13
Enter the location where you wish to insert an element
6
The position entered is invalid
Deletion of an element from the array:
- For deletion of an element from the array, we need to accept the location from which user wishes to delete the element.
- When the location is entered by user, we store it in a variable – position.
- For user location starts from position : 1. However array index starts from 0. Hence, when we delete the element we need to delete element present at location = position -1.
- What we essentially do is, shift the element next to the element to be deleted to the location = position -1, i.e. the next element is placed in position of deleted element and so on we keep shifting the remaining elements by one position to the left.
- If an invalid location is entered, deletion is not possible an the same is conveyed to the user.
Code:
#include <stdio.h>
int main()
{
int array[100], position, i, n, num;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the location from where you wish to delete the element:\n");
scanf("%d", &position);
num = array[position-1];
if (position >= n+1 || position < 0) /*n+1, since user will count element as position 1
onwards. Internally though indexing starts from 0,
which user would not know.*/
printf("Deletion not possible as entered location is invalid.\n");
else
{
for (i = position - 1; i < n - 1; i++) //since array index starts from 0; thus position in terms
of array index is position-1
array[i] = array[i+1];
printf("Resultant array after deletion of element %d from location %d:\n", num, position);
for (i = 0; i < n - 1; i++)
printf("%d\n", array[i]);
}
return 0;
}
Output:
Case 1: Entered location is valid
Enter number of elements in array
5
Enter array elements:
14
15
16
17
18
Enter the location from where you wish to delete the element:
3
Resultant array after deletion of element 16 from location 3:
14
15
17
18
Case 2: Entered location is invalid
Enter number of elements in array
5
Enter array elements:
14
15
16
17
18
Enter the location from where you wish to delete the element:
6
Deletion not possible as entered location is invalid.
- Searching element of an array:
- Searching an element of an array is to check if a user entered element is present in the array or not. If present we display the position at which the element was found in the array.
- There are various searching techniques that can be used to find an array element.
- We are going to learn to implement the same using Linear Search.
- Linear search is not efficient as it scans for each element sequentially, however it is the easiest to understand and implement for beginners.
Approach:
- We accept the array input from user.
- The user will enter the element that needs to be searched in the array.
- We scan the entire array sequentially to search for entered element ‘num‘.
- On discovering the element in the array at location i, we display it to the user.
- If the element occurs at more than one place as well, the search continues until end of array is reached.
- Once end of array is reached, loop is exited.
- If even after scanning entire array element is not found, ( i==n) then same is conveyed to user.
Code:
#include <stdio.h>
int main()
{
int array[100], num, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter array elements: \n");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &num);
for (i = 0; i < n; i++)
{
if (array[i] == num) /* When required element is found */
{
printf("The element: %d is present at location %d.\n", num, i+1);
if(i == n-1) //if end of array then exit search
break;
}
}
if (i == n)
printf("The element %d is not present in the array.\n", num);
return 0;
}
Output:
Case 1: Element present in array
Enter number of elements in array
10
Enter array elements:
10
20
30
40
50
60
20
10
30
10
Enter a number to search
10
The element: 10 is present at location 1.
The element: 10 is present at location 8.
The element: 10 is present at location 10.
Case 2: Element not present in array
Enter number of elements in array
4
Enter array elements:
10
20
30
50
Enter a number to search
40
The element 40 is not present in the array.
- Merging of two arrays:
- Two arrays are taken as input from the user in order to merge them into a resultant array ‘res‘.
- The arrays can be sorted or unsorted.
- You can either after taking the input from the user, sort the array else after merging sort the array.
- In our case, we first merge the array and then sort them.
- For sorting, we use the basic bubble sort technique to sort the array in ascending order as previously seen in sorting an array program.
- Since in our case, we sort the array post merging, initially you can simply save elements of arr1 and arr2 into result in the entered order itself.
- However, we have written the code for merge considering that if the array input by user is already sorted then the merge function alone would suffice the need.
- The function, compares the elements of the two arrays and inserts the elements in the ascending order into res array.
- Since, we do not know if user will always enter a sorted array, we sort the resultant array res post merging as well.
Code:
#include<stdio.h>
void merge(int arr1[20], int arr2[20], int n1, int n2) //merging arrays
{
int i, j, k;
int res[40];
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2)
{
if (arr1[i] <= arr2[j])
{
res[k] = arr1[i];
i++;
k++;
}
else
{
res[k] = arr2[j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining where as array 'arr2' is exhausted */
while (i < n1)
{
res[k] = arr1[i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining where as array 'arr1' is exhausted */
while (j < n2)
{
res[k] = arr2[j];
k++;
j++;
}
sort(res, (n1+n2));
//Displaying elements of array 'res'
printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("\n%d", res[i]);
}
void sort(int arr[20], int n) //to sort the resultant array
{
int i, j, swap;
for (i = 0 ; i < n - 1; i++)
{
for (j= 0 ; j < n - i - 1; j++)
{
if (arr[j] > arr[j+1]) /* For decreasing order use < */
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
}
int main()
{
int arr1[20], arr2[20];
int i, n1, n2;
printf("Enter no of elements in 1st array :\n");
scanf("%d", &n1);
printf("Enter elements of 1st array : \n");
for (i = 0; i < n1; i++)
{
scanf("%d", &arr1[i]);
}
printf("\nEnter no of elements in 2nd array :\n");
scanf("%d", &n2);
printf("Enter elements of 2nd array : \n");
for (i = 0; i < n2; i++)
{
scanf("%d", &arr2[i]);
}
merge(arr1, arr2, n1, n2);
return 0;
}
Output:
Enter no of elements in 1st array :
4
Enter elements of 1st array :
96
54
32
174
Enter no of elements in 2nd array :
5
Enter elements of 2nd array :
20
35
201
11
4
Merged array is :
4
11
20
32
35
54
96
174
201