Q:

C Program To Delete Element From Array At Desired Or Specific Position

0

This C program is to delete an element from an array from a specified location/position.For example, if an array a consists of elements a={7,8,12,3,9} and if we want to delete element at position 3 then the new array would be a={7,8,12,9} (as array starts from index 0).

Logic:We start to iterate from the position from which we want to delete the element.The reason for this is so that all the elements after the element which is deleted will be shifted by one place towards the left.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
int main()
{
  int i,arr[50],pos,len;

  printf("Enter no. of elements for the array:\n");
  scanf("%d",&len);

  printf("Enter %d elements for the array:\n",len);
  for(i=0;i<len;i++)
            scanf("%d",&arr[i]);

  printf("Enter position of the element to delete:\n");
  scanf("%d",&pos);

  i = pos-1;

  while(i<len - 1)
  {
            arr[i]=arr[i+1];
            i++;
  }
  len--;
  printf("Array after deleting element\n");

  for(i=0;i<len;i++)
            printf(" %d",arr[i]);

  return 0;
}

 

Output:

Enter no. of elements for the array:

6

Enter 6 elements for the array:

25

369

258

365

12

4

Enter position of the element to delete:

5

Array after deleting element

 25 369 258 365 4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now