noam noam - 29 days ago 6
C++ Question

how to check a matrix for duplicate numbers C++

I'm trying to generate a 5x20 matrix filled with random numbers. How can I make sure none of the random numbers are duplicates? This is the code I have for filling the matrix with random numbers.

srand(time(0));
int matrix[5][20];
int i = 0;
int j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 20; j++)
{
matrix[i][j] = 1 + (rand() % 100);
cout << matrix[i][j] <<"_";
}
cout << endl;
}


the code works but there are sometimes duplicates. If this were an array I could make use of a simple for loop and compare all of the elements in the array. but I have no idea how to do so with a matrix. I have searched everywhere but cant seem to find a solution.

Answer

as a heads up, you shouldn't use rand() unless you've got explicit reasons to (such as a professor's requirements).

The following approach uses a GetIndex function to simulate an int[5][20] with an int[100]. See if you can figure out how to use the code I wrote to create an int[5][20].

Your code fills the matrix with random numbers between 1 and 100 (1 + (rand() % 100)) inclusively, but you do no work to ensure you don't get duplicates! So you won't be able to guarantee that you don't get duplicates (in fact, it's very unusual for you to get no duplicates).

If you first initialize all your values to 1,...,100 and then later shuffle them, you know you have no duplicates.

#include <iostream>
#include <iomanip>
#include <random>
#include <algorithm>

constexpr size_t kDefaultMatrixHeight = 20;
constexpr size_t kDefaultMatrixWidth = 5;

constexpr size_t GetIndex(size_t i, size_t j) {
  return i * kDefaultMatrixWidth + j;
}

int main() {
  int matrix[kDefaultMatrixWidth * kDefaultMatrixHeight];

  for (size_t i = 0 ; i < kDefaultMatrixHeight * kDefaultMatrixWidth ; i++) {
    matrix[i] = i + 1;
  }

  std::mt19937 rng(std::random_device{}());

  std::shuffle(std::begin(matrix), std::end(matrix), rng);

  for (size_t i = 0 ; i < kDefaultMatrixHeight ; i++) {
    for (size_t j = 0; j < kDefaultMatrixWidth ; j++) {
      std::cout << std::setw(4) << matrix[GetIndex(i,j)];
    }
    std::cout << '\n';
  }
}

And for example output:

Test@Test:/tmp/example$ g++ example.cpp && ./a.out
  93  28  70  14  39
  83   3  80  95  58
  42  69  71  16  49
  75  63  41  82  46
  26  50  81  33  97
  65  10  77  68  12
   8  19  30  86  37
  57  24  78  31  88
   2  90   4  13  56
  36  15  35  32  85
  29  76  99  45  18
  54  11  44  62  98
   9  96  79  34  27
  40  21  52  22  55
  72   1  47  92  59
  94   7  64  91  53
  74   5  61 100  89
  48  23  66  67  51
  38   6  87  17  20
  60  25  84  43  73
Comments