Insert &Delete an element in 1-D array: C Program

0
39

C program to insert an element in 1-D array

This code will insert an element into an array, For example consider an array a[20] having three elements in it initially and a[0] = 6, a[1] = 7 and a[2] = 9 and you want to insert a number 10 at location 1 i.e. a[0] = 10, so we have to move elements one step below so after insertion a[1] = 6 which was a[0]=10 initially, and a[2] = 7 and a[3] = 9. Array insertion does not mean increasing its size i.e array will not be containing 21 elements.

C  program to delete an element from  1-D array

This program delete an element from an array. Deleting an element does not affect the size of array. It is also checked whether deletion is possible or not, For example if array is containing five elements and you want to delete element at position six which is not possible.

[message_box title="PROGRAM" color="yellow"] #include <stdio.h> #include <conio.h> 

void main() { int array[20], position, c, n, value; clrscr();   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 want 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]);   getch(); } [/message_box]

[/sociallocker-bulk-1]

Leave a Reply

Be the First to Comment!

Notify of
wpDiscuz