Translate

Labels

Sunday 16 June 2013

PROGRAM TO INSERT AN ELEMENT IN AN ARRAY

#define MAX 10
main()
{
int a[MAX]={11,22,33,44,55,77,88};
int n,x;
printf("Enter an element to be inserted \n");
scanf("%d",&x);
printf("Enter the position at which\n");
printf("this element to be inserted \n");
scanf("%d",&n);
insert(a,x,n);
}


insert(int *a,int x,int n)
{
int i,j=0,*a1,temp;
a1=(int*)malloc((MAX-n)*sizeof(int));
for(i=n;i<MAX;i++)
*(a1+j++)=*(a+i);
*(a+n)=x;
for(i=n+1;i<MAX;i++)
*(a+i)=*(a1++);
for(i=0;i<MAX;i++)
printf("%d",*(a+i));
putchar('\n');
}

OUTPUT
Enter an element to be inserted 
66
Enter the position at which
this element to be inserted 
3
11   22   33   66   44   55   77   88   0   0

No comments: