Insertion of element in an array

http://install4install.com

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

In 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

Screen Shot 2014-03-20 at 3.31.20 PM

Fig 1 : Elements of array before insertion

Screen Shot 2014-03-20 at 3.32.56 PM

Fig 2 : Working of Program.

Screen Shot 2014-03-20 at 3.33.11 PM

Fig 3 : Insertion at the beginning of array.

 

In the end of array

The following program is to insert element in 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

Screen Shot 2014-03-20 at 3.31.20 PM

Fig 3: Elements of array before insertion

Fig 4 : Insertion in the end of array

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

Screen Shot 2014-03-20 at 3.31.20 PM

Fig 5 : Elements of array before insertion

Screen Shot 2014-03-20 at 4.54.13 PM

Fig 6 :Working of Program

Screen Shot 2014-03-20 at 4.54.22 PM

Fig 7 : Insertion at any location of user choice.

 



Short URL: http://tinyit.cc/4088dc
Author: Cusp2207 on March 21, 2014
Category: Algorithms, Computer Science, Data Structures
Tags:

Leave a Reply

Last articles