Problem with Printing Repeated Numbers in C

Asked

Viewed 764 times

1

The question asked me to make a code that would store 10 integer values, check if there were any repeated numbers, and if there were, print the repeated numbers, but my code or print some repeated numbers more than once, or print 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;
}
  • Before placing on the variable aux You need to check if there’s any there, you need to create a function to verify that. The way you’re doing is putting all the numbers on the variable aux

  • Much more "simple" and efficient is to sort the array using qsort (or a copy thereof with memcpy if you want to keep the original) and then use a loop normal to traverse and indicate which repeat will all be followed.

2 answers

1

Well one possible solution is to use boolean to solve the problem, just include at the beginning of the code #include <stdbool.h> and declare a variable bool, to verify whether or not there already exists within aux the same number, if there is it will not add again to 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 didn’t really understand if you were looking for the total number of repetitions or how many numbers were repeated, so I did it for the last case.

1

There’s a logic error in your algorithm, when you go through this stretch

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 he’s going to check if on the number list you put in, there really are repeated numbers, but what you might not have noticed is that and if there’s more than one repeat?

Imagine the following situation:

2 4 8 2 9 2 0 1 3 2

Note that the number 2 repeated 4 times.

When your code is to evaluate 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 we proceeded in the code, but shortly after you will pass the 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 point now is that 2 has already been analyzed, it would have no reason to be re-counted, and probably that’s why you print the same number more than once at the end.

What remains is to make an exception to skip the numbers 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 I’ve helped, any questions ask, hug!

Browser other questions tagged

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