Friday, August 27, 2010

C Program to insert an element in array



#include<stdio.h>
#include<conio.h>
int i,len,pos,num;
main()
{
int a[100];
void insert(int a[],int,int,int);
clrscr();
printf(" Enter the no. of array elements  to be read ");
scanf(" %d",&len);
printf("\n Enter integers ");
for(i=0;i<=len-1;i++)
{
scanf(" %d",&a[i]);
}
printf(" Enter integer to be inserted ");
scanf("%d",&pos);
--pos;
insert(a,len,pos,num);
}
void insert(int a[],int len,int pos,int num)
{
for(i=len;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;

if(pos>len)
{
printf(" Insertion Outside the Array ");
}
len++;
printf(" \n New Array");
for(i=0;i<len;i++)
{
printf(" %d\n",a[i]);
}
}


No comments:

Post a Comment