User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,382 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,050 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 13505 | Replies: 49
Reply
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #21  
Oct 30th, 2004
>is that way wrong?
You're on the right track, but it will give false positives. Compare this:
#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = 0; j < 7; j++ ) {
      if ( a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}
With almost identical code that has an added test for self-comparison. If a[i] and a[j] are not the same element but have the same value, a duplicate was found:
#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = 0; j < 7; j++ ) {
      if ( &a[i] != &a[j] && a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}
You'll also notice that this finds each duplicate twice, once for a == b and once for b == a. You can fix the problem by starting the inner loop at i instead of 0 because everything up to i has already been tested:
#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = i; j < 7; j++ ) {
      if ( &a[i] != &a[j] && a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Location: Lebanon OR, 97355
Posts: 67
Reputation: N3wbi3C0d3r is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
N3wbi3C0d3r's Avatar
N3wbi3C0d3r N3wbi3C0d3r is offline Offline
Junior Poster in Training

Re: deleting duplicates in array

  #22  
Oct 30th, 2004
Oh and for your future info, galmca when you post a code type CODE in [] (
)
and at the end of the code put /CODE in [] (
)
Reply With Quote  
Join Date: Oct 2004
Location: Lebanon OR, 97355
Posts: 67
Reputation: N3wbi3C0d3r is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
N3wbi3C0d3r's Avatar
N3wbi3C0d3r N3wbi3C0d3r is offline Offline
Junior Poster in Training

Re: deleting duplicates in array

  #23  
Oct 30th, 2004
oops put CODE inside of [] and put /CODE inside of [] at the end of it
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #24  
Oct 31st, 2004
i have tried using that what u just told.....but it is still giving very weird output when i enter 5 array elements: 1 2 3 1 4 ....then it should print duplicate exists:1
instead of printing that it is printing 10 or 15 times...duplicate does not exists:
now what do i do now??
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #25  
Oct 31st, 2004
>now what do i do now??
If you have a problem with code you've written, describe the problem and POST THE CODE!

>i have tried using that what u just told
I'm guessing that you took the code I posted and butchered it into something broken.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #26  
Oct 31st, 2004
could any 1 tell plz...what do i do now?
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if((i!=j)&&(a[i]==a[j]))
printf("duplicate exists");
else
printf("duplicate does not exists");
}
}
it is still giving very weird out put by printing 10 or 15 times duplicate does not exists when i run the program...otherwise there is no any complilation error....
when i run this code and i give...5 values of an array like...
1
2
3
1
4
then instead of printing:
duplicate exists:1
it gives weird out put like i mentioned above.......so plz help me out.........i would be very greatful if u can.......
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #27  
Oct 31st, 2004
>it gives weird out put like i mentioned above
It's not weird, it's exactly what you asked for. If there's a duplicate, print "duplicate exists" and every time there is not a duplicate, print "duplicate does not exists". Change your code to this to get what you want:
#include <stdio.h>

int main ( void )
{
  int a[] = {1,2,3,1,4};
  int i, j;

  for(i=0;i<5;i++)
  {
    for(j=i;j<5;j++)
    {
      if((i!=j)&&(a[i]==a[j]))
        printf("duplicate exists: %d\n", a[j]);
    }
  }

  return 0;
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Posts: 5
Reputation: prathys is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
prathys prathys is offline Offline
Newbie Poster

Re: deleting duplicates in array

  #28  
Oct 31st, 2004
try out this code

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[5],i,j;
clrscr();
int len=5;
printf("ENTER ARRAY ELEMENTS:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{
if(a[i]==a[j])
{
a[j]=a[j+1];
len--;
j--;
}
}
}
for(i=0;i<len;i++)
printf("%d\n",a[i]);
}
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #29  
Oct 31st, 2004
>try out this code
Why bother? It won't work, and even if it did I wouldn't run it anyway because it has undefined behavior. Shall we tick off the problem areas?

>#include<conio.h>
This program does nothing that warrnats features from conio.h. You should avoid this nonstandard and highly nonportable header whenever possible.

>int main(void)
At least you got this right.

>clrscr();
Very antisocial. If you're running the program in an IDE window then there's no reason to clear the screen. If you're running the program on a command line then clearing the screen will **** off anyone who was interested in the output of previously run programs. Either way you don't want to clear the screen.

>int len=5;
This is why the program won't compile, which tells me that you're trying to write C programs and compile them as C++. This leads to all sorts of bad style and error prone C code. Remember: In C, declarations must be at the beginning of a block. Because you called clrscr(), the declaration for len is not at the beginning of the block. [Note: In C99 you can declare variables anywhere, but you're not probably not using C99 as I don't know of a compiler that supports C99 and conio.h]

>printf("ENTER ARRAY ELEMENTS:");
You didn't flush the output stream, so some users may not get this prompt. The program will sit and wait for user input and the user will sit and wait for a prompt for input. This is called a deadlock, and it is to be avoided.

>scanf("%d",&a[i]);
Your lack of error checking is disappointing. User input should always be tested for validity, and any use of scanf should be tested carefully because nobody seems to know how to use it correctly.

>fflush(stdin);
This is undefined, fflush is only defined to work on output streams.

><snip loop>
Did you even compile and run this code? It will loop infinitely. What's even more annoying is that the reason it will loop infinitely is because you made a mistake that I pointed out very clearly in this thread as a potential problem. Though if it makes you feel any better, once the self-comparison problem is fixed, the loop will still be broken for just about every input sequence.

>printf("%d\n",a[i]);
>}
You declare main correctly and then forget to return a value. This is undefined behavior unless you're compiling as C++ or C99. Since we've already established that you aren't compiling as C99, I have to again assume C++, which is still a bad idea.

Shall we look at the same program with the changes needed to make it work?
#include<stdio.h>

int main ( void )
{
  int a[5], len;
  int i, j, k;

  printf ( "ENTER ARRAY ELEMENTS: " );
  fflush ( stdout );
  for ( len = 0; len < 5; len++ ) {
    if ( scanf ( "%d", &a[len] ) != 1 )
      break;
  }

  for ( i = 0; i < len; i++ ) {
    for ( j = i; j < len; ) {
      if ( i != j && a[i] == a[j] ) {
        for ( k = j; k < len - 1; k++ )
          a[k] = a[k + 1];

        --len;
      }
      else
        ++j;
    }
  }

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

  return 0;
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #30  
Oct 31st, 2004
thx alot narue..... my code is running fine now......but im also really very sorry about....that day when i complained about not getting my program done .....to be honest with u this is the first time i have ever been into any forum site where ican actually put up questions and try to solve a problem as im a newbie here.....so i really didn't know at all about the rules about this site and how do the people work here so im really sorry about that......now i know that u people do thsi just for the convienience of us for free....so that's really nice to know.....and im proud of u guys!!!!!!!
just don't mind plz.......im not a girl like what u thought........at all......im really sorry once again......if i made u feel like that.......plz forgive me......if u can.......i'll be very greatful to u.......and thx alot for ur help and that u took time out for me..........
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 4:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC