Friday, 5 July 2013

Program to insert an element in an array



//Header Files
#include<stdio.h>
#include<conio.h>
#include<process.h>
int arr[10],n,i,j,k,item,loc;

void main()
{
clrscr();
printf("Enter size of array:");
scanf("%d", &n);

if(n>10)
{
    printf("Entered value exceed the limit of array");
    getch();
    exit(0);
}
printf("\n Enter array elements....");
for(i=0;i<=n-1;i++)
    scanf("%d",&arr[i]);
printf("Enter the element to be inserted...");
scanf("%d",&item);
printf("Enter its location...");
scanf("%d",&loc);
for(j=n-1;j>=loc;j--)
    arr[j+1]=arr[j];

arr[loc]=item;
printf("\n Array after Insertion...");
k=0;
while(k<=n)
{
    printf("\n");
    printf("%d", arr[k]);
    k=k+1;
}
getch();
}

No comments:

Post a Comment