Facebook

Insertion of element in an array

Inserting an element means to embed element in the array.It is quite easy to insert the element in the end of array provided sufficient memory locations are present to accommodate the additional element.On the controversial side,what is needed to insert the element at our desired location like somewhere in the middle of array .All the elements( after insertion at requisite location) must be moved downwards to welcome new element and to keep their order.By the word downward I mean moving to larger subscripts.

Ways to insert an element in the array

  • At the beginning of array
  • At the end of array
  • At any location of user choice

At the beginning of array

The following program is to insert element in the beginning of array.

Program

#include<stdio.h>

 int main()
 {
 int a[10];
 int i,k;
 int element;
 //printf("enter array elements:");
 for(i=0;i<5;i++)
 {
 scanf("%d",&a[i]);
 }
 //printf("enter the element you want to insert:");
 scanf("%d",&element);
 for(k=5;k>0;k--)
 a[k]=a[k - 1];
 a[0]=element;//Fig 3
  printf("\n");
 for(i=0;i<6;i++)//display the result
 printf("%d \t",a[i]);
return 0;
 }
Run

 

 

Illustration of Program

  • Array a of integer type and size 10 is declared.
  • i is set to 0 and condition of for loop is i <5 which means that we can enter up to 5 values in array.The remaining memory locations are to adjust the new elements if user wants to insert at later stage.
  • scanf reads 5 values  from the user .If user gives more than 5 values then those will be simply discarded as loop runs only 5 times.Control exits the loop when i becomes equal to 5. Say user enters 10,20,30,40,50(Fig 1)
  • Say user want to insert 45 in the beginning of array.
  • Working of for loop is explained in the Fig 2.
Fig 1 : An Array
Fig 2 : Working of the program
Fig 3 : Insertion at the beginning of array

At the end of array

The following program is to insert element at the end of array.

Program

#include<stdio.h>

 int main()
 {
 int a[10];
 int i;
 int element;
 //printf("enter array elements:");
 for(i=0;i<5;i++)
 scanf("%d",&a[i]);
 //printf("enter the element you want to insert:");
 scanf("%d",&element);
 a[5]=element;//Fig 4
 for(i=0;i<6;i++)
 printf(" %d",a[i]);
 return 0;
 }
Run

 

Illustration of Program

  • Array a of integer type and size 10 is declared.
  • i is set to 0 and condition of for loop is i <5 which means that we can enter up to 5 values in array.The remaining memory locations are to adjust the new elements if user wants to insert at later stage.
  • scanf reads 5  values  from the user .If user gives more than 5 values then those will be simply discarded as loop runs only 5 times.Control exits the loop when i becomes equal to 5. Say user enters 10,20,30,40,50(Fig 3)
  • Say user wants to insert 65 at the end of array.
  • The elements are located till 4 index.
  • In order to insert element at the last,we give value of element to value of  5th index.(a[5] = element).(Fig 4)
  • All the elements are displayed through printf.
Fig 3: Elements of array before insertion
Fig 4 : Insertion at the end of array

 

At any location of user choice

The following program is to insert element at any location.

Program

#include<stdio.h>

 int main()
 {
 int a[6];
 int i,k;
 int element_choice;
 int element;
 //printf("enter array elements:");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 //printf("enter the element:");
 scanf("%d",&element);
 //printf("enter the element after which the number has to be inserted:");
 scanf("%d",&element_choice);
 for(i=0;i<5;i++)
 {
 if(a[i]==element_choice)//finding the element
 break;
 }
 for(k=5;k>i;k--)
{
  a[k]=a[k -1];//shifting the element
}
 a[i+1]=element;
 printf("\n");
 for(i=0;i<6;i++)
{ 
 printf("%d \t",a[i]);
}
 return 0;
 }
Run

 

 

Illustration of Program

  • Array a of  integer type  and  size 10 is declared.
  •  is set to 0 and condition of for loop is i <5 which means that we can enter up to 5 values in array.The remaining memory locations are to adjust the new elements if user wants to insert at later stage.
  • scanf reads 5 values  from the user .If user gives more than 5 values then those will be simply discarded as loop runs only 5 times.Control exits the loop when i becomes equal to 5. Say user enters 10,20,30,40,50(Fig 5)
  • Say user wants to insert 90 after 30 in array.(Fig 6)
Fig 5 : Elements of array before insertion
Fig 6 :Working of the Program
Fig 7 : Insertion at any location of user choice

 

Rate this post

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar