How to compare values within a single vector? IN C

Asked

Viewed 352 times

1

I made my code as follows, as placed below. However, if I put 2 repeated numbers it says it has only 1 repeated number, if I put 4 repeated numbers, it says it has 6 repeated numbers.I no longer know how to do this, I just wanted to compare values within a single vector.

main(){
int numeros[5],contador,numerosIguais,contador2;
numerosIguais=0;
for(contador=0; contador<5; contador++){
    printf("Digite aqui o numero n%c %i\n",248,contador+1);
    scanf("%i",&numeros[contador]);
}
    
for(contador=0; contador<5; contador++)
{
    for(contador2=contador+1; contador2<5; contador2++){
    
    if(numeros[contador]==numeros[contador2]){
        numerosIguais=numerosIguais+1;
    }
}
}
printf("A quantidade de numeros iguais e %i\n",numerosIguais);
    
    
    return 0;
}
  • 1

    What is the expected result if your input is: 1, 2, 3, 2, 1? In the case of your program it effectively only considers 1 when there is a single repetition (only increment once the variable numerosIguais) now if there are several repetitions it will increase unduly because of the first loop and all the forward that it considers in the second loop, have or have not already been considered. Take a table test but rethink your logic.

  • There’s no way I can do a table test with something I didn’t learn. That’s comparing vectors.

No answers

Browser other questions tagged

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