Adrian Buciuman
Adrian Buciuman

Reputation: 325

Find distinct numbers from an array in C

I saw the same question for C++, but I still don't know how to apply it for C even the two languages are very similar. I think the error is somwhere in the inner for loop. For example the input : v[100] = {1,2,3,3,4,1} and I expected the output : w[100] = {1,2,3,4}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v[100], w[100], n, m, i, j, distinct=1;
    printf("n= ");
    scanf("%d", &n);

    for(i=0; i<n; i++)
    {
        printf("v[%d]= ", i);
        scanf("%d", &v[i]);
    }

    for(i=0; i<n; i++)
    {
        for(j=i+1; (j<n)&&(distinct==1); j++) 
            if(v[i]==w[j])
                distinct=0;
        if(distinct==1)
        {
            w[m]=v[i];
            m++;
        }
    }
    printf("the distinct elements are: ");
    for(i=0; i<m; i++)
        printf("%d\n", w[i]);

    return 0;
}

But the output of this is: randoms number

Upvotes: 2

Views: 7354

Answers (3)

aakansha
aakansha

Reputation: 695

Hey here is a much shorter code if you like with a single loop and

complexity O(N)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
    int v[100], w[100],n,i;
    bool check[1000001];// set the size to maximum integer you can take as input
    printf("n= ");
    scanf("%d", &n);
    int j=0;    
    for(i=0; i<n; i++)
    {
        printf("v[%d]= ", i);
        scanf("%d", &v[i]);
    if(!check[v[i]])
    {
        check[v[i]]=true;
        w[j]=v[i];
        j++;
    }
    }          
    printf("the distinct elements are: \n");
    for(i=0; i<j; i++)
        printf("%d\n", w[i]);

    return 0;
}

Hope it helps.Happy Coding!!!

Let me know if you face any problem in understanding my code

Upvotes: 1

zhangyiying
zhangyiying

Reputation: 424

I fixed your code.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v[100], w[100], n, m=0, i, j;
    printf("n= ");
    scanf("%d", &n);

    for (i = 0; i<n; i++)
    {
        printf("v[%d]= ", i);
        scanf("%d", &v[i]);
    }

    for (i = 0; i<n; i++)
    {
        int distinct = 1;           <-------- changed
        for (j = 0; j < m; j++)    <-------- changed
        {
            if (v[i] == w[j])
                distinct = 0;
        }

        if (distinct == 1)
        {
            w[m] = v[i];
            m++;
        }
    }

    printf("the distinct elements are: ");
    for (i = 0; i<m; i++)
        printf("%d\n", w[i]);


    return 0;
}

There were some problems in judgment.

for (j = i + 1; (j<n) && (distinct == 1); j++)
    if (v[i] == w[j])   <--------- the w[] is empty at beginning.So you could add any element to it first time.
        distinct = 0;
if (distinct == 1)
{
    w[m] = v[i];
    m++;
}

Upvotes: 1

tom
tom

Reputation: 1313

You have not set the initial value of m to be equal to zero - it is undefined....

you also need to reset distinct to 1 for each element of the array you test. (in fact you don't need the variable distinct see below)

You also need to change the j loop - it should be

for(j=0; (j<m)&&(distinct==1); j++)

because j needs to loop through the array w

with these fixes it works.... (see below)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v[100], w[100], n, m=0, i, j, distinct=1;
    printf("n= ");
    scanf("%d", &n);

for(i=0; i<n; i++)
{
    printf("v[%d]= ", i);
    scanf("%d", &v[i]);
}
 distinct=1;
for(i=0; i<n; i++)
{
    for(j=0; (j<m)&&(distinct==1); j++)
        if(v[i]==w[j])
            distinct=0;
    if(distinct==1)
    {
        w[m]=v[i];
        m++;
    }
}
printf("the distinct elements are: ");
for(i=0; i<m; i++)
    printf("%d\n", w[i]);


return 0;
}

without distinct - can you see how this works? (This code also works)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v[100], w[100], n, m=0, i, j;
    printf("n= ");
    scanf("%d", &n);

for(i=0; i<n; i++)
{
    printf("v[%d]= ", i);
    scanf("%d", &v[i]);
}

for(i=0; i<n; i++)
{
    for(j=0; j<m; j++)
        if(v[i]==w[j])
            break;
    if(j==m)
    {
        w[m]=v[i];
        m++;
    }
}
printf("the distinct elements are: ");
for(i=0; i<m; i++)
    printf("%d\n", w[i]);


return 0;
}

Upvotes: 6

Related Questions