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 variableaux
– Fábio Morais
Much more "simple" and efficient is to sort the array using
qsort
(or a copy thereof withmemcpy
if you want to keep the original) and then use a loop normal to traverse and indicate which repeat will all be followed.– Isac