Problem with C program

Asked

Viewed 35 times

-1

I am solving an exercise that asks to read a vector of 20 positions and print on the screen excluding repeated values, I was able to solve however, it is showing the values on the screen several times.

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

int main()
{
 int vet[20], x;
 for(int i = 0; i < 20; i++)
 {
     printf("Digite um numero: ");
     scanf("%d", &vet[i]);
 }
 for(int i = 1; i < 20; i++)
 {
    for(x = 0; x < 20; x++)
    {
       if(vet[i] != vet[x])
       {
          printf("\n%d", vet[x]);
       }
    }
 }

}

1 answer

-1


A small change in your program improves this solution:

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

int main() {
    int vet[20], x, repete;
    for(int i = 0; i < 20; i++) {
        printf("Digite um numero: ");
        scanf("%d", &vet[i]);
    }
    for (int i = 0; i < 20; i++) {
        repete = 0
        for(x = 0; x < 20; x++) {
            if(i != x && vet[i] == vet[x]) {
                repete++;
            }
        }
        if (repete == 0)
            printf("\n%d", vet[x]);
    }
    return 0;
}
  • Thank you so much, I was racking my brain here for something so simple kkkkk

Browser other questions tagged

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