Monday, 12 January 2015

Program to Insert an Element into an Unsorted Array

                    Program to Insert an Element into an Unsorted Array

#include <stdio.h>
void insert_unsorted();
int a[50], n, loc, item;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter location of insertion: ");
scanf("%d", &loc);
printf("\nEnter ITEM to insert: ");
scanf("%d", &item);
insert_unsorted();
printf("\n\nAfter insertion:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insert_unsorted()
{
int i=n-1;
while(i>=loc-1)
{
a[i+1] = a[i];
i--;
}
a[loc-1] = item;
n++;
}


OUTPUT


0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More