Check for repeated number and delete

Asked

Viewed 64 times

-2

Enter the data in VET, and then check the repeated numbers, and leave in VET1 only the numbers that are not repeated, but I’m not able to do this last part at all, follow the code I’ve done so far:

#include <stdio.h>
#include <stdlib.h>
#define T 3

int main()
{
    int vet[T];
    int vet1[T];
    int i, j, achou = 0, quant = 0;

    printf("Digitar os numeros:");
    for(i = 0; i < T; i++)
    {
        scanf("%d", &vet[i]);
    }
    for(i = 0; i < T; i++)
    {

        for(j = 0; j < quant; j++)
        {
            achou = 0;
            if(vet[i] == vet1[j])
            {
                achou = 1;
                break;
            }
            if(achou == 0)
            {
                vet1[quant] = vet[i];
                quant ++;
            }
        }

    }
    for (i = 0; i <= quant; i++)
    {
        printf("%d", vet[i]);
    }
    return 0;
}
  • 1

    the second loop will never run pq Uant is zero

  • even moving to T, still does not work :/

1 answer

0


I managed to make:

#include <stdio.h>
#include <stdlib.h>
#define T 5

int main(){
int vet[T];
int vet1[T];
int i, j, n= 0;

printf("Digitar os numeros:");
for(i = 0; i < T; i++)
    {
        scanf("%d", &vet[i]);
    }
    for(i = 0; i < T; i++)
    {
        for( j = 0; j < n; j++ )
        {
            if( vet[i] == vet1[j] )
                break;
        }

        if( j == n )
        {
            vet1[n] = vet[i];
            n++;
        }
      }
    for (i = 0; i <n; i++)
    {
        printf("%d", vet1[i]);
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.