hi
i have just 1 question could any of u plz solve this.,it is.........TO DELETE DUPLICATE ELEMENTS IN AN ARRAY.......i have done like this......
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,flag;
clrscr();
printf("ENTER ARRAY ELEMENTS:");
scanf("%d",&a[i]);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if((i!=j)&&(a[i]==a[j]))
{
printf("duplicate exists:%d",a[j]);
flag=1;
break;
}
}
if(flag=1)
for(k=j;k<4;k++)
{
a[k]=a[k+1];
printf("\nARRAY AFTER DELETION:");
for(i=0;i<4;i++)
printf("\n%d",a[i]);
}
}
getch();
}
but when i run this code although there is no compilation error but when i run this...then....
ENTER ARRAY ELEMENTS:
10
20
10
30
40
THEN IT PRINTS:::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
30
ARRAY AFTER DELETION:
10
20
30
40
so why does it do that way then?why is it printing twice?the first time it is printing wrongly and the second time it is printing the desired output....is there any mistake in my code?......if there is then plz help me out.....
but when i run this codeby entering five values as.....
ENTER ARRAY ELEMENTS:
10
20
30
10
40
then it prints::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
40
now it is printing rightly......why?
whereas when i put the duplicate element that's 10 in the third place above it was printing twice first wrongly and second rightly and now when the duplicate element that's 10 is positioned in the fourth place is printing rightly....why?
plz help me out.......tell me what was wrong in that code?or what was lacking?how can i make it run correctly for any duplicate element positioned anywhere in an array......
plz help me out!!!!!!!