Re: duplicates in array
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 22 May 2007 06:57:11 +0000
Ramesh3839 said:
<snip>
for(i=0;i<n;i++)
{
if(A[NUM[i]]==0)
{
A[NUM[i]]=1;
}
if(A[NUM[i]]==1)
{
printf("%d",NUM[i]);
}
}
this loop structure prints only the repeated elements in an array of
integers....
Let's try it, shall we? And just for fun, let's try it on an array with
no repeats at all. So there should be no output, according to you.
#include <stdio.h>
#define n 16
int main(void)
{
int i = 0;
int A[n + 1] = { 0 };
int NUM[n] = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 };
for(i = 0; i < n; i++)
{
if(A[NUM[i]] == 0)
{
A[NUM[i]] = 1;
}
if(A[NUM[i]] == 1)
{
printf("%d", NUM[i]);
}
}
putchar('\n');
return 0;
}
Output:
12345678910111213141516
Oopsie!
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- References:
- duplicates in array
- From: ak
- Re: duplicates in array
- From: Ramesh3839
- duplicates in array
- Prev by Date: Re: file access - design thoughts.
- Next by Date: Re: gets() is dead
- Previous by thread: Re: duplicates in array
- Next by thread: Re: duplicates in array
- Index(es):
Relevant Pages
|