Problem with printing repeated numbers in C

1

The question asked me to make a code that would store 10 integer values, check if there were any numbers repeated, and if there were, print the repeated numbers, but my code or printe some numbers repeated more than once, or printa one once. How can I fix this?

#include <stdio.h>
#include <stdlib.h>
#define N 10
int main (){
    int i,Vetor[N],aux[N]={0,0,0,0,0,0,0,0,0,0},count=0,j;
    for(i=0;i<N;i++){
        printf("insira o %dº número\n",i+1);
        scanf("%d",&Vetor[i]);
    }
    for(i=0;i<N;i++){
        for(j=i+1;j<N;j++){
            if(Vetor[i]==Vetor[j]){
                count++;
                aux[i]=Vetor[i];
            }
        }
    }
    printf("existem %d números iguais\n",count);
    printf("os números que aparecem repetidos são:\n");
    for(i=0;i<N;i++){
        if(aux[i]!=0)
            printf("%d\n",aux[i]);
    }
    return 0;
}
    
asked by anonymous 23.09.2018 / 00:52

2 answers

1

Well a possible solution is to use Boolean to solve the problem, just include at the beginning of the code #include <stdbool.h> and declare a bool variable, to check whether or not there already exists inside the same number, if it exists it will not add again to the aux, thus avoiding the repeated numbers in the aux vector, here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 10
int main (){
    int i,Vetor[N],aux[N]={0,0,0,0,0,0,0,0,0,0},count=0,j;
    bool repete;//Variável que verifica se existe ou não o número no vetor aux
    for(i=0;i<N;i++){
        printf("insira o %dº número\n",i+1);
        scanf("%d",&Vetor[i]);
    }
    for(i=0;i<N;i++){
        for(j=i+1;j<N;j++){
            if(Vetor[i]==Vetor[j]){
                repete=false;//Definir inicialmente como falso
                for(int k=0; k<i; k++)//Pecorre o vetor aux
                {
                    if(aux[k]==Vetor[i]){//Se já existir fica verdadeiro
                       repete=true;
                    }
                }
                if(!repete){//Se nao existir no vetor ele é adicionado
                    aux[i]=Vetor[i];
                }
            }
        }
    }
    for(int l=0; l<N; l++)//Verifica quantos números repetidos existem no vetor
        if(aux[l]!=0)
            count++;
    printf("existem %d números iguais\n",count);
    printf("os números que aparecem repetidos são:\n");
    for(i=0;i<N;i++){
        if(aux[i]!=0)
            printf("%d\n",aux[i]);
    }
    return 0;
}

I could not be sure if you were looking for the total number of repetitions of numbers or how many numbers were repeated, so I did the last case.

    
23.09.2018 / 03:01
1

There is a logic error in your algorithm, when it goes through this

for(i=0;i<N;i++){
    for(j=i+1;j<N;j++){
        if(Vetor[i]==Vetor[j]){
            count++;
            aux[i]=Vetor[i];
        }
    }
}

What happens is that it will check if the number list you have put, there are actually repeated numbers, but what you might not have noticed is that if there is more than one repetition?

Imagine the following situation:

2 4 8 2 9 2 0 1 3 2

Notice that number 2 has been repeated 4 times.

When your code evaluates these 10 numbers it will identify the following answer, considering the return of if as true (1) or false (0)

2 4 8 2 9 2 0 1 3 2
- 0 0 1 0 1 0 0 0 1

Then he identified 3 repetitions of the number 2, added it to the aux vector and proceeded in the code, but shortly after you will go through 2 again, when the code reaches the 4 digit, it will evaluate again

2 4 8 2 9 2 0 1 3 2
      - 0 1 0 0 0 1

The question now is that 2 has already been analyzed, there would be no reason to re-count it, and that is probably the reason for printing the same number more than once at the end.

What's missing is to make an exception to skip over the numbers that are already repeated, something like:

int rep = 0, k;
for(i=0;i<N;i++){
    for(j=i+1;j<N;j++){
        if(Vetor[i]==Vetor[j]){
            // Faz uma busca no aux para ver se esse numero ja foi analisado
            for(k=0;k<N;k++){
                if(aux[k]!=0 && aux[k]==Vetor[i]){
                    // Salva nessa variavel se esse numero ja foi usado
                    rep = 1
                }
            }
            // Se nao foi usado, salva normalmente
            if(rep==0){
                count++;
                aux[i]=Vetor[i];
            }
        }
    }
}

I hope to have helped, any questions ask, hug!

    
23.09.2018 / 03:19