Thursday 9 May 2013

Delete an element from an array of integers



DESCRIPTION: This program deletes an element from an array of integers at the valid position specified by the user.


PROGRAM:


#include<stdio.h>
#include<stdlib.h>
main()
{
  int n,arr[20],i,j,pos;
  printf("enter the no of elements\n");
  scanf("%d",&n);
  printf("enter the elements:\n");
  for(i=0;i<n;i++)
  scanf("%d",&arr[i]);
  printf("enter the position to delete the element:\n");
  scanf("%d",&pos);
  if(pos>n)
 {
  printf("invalid\n");
  exit(0);
 }
  for(j=pos-1;j<n;j++)
  arr[j]=arr[j+1];
  for(i=0;i<n-1;i++)
  printf("%d ",arr[i]);
}



OUTPUT WINDOW:




No comments:

Post a Comment