Error finding equal numbers

Asked

Viewed 40 times

-1

I’m trying to ask a question that if the same values are going to be counted with a single one, only I’m not getting to score as a single value.

question link

#include <stdio.h>

int main(int argc, char** argv)
{
   int a, m, i, j, cont = 0;
   while(1)
   {
      scanf("%d %d", &a, &m);
      if(a == 0 && m == 0)
        break;
      int vetor[m], verifica[m];
      for(i = 0; i < m; i++)
      {
        verifica[i] = 0;
      }
      for(i = 0; i < m; i++)
      {
        scanf("%d", &vetor[i]);
      }
      for(i = 0; i < m; i++)
      {
        for(j = 0; j < m; j++)
        {
            if(vetor[i] == vetor[j] && verifica[i] != 1 && verifica[j] != 1)
             {
                 cont++;
                 verifica[i] = 1;
                 verifica[j] = 1;
             }
         }
       }

     printf("%d\n", cont);
   }
   return 0;
} 
  • I’m not sure what you want in the check. Want to check what are the unique values in the array? You can use an auxiliary vector to store values that do not repeat.

1 answer

0

I believe this is what you would like:

int main(int argc, char** argv)
{
   int m, i, j, cont = 0;
   int vetor[100], verifica[100];

      scanf("%d", &m);
    if(m != 0) { 
        for(i = 0; i < m; i++)
            verifica[i] = 0;

        for(i = 0; i < m; i++)
            scanf("%d", &vetor[i]);

        for(i = 0; i < m; i++)
            for(j = i + 1; j < m; j++)
                if(vetor[i] == vetor[j] && verifica[j] != 1 && verifica[i] != 1) {
                    cont++;
                    verifica[i] = 1;
                    verifica[j] = 1;
                }   
     } 
     for (i = 0; i < m; i++)
        printf("%d - %d\n", vetor[i], verifica[i]);


     printf("Total: %d\n", cont);
   return 0;
}

Your biggest problems were the definition of the vector (the size in c needs to be fixed) and the variable "a" that was not used. I put in a shed to show how the vectors turned out.

Browser other questions tagged

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