a_pramanik

insertion in array in c

Sep 30th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int array[100], position, c, n, value;
  6.  
  7.    printf("Enter number of elements in array\n");
  8.    scanf("%d", &n);
  9.  
  10.    printf("Enter %d elements\n", n);
  11.  
  12.    for (c = 0; c < n; c++)
  13.       scanf("%d", &array[c]);
  14.  
  15.    printf("Enter the location where you wish to insert an element\n");
  16.    scanf("%d", &position);
  17.  
  18.    printf("Enter the value to insert\n");
  19.    scanf("%d", &value);
  20.  
  21.    for (c = n - 1; c >= position - 1; c--)
  22.       array[c+1] = array[c];
  23.  
  24.    array[position-1] = value;
  25.  
  26.    printf("Resultant array is\n");
  27.  
  28.    for (c = 0; c <= n; c++)
  29.       printf("%d\n", array[c]);
  30.  
  31.    return 0;
  32. }
Add Comment
Please, Sign In to add comment