Tuesday, 17 May 2016

// Inserting an element in an array

#include<stdio.h>

main()
{
void insert(int[],int,int,int);

int x=0,p,top,i;
int a[20];

printf("Enetr the no. of elements you wanna insert(max 20)");
scanf("%d",&top);

printf("Enter elements in array");
for(i=0; i<=top-1; i++)
{
   scanf("%d",&a[i]);
}

printf("\nEnter the element to insert in array");
scanf("%d",&x);
printf("\nEnter the position to enter the element");
scanf("%d",&p);

if(p==19)
printf("array full");
else
insert(a,p,x,top);
}

void insert(int b[], int p, int x, int t)
{
int i=0;
for(i=0; i<=t-1; i++)
printf("%d",b[i]);
for(i=t-1; i>=p-1; i--)
{
  b[i+1]=b[i];
}
  b[p-1]=x;
for(i=0; i<=t; i++)
   printf("%d ",b[i]);
}


No comments:

Post a Comment