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.  

18 comments:

Zain said...

Why is that i<10? Please explain it.

Anonymous said...

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

Anonymous said...

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

Jatin Sindhu said...

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

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

Unknown said...

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

Unknown said...

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");
}

Unknown said...
This comment has been removed by the author.
Ashish Gupta said...

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

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

Unknown said...

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

Unknown said...

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

Anonymous said...

#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

Anonymous said...

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

Anonymous said...

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

Unknown said...

guys dere are two errors in the source code

firstly have a look at the second while loop from the bottom
it shud be while(i!=pos)
secondly in the first while loop from the bottom it shud be while(i<size)
:)

Unknown said...

yes it does ...note that the position here starts from 1 not 0

Unknown said...

gud

Unknown said...

simple code is here


#include

int main()
{
int a[100],i,n,pos;

printf("Enter the size of array : ");
scanf("%d",&n);

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

printf("Enter the position to delete number : ");
scanf("%d",&pos);

for(i=pos-1;i<n;i++)
{
a[i]=a[i+1];
}
n--;

printf("Elements of array are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

Unknown said...

Modified code is :


#include

int main()
{
int a[100],i,n,pos,k;

printf("Enter the size of array : ");
scanf("%d",&n);

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

printf("Enter the position to delete number : ");
scanf("%d",&pos);

printf("\nElements of array are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);


k=a[pos-1];


for(i=pos-1;i<n;i++)
{

a[i]=a[i+1];
}
n--;


printf("\nDeleted number is %d",k);

printf("\nElements after deletion are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}