0% found this document useful (0 votes)
146 views

Insertion and Deletion in An Array

This C program allows the user to insert an element into an array at a specified position by shifting all subsequent elements over by one index. It also allows the user to delete an element from a specified position by shifting all subsequent elements back by one index. The program prompts the user to enter the size of the array, the array elements, the insertion/deletion position, and the value to insert. It then prints the resultant array.

Uploaded by

anuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Insertion and Deletion in An Array

This C program allows the user to insert an element into an array at a specified position by shifting all subsequent elements over by one index. It also allows the user to delete an element from a specified position by shifting all subsequent elements back by one index. The program prompts the user to enter the size of the array, the array elements, the insertion/deletion position, and the value to insert. It then prints the resultant array.

Uploaded by

anuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Insertion

#include <stdio.h>

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;
}
deletion in an array

#include <stdio.h>
int main()
{
   int array[100], position, c, n;
   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 delete element\n");
   scanf("%d", &position);
   if (position >= n+1)
      printf("Deletion not possible.\
n");
   else
   {
   
for (c = position - 1; c < n - 1; c++)
         array[c] = array[c+1];
      printf("Resultant array:\n");
      for (c = 0; c < n - 1; c++)
         printf("%d\n", array[c]);
   }
   return 0;
}

You might also like