Reset repeated values of a matrix

Asked

Viewed 154 times

3

Hey there, guys. I have an exercise where I must populate a matrix with random numbers from 0 to 9 and then replace the repeated numbers (except the first time the number appears) with the number 0.
The first part I managed to do, but I’m having difficulty zeroing the repeated values. Follow the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    main ()
   {
      int N, random, coluna, linha, temp, l, k;
      printf("Insira o tamanho da matriz\n");
      scanf("%d", &N);
      int matriz[N][N];
      srand(time(NULL));

    // Populei a matriz com números aleatórios  
     for (coluna = 0 ; coluna < N ; coluna++){
       for(linha = 0 ; linha < N ; linha++){
          random = rand () % 10;
          matriz[coluna][linha] = random;
          printf("%d ", matriz[coluna][linha]);
       }
     printf("\n");
     }
     printf("\n\n\n");

    // A maneira que pensei para comparar a matriz não deu muito certo...
    for (coluna = 0 ; coluna < N ; coluna++){
      for (linha = 0 ; linha < N ; linha++){
        for (k = coluna ; k < N ; k++){
          for (l = 1 ; l < N ; l++){
            if (matriz[coluna][linha] == matriz[k][l]){
                matriz[coluna][linha] = 0;
            }
          }
        }
      }
    }


   for (coluna = 0 ; coluna < N ; coluna++){
      for(linha = 0 ; linha < N ; linha++){
          printf("%d ", matriz[coluna][linha]);
      }
    printf("\n");
   }

}

1 answer

1

The secret is to create a vector of integers with 10 positions int cont[ 10 ], each position shall be an independent counter for each number (from 0 to 9).

Follow the solution tested and commented:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define MAX_COLS     (16)
#define MAX_LINS     (16)

int main( int argc, char * argv[] )
{
    int i = 0;
    int cl = 0;
    int ln = 0;
    int matriz[ MAX_COLS ][ MAX_LINS ];
    int cont[ 10 ];

    /* Utiliza a data/hora atual como semente */
    srand(time(NULL));

    /* Inicializa a matriz de contagem */
    for( i = 0; i < 10; i++ )
        cont[i] = 0;

    /* Populando matriz com numeros aleatorios */
    for( cl = 0; cl < MAX_COLS; cl++ )
        for( ln = 0; ln < MAX_LINS; ln++ )
            matriz[cl][ln] = rand() % 10;

    /* Exibe matriz gerada aleatoriamente */
    for( cl = 0; cl < MAX_COLS; cl++ )
    {
        for( ln = 0; ln < MAX_LINS; ln++ )
            printf( "%d ", matriz[cl][ln] );

        printf("\n");
    }

    printf("\n");

    /* Conta as repeticoes de cada numero na matriz e substitui por zero */
    for( cl = 0; cl < MAX_COLS; cl++ )
        for( ln = 0; ln < MAX_LINS; ln++ )
            if( cont[ matriz[cl][ln] ]++ )
                matriz[cl][ln] = 0;

    /* Exibe matriz gerada modificada */
    for( cl = 0; cl < MAX_COLS; cl++ )
    {
        for( ln = 0; ln < MAX_LINS; ln++ )
            printf( "%d ", matriz[cl][ln] );

        printf("\n");
    }

    printf("\n");

    return 0;
}

/* fim-de-arquivo */

Exit:

8 4 2 3 0 6 2 0 7 0 7 9 4 7 9 2
6 7 2 6 3 4 5 2 2 5 7 2 1 8 9 8
3 5 6 7 3 4 0 6 7 7 4 1 7 6 5 4
6 7 2 3 1 8 9 1 7 6 5 9 4 9 0 9
9 9 0 5 1 3 4 2 2 5 4 7 8 2 6 1
4 1 5 5 2 7 0 2 4 4 6 8 4 3 2 3
8 1 6 1 8 0 1 1 3 7 1 5 5 7 6 9
4 2 4 5 6 5 2 7 8 8 7 8 3 4 2 4
7 8 1 8 4 4 8 1 9 4 2 7 7 2 2 0
0 3 8 0 1 6 8 2 8 4 0 9 5 5 3 0
9 0 1 6 9 3 0 2 1 7 2 9 4 8 5 6
3 0 2 9 4 9 4 4 2 9 0 1 5 3 9 8
2 3 3 6 5 8 3 3 4 2 7 3 6 2 1 3
9 1 9 6 9 3 0 2 4 6 8 4 9 2 7 5
9 1 4 4 1 8 1 5 3 7 8 8 8 8 6 7
8 9 7 8 4 3 2 1 4 7 2 7 5 0 4 3

8 4 2 3 0 6 0 0 7 0 0 9 0 0 0 0
0 0 0 0 0 0 5 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I hope it’s useful!

  • This code will also be faster than four for one inside the other.

  • Hi! I understand much of the code, except lines 40-44, which is the part that changes the number by 0. If the counter at [cl][ln] position of the matrix increases, the matrix [cl][ln] turns 0... But why the counter increases?

  • @1) The condition of the if is se o contador for diferente de zero; 2) The meter shall be incremented only after the evaluation of the if.

  • @This is an equivalent code: if( ++cont[ matriz[cl][ln] ] > 1 ) matriz[cl][ln] = 0;

  • @Lacobus I think I now understand. If the counter is greater than 1, it Zera the matrix. That’s it?

  • Exactly @Dervel! stay tuned for the increment operator ++, when it is using before the variable (ex.: ++i;), the increment happens before the evaluation of the expression (pre-increment), when it is used after the variable (ex.: i++;), the increment happens after the evaluation of the expression (post-increment).

  • @Dervel "Simplicity is the ultimate sophistication." Leonado da Vinci

  • @Lacobus I’m trying to do it in a more simplified (or, ugly, how to prefer hehe) way. Can I increment the cont before the if? I tried that way, but it wasn’t... for( cl = 0; cl < MAX_COLS; cl++ )&#xA; for( ln = 0; ln < MAX_LINS; ln++ )&#xA; cont[matriz[cl][ln]]++;&#xA; if(cont[matriz[cl][ln]] > 1 ){&#xA; matriz[cl][ln] = 0;&#xA; }

  • @Dervel The keys are missing: for( cl = 0; cl < MAX_COLS; cl++ ) { for( ln = 0; ln < MAX_LINS; ln++ ) { cont[ matriz[cl][ln] ]++; if( cont[ matriz[cl][ln] ] > 1 ) { matriz[cl][ln] = 0; } } }

Show 4 more comments

Browser other questions tagged

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