/* Insertion in an array: Inserting a new element at a desired position in an array. */
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n+1], x, y, i;
for(i = 0; i < n; i++){
printf("Enter element %d: ", i+1);
scanf("%d", &a[i]);
}
printf("Array you entered is .....\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nEnter the position and element you want to insert: ");
scanf("%d %d", &y, &x);
for(i = n; i >= y-1; i--){
a[i+1] = a[i];
}
a[y-1] = x;
printf("\nNew array is .....\n");
for(i = 0; i <= n; i++)
printf("%d ", a[i]);
return 0;
}
Comments
Post a Comment