Insertion in array


Insertion an element in the array means adding a new data element in an already existing array.we
just have to add 1 to the upper_bond and assign the value. The memory space allocated for the array is still available.
For example If an array is declared to contain 10 elements,but currently it is having only 8 elements,then obviously there is space to accommodate two more elements.But if already has 10 elements,then we will not be able to add.


Write a program to insert a number at given location in an array


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,num,pos,arr[10];
clrscr();
printf("\n Enter the number of elements in the array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
print("\n arr[%d]=:",i);
scanf("%d",&arr[i]);
}
printf("\n Enter the number to be inserted:");
scanf("%d",&num);
printf("\n enter the position at which the number has to be added:");
scanf("%d".&pos);
for(i=n;i>=pos;i--)
arr[i+1]=arr[i];
arr[pos]=num;
n++;
printf("\n The array after insertion of %d is:",num);
for(i=0;i<n+1;i++)
printf("\n arr[%d]=%d",i,arr[i]);
getch();
}

Comments