0

I created a matrix[10][10] with random numbers

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        matrix[i][j] = rand() % 100 ;

    }   
}

But I need to use bool function for check duplicate numbers and if its same use random again.How can i do it?

RHertel
  • 23,412
  • 5
  • 38
  • 64
teknoman
  • 5
  • 1
  • 1
    You could store the elements of the matrix in a `std::vector` and use `std::find`. Take a look at https://stackoverflow.com/a/571405/ and https://stackoverflow.com/a/19874174/ – RHertel Dec 05 '17 at 13:09

2 Answers2

1

An efficient way to test for duplicates is to store the elements that have been inserted into the matrix in a std::vector and to use to std::find. This allows to check whether a newly generated random number is already included in the previously stored elements or not. If it is found, then another random number should be generated and the test repeated.

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>

bool alreadySelected(int n, int nvalues, int values[][10]) {
  std::vector<int> v(&values[0][0], &values[0][0] + nvalues );
  return (std::find(v.begin(), v.end(), n) != v.end());
}

int main() {
  int matrix[10][10];      
  for (int i = 0; i < 10; i++) {
    int n;
    bool dupe;
    for (int j = 0; j < 10; j++) {
    int nvalues = i * 10 + j;
    do {
      n = std::rand() % 100 ;
      dupe = alreadySelected( n, nvalues, matrix );
    } while ( dupe );
    matrix[i][j] = n;
    std::cout << matrix[i][j] << " ";
   }
   std::cout << "\n";
  }
}

A much simpler way to generate such a matrix would be to use std::random_shuffle.

RHertel
  • 23,412
  • 5
  • 38
  • 64
-1

There are multiple ways to achieve this.

  1. Write a function which returns bool and takes 10*10 matrix size. Compute sum of all numbers. Compare this result with the sum of numbers from 1...99. If both matches then no duplicate return true, otherwise duplicate return false. Sum of 1..99 can be calculated using n(n+1)/2, where n = 99.
  2. In function create array of size 100. Initialize all array elements with 0. Iterate over matrix, use matrix element as index of array. If array contains 1 at that position you got duplicate element otherwise make array element at that index 0.

Implementation of first approach

#include <iostream>
#include <cstdlib>
#include <iomanip>

#define ROW 10
#define COL 10
#define MOD 100

int main()
{
 int mat[ROW][COL];
 int sum = 0;
 int range_sum = ((MOD-1)*(MOD))/2; // n = MOD-1, sum = n(n+1)/2
 while(true){
    sum = 0;
    for(int i = 0; i < ROW; i++){
        for(int j = 0; j < COL; j++){
            mat[i][j] = rand()%MOD;
            sum += mat[i][j];
        }
    }
    if(sum==range_sum){
        break;
    }

 }

 for(int i = 0; i < ROW; i++){
    for(int j = 0; j < COL; j++){
        std::cout << std::setw(2) << mat[i][j] << "  ";
        }
        std::cout << std::endl;
    }

 return 0;
}
  • A comparison of sums as described in point 1 is not sufficient. A matrix can have several duplicates and yield the same sum as the ordered set. – RHertel Dec 04 '17 at 15:36
  • @RHertel That's what user want to detect. If there are duplicate numbers then repeat the process. Performance wise this is worst implementation I agree. – Mahendra Garodi Dec 06 '17 at 06:15
  • @MahendraGarodi The point is that the test `sum==range` is not suitable to decide whether there are duplicates or not. A vector {2,2,2} has the same sum as the vector {1,2,3}. The one has duplicates, the other not. Your code does not work. – RHertel Dec 06 '17 at 07:42