F How To Insert an element at a particular position in an integer array? | CodeTheta

How To Insert an element at a particular position in an integer array?

April 16, 2014

 This program allows you to insert a particular element in a particular array position.


/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#define MAX 20
void insert_in_array(int[], int, int, int);
void main()
{
int a[MAX], pos, it, max, i;
printf("/* http://native-code.blogspot.com */ \n\nEnter maximum Array range:\n");
scanf("%d", &max);
puts("Enter the elements:");
for(i=0; i<max; i++)
scanf("%d", &a[i]);
puts("Enter the position and value");
scanf("%d %d", &pos, &it);
insert_in_array(a, pos, it, max);
getch();
}
void insert_in_array(int a[], int pos, int it, int max)
{
int i;
pos=pos-1;
for(i=max; i>=pos; i--)
a[i+1]=a[i];
if(pos<=max)
{
a[pos]=it;
for(i=0; i<=max; i++)
printf("%d ", a[i]);
}
else
return 0;
}

Post a Comment