INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION USING C PROGRAM


How to insert or add an element in the array at specific or desired posting by using c programming language? Source code is as follow:


#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}

15 comments:

Unknown said...

insertion of an element using for loop..

printf("\nEnter the size of the array:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=num;
n++;
printf("\nElements in the array...\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);

Anonymous said...

i cant understand please explain this

Anonymous said...

but program required for inserting element into an existing array

Anonymous said...

Make this program using structure.Please.I need it.

Anonymous said...

i am not understand these example.
you make more complicated.
plz publish simple eg so that will help the student.
and add your description also ....

Anonymous said...

can not understand this program

Unknown said...

how about a element insertion with a function of 4 arguments?
one for the array, the other for lenth, index and the desired value

Anonymous said...

*insertion at any pos in an array by hitian*

#include
int main(){
int a[50],i,pos,size,item;
printf("\nEnter size of the array: ");
scanf("%d",&size);

printf("\nEnter %d elements in to the array: ",size);
for(i=0;i=pos-1)
{
a[i+1]=a[i];
i--;

}

a[pos-1]=item;

for(i=0;i<=size;i++)
printf(" %d",a[i]);

return 0;
}

Unknown said...

//error hai thoda thik karle..

#include
int main(){
int a[50],i,pos,size,item;
printf("\nEnter size of the array: ");
scanf("%d",&size);

printf("\nEnter %d elements in to the array: ",size);
for(i=0;i=pos-1)
{
a[i+1]=a[i];
i--;

}

a[pos-1]=item;

for(i=0;i<=size;i++)
printf(" %d",a[i]);

return 0;
}

SEO in Banlga said...

please describe the program, how does it work?

Anonymous said...

#include

int main()
{
int array[100], position, c, n, value;

printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);

printf("Enter the value to insert\n");
scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];

array[position-1] = value;

printf("Resultant array is\n");

for (c = 0; c <= n; c++)
printf("%d\n", array[c]);

return 0;
}

Unknown said...

#include

int main()
{
int n,pos,a[50],i,val,j;
printf("enter the no of array elements:");
scanf("%d",&n);
printf("enter the %d array elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the insert position array elements:");
scanf("%d",&pos);
printf("enter the insert position array elements:");
scanf("%d",&val);
j=n;
for(i=pos-2;i<n;i++)
{
a[j+1]=a[j];
--j;
}
a[pos-1]=val;
n++;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

Unknown said...

#include
int main()
{
int n,pos,a[50],i,val,j;
printf("enter the no of array elements:");
scanf("%d",&n);
printf("enter the %d array elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the insert position array elements:");
scanf("%d",&pos);
printf("enter the value at %d position:", pos);
scanf("%d",&val);
for(j=n-1;j<pos-1;j--)
{
a[n-1]=a[n-2];
}
a[pos-1]=val;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

Unknown said...

thanx mili...

Anonymous said...

int main()
{
int arr[50];
int n,i,num,pos,temp;
printf("nhap kich thuoc cua mang:");
scanf("%d",&n);
printf("\nnhap cac phan tu cua mang:");
for(i=0;ipos;i--)
{
arr[i]=arr[i-1];
arr[i-1]=num;
}
n++;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getchar();
getchar();
return 0;
}