6 Most Important Operations on 1-D Arrays in C | Arrays in C | C programming Tutorial



Welcome Programmers, Most of us are now familier with Arrays  and how to carry basic Input/Output operations with Arrays. In this tutorial, we are going to talk about the Most Important Array operations that would be even helpful for making complex programs also.


These operations include:
  • Traversal
  • Insertion
  • Search 
  • Deletion
  • Merging 
  • Sorting

****************************

1. Traversal : Traversing the array elements means accessing each element of the array for a specific purpose. Since an Array is a linear data structure (because all the elements form a sequence), traversing its elements is very simple and straight-forward.

#include<stdio.h>
int main()
{
int A[100],K=0,size;
printf(“Enter the Array size\n“);
scanf(“%d”,&size);
printf(“Enter the elements in array: \n”);
for(K=0;K<size;K++)
{
scanf(“%d”,&A[K]);
}
printf(“The Traverse of array is:\n”);
for(K=0;K<size;K++)
{
printf(“%d\n”,A[K]);
}
return 0;
}

* If A is an array, then traversing can include printing every element, counting the total number of elements, or performing any processing of these elements.

****************************

2. Insertion :  Inserting in an array is meant to insert a new element in an Existing array. Insertion in an array can be of 3 types: At the Beginning , At Middle or At Ending . Our program will be able to Insert elements at any specified position of the array.

ALGORITHM : We only need to move each element one step ahead. And when the required position is reached. We will get 2 elements having same value ,one just ahead the required position. Then we need to just overwrite the element on the required position.

#include<stdio.h>
int main()
{
int A[100],K=0,size,val,ind,i;
printf(“Enter the Array size\n“);
scanf(“%d”,&size);
printf(“Enter the elements in array: \n”);
for(K=0;K<size;K++)
{
scanf(“%d”,&A[K]);
}
printf(“Enter the value to be entered:\n”);
scanf("%d",&val);
printf(“Enter the index where to enter the value:\n”);
scanf("%d",&ind);

for(K=size;K>=ind;K++)
A[K+1]=A[K];
A[ind]=val;
size++;

printf(“The array after insertion :\n”);
for(K=0;K<size;K++)
{
printf(“%d\n”,A[K]);
}
return 0;
}

****************************

3. Deletion : Deletion of an array element may be defined as deleting an existing element from a specific position in an array.

ALGORITHM : We only need to overwrite the element that we want to delete by the the element succeeding  it, and then reduce the size by 1. By this way we can delete the required element very easily.

#include<stdio.h>
int main()
{
int A[100],K=0,size,pos;
printf(“Enter the Array size\n“);
scanf(“%d”,&size);
printf(“Enter the elements in array: \n”);
for(K=0;K<size;K++)
{
scanf(“%d”,&A[K]);
}
printf(“Enter the position from the element is to be deleted:\n”);
scanf("%d",&pos);

for(K=pos;K<size;K++)
A[K]=A[K+1];
size--;

printf(“The array after deletion :\n”);
for(K=0;K<size;K++)
{
printf(“%d\n”,A[K]);
}
return 0;
}

****************************

4. Searching : Finding a particular item in an array.

ALGORITHM : Here we need to iterate over each element of the array. And keep on comparing each value with the item to be searched. And when the item is matched then the output is to be displayed on the screen.

#include<stdio.h>
int main()
{
int A[100],K=0,size,item,found=0;
printf(“Enter the Array size\n“);
scanf(“%d”,&size);
printf(“Enter the elements in array: \n”);
for(K=0;K<size;K++)
{
scanf(“%d”,&A[K]);
}
printf(“Enter the item to be searched:\n”);
scanf("%d",&item);
found=0;
for(K=0; K<size; K++)
{
if(arr[i] == item)
{
   found = 1;
   break;
}
}
if(found == 1)
    {
        printf("\n%d is found at position %d", item, i + 1);
    }
else
    {
        printf("\n%d is not found in the array", item);
    }
return 0;
}

If the element is found, then variable found will get a incremented, and if case will be executed otherwise it will say element do not exists.

****************************

5. Merging : Merging of 2 arrays refers to joining 2 arrays together to form 1 single array from the 2.

ALGORITHM : A 3rd array(of greater size) is to be taken to store the elements of the 1st array firstly and when the last element is incremented, our incrementing variable will take us to the adjacent next location and then overwrite it with the 1st element of the 2nd array and then keep on adding the other elements.

#include<stdio.h>
int main() {
 int arr1[30], arr2[30], res[60];
 int i, j, k, n1, n2;
 
 printf("\nEnter no of elements in 1st array :");
 scanf("%d", &n1);
 for (i = 0; i < n1; i++) 
 scanf("%d", &arr1[i]);
 
 printf("\nEnter no of elements in 2nd array :");
 scanf("%d", &n2);
 for (i = 0; i < n2; i++) 
 scanf("%d", &arr2[i]);
 
 i = 0;
 j = 0;
 k = 0;
 
 while (i < n1 && j < n2) {      //Merging the Arrays
 if (arr1[i] <= arr2[j]) {
 res[k] = arr1[i];
 i++;
 k++;
 } else {
 res[k] = arr2[j];
 k++;
 j++;
 }
 }
 
 while (i < n1) {           
 res[k] = arr1[i];
 i++;
 k++;
 }
 
 while (j < n2) {
 res[k] = arr2[j];
 k++;
 j++;
 }
 printf("\nMerged array is :");
 for (i = 0; i < n1 + n2; i++)
 printf("%d ", res[i]);
 
 return (0);
 
}

****************************

6. Sorting : Sorting of an array refers to arranging a given array in a particular order, i.e ascending or descending. Sorting is of many types including bubble sort, insertion sort and selection sort but here are going to learn about the simplest approach to sort.

ALGORITHM : For sorting in ascending order we need to start from the beginning and find the minimum element of the array repeatedly by comparing each element with the element next to it, and put it at the beginning repeatedly using swap operation. And at the end we will get a sorted array.

#include <stdio.h>
int main()
{
int size;
printf("Enter the size of the array");
scanf("%d",&size);
int arr[size];
int i, j, temp;
printf("Enter elements in array: ");
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
    
for(i=0; i<size; i++)
{
  for(j=i+1; j<size; j++)
    {
     if(arr[i] > arr[j])
       {
         temp = arr[i];
         arr[i] = arr[j];
         arr[j] = temp;
       }
    }
}
printf("\nElements of array in ascending order: ");
for(i=0; i<size; i++)
  {
   printf("%d\t", arr[i]);
  }
return 0;
}
Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post