Insert any element into an array at a desired position
#include<stdio.h>
#include<conio.h>
main()
{
int a[15],n,i,item,pos;
clrscr();
printf("\n How many elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element no. %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Array elements are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n enter the element to be inserted : ");
scanf("%d",&item);
printf("\n enter the position of insertion : ");
scanf("%d",&pos);
for(i=n;i>=pos;i--) // shifting the elements
a[i]=a[i-1];
a[--pos]=item;
/* pos=pos-1; // pos. is decremented b'coz of Array storage process
a[pos]=item; */
printf("\n New array elements are:");
printf("\n");
for(i=0;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[15],n,i,item,pos;
clrscr();
printf("\n How many elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element no. %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Array elements are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n enter the element to be inserted : ");
scanf("%d",&item);
printf("\n enter the position of insertion : ");
scanf("%d",&pos);
for(i=n;i>=pos;i--) // shifting the elements
a[i]=a[i-1];
a[--pos]=item;
/* pos=pos-1; // pos. is decremented b'coz of Array storage process
a[pos]=item; */
printf("\n New array elements are:");
printf("\n");
for(i=0;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
No comments:
Post a Comment