Source code:
/*Write a C program to insert an element / item in the sorted array. */
#include<stdio.h>
int main( )
{
int a[20],n=10,item,i;
printf("Enter the sorted array: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the item which to be inserted in the array: ");
scanf("%d", &item);
i = n-1;
while(item<a[i] && i>=0)
{
a[i+1] = a[i];
i--;
}
a[i+1] = item;
n++;
printf("ITEM to be inserted:%d\n",item);
printf("After insertion array is:\n");
for(i=0; i<n; i++)
{
printf("%d\n",a[i]);
}
return 0;
}
No comments:
Post a Comment