DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION USING C

Write a program (wap) to delete an element at desired position from an array in c language

#include<stdio.h>
int main(){
  int a[50],i,pos,size;
  printf("\nEnter size of the array: ");
  scanf("%d",&size);

  printf("\nEnter %d elements in to the array: ",size);
  for(i=0;i<size;i++)
            scanf("%d",&a[i]);

  printf("\nEnter position where to delete: ");
  scanf("%d",&pos);

  i=0;
  while(i!=pos-1)
            i++;
  while(i<10){
            a[i]=a[i+1];
            i++;
  }

  size--;
  for(i=0;i<size;i++)
            printf(" %d",a[i]);

  return 0;
}

If you have any suggestion of above program for deleting an element from an array, pleases suggest us.  

13 comments:

  1. Why is that i<10? Please explain it.

    ReplyDelete
    Replies
    1. if the value of i is less than 10... for example the value of i is 0 then the condition is false because 0
      is less than 10

      Delete
  2. your program doesn't actually delete the last element does it?

    ReplyDelete
  3. i think there is a problem in it......

    ReplyDelete
  4. The last element still occupies memory but is not being shown.

    So technically you have made it invisible and not deleted it.

    ReplyDelete
  5. i think it shud b (i!=pos) in first while loop..m i correct..??

    ReplyDelete
  6. printf("enter the position at which element has to be deleted");
    scanf("%d",&loc);
    for(i=loc-1;i<n-1;i++)
    a[i]=a[i+1];
    for(i=0;i<n-1;i++)
    printf("%d\n",a[i]);
    system("pause");
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. shouldn't it be like this.
    ----------------------

    if (pos < size) {
    i=(pos-1);
    while(i<size){
    a[i]=a[i+1];
    i++;
    }
    }

    ReplyDelete
  9. what is the meaning of while(i<10) .
    i cant understand.....

    ReplyDelete
  10. i think here should be while(i<size).... ???
    am i right????

    ReplyDelete
  11. #include
    #include
    void main()
    {
    int a[10],count[20],i,j,n,count1[20],max,b[10],init;
    clrscr();
    printf("Enter the array size\n");
    scanf("%d",&n);
    printf("Enter the array elements\n");
    for(i=0;icount1[j])
    {
    count1[i]=count1[i]+count1[j];
    count1[j]=count1[i]-count1[j];
    count1[i]=count1[i]-count1[j];
    }
    }
    }
    max=count1[n-1];
    init=-1;
    for(i=0;i<n;i++)
    {
    if(max==count[i])
    {
    init++;
    b[init]=a[i] ;

    }
    }
    // printf("%d\n",init);
    // printf("the max elements are %d %d\n",b[0],b[1]);

    for(i=0;i<=init;i++)
    printf("the elements are %d\n",b[i]);
    getch();
    }
    regards
    cherry

    ReplyDelete
  12. actually the above program is to count a number that is repeating maximum times for example array input is {1,1,1,2,2,2,3,3,4} then output is {1,2} orlese if input is {2,2,2,11,1,3,3,4} then output is {1}
    regards
    cherry

    ReplyDelete