Generate a matrix without repetitions

Asked

Viewed 63 times

0

I need to make a code that takes an array and puts random numbers on it, no repetitions. I am able to generate the matrix, however, it still has some repeated numbers ;-;

#include <stdio.h>
#include <time.h>
#define TAM 5


int verificar(int *matriz[TAM][TAM]){
    int c1, c2, c3, c4;

   for (c1=0; c1<TAM; c1++){
        for (c2=0; c2<TAM; c2++){
            for (c3=0; c3<c1; c3++){
                for (c4=0; c4<c2; c4++){
                    if (matriz[c1][c2] == matriz[c3][c4]){
                        matriz[c3][c4] = rand() % 99;
                        verificar(matriz);
                    }
                }
            }
        }
    }
}

int main(){
    int matriz[TAM][TAM], c2, c, temp, c1, c3, c4;

    srand(time(NULL));
    for (c=0; c<TAM; c++){
        for (c2=0; c2<TAM; c2++){
            matriz[c][c2] = rand() % 99;
            verificar(matriz);
        }
    }

    for (c=0; c<TAM; c++){
        printf("\n");
        for (c2=0; c2<TAM; c2++){
            printf("%d ", matriz[c][c2]);
        }
    }
}
  • 1

    I even thought about coming up with an answer, but in fact it’s a duplicate. Create an array of elements from 0 to 99, shuffle them randomly and then take the first 25 elements of that array to populate your matrix.

  • @Victorstafusa I myself was going to answer exactly with this solution, but I began to think that almost certainly there would be a duplicate, and quickly found a.

  • If you understand English, you can consult in detail that answer

No answers

Browser other questions tagged

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