Limiting duplicate entries in a matrix. How to make it look like a sudoku?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewCplusplus
    New Member
    • Oct 2016
    • 2

    Limiting duplicate entries in a matrix. How to make it look like a sudoku?

    Hello. I have made a square matrix of order nxn with random values in it. The entries are 1 to n appearing randomly as desired. Now i would like to limit all entries to appear strictly n times. for e.g. in the code below the output is a 9x9 matrix with entries 1 to 9. I want every number to appear only 9 times in the matrix to make it look like a sudoku. So how to do that?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    const int ROWS = 9; const int COLS = 9;   //no more magic numbers. Named type now
    typedef int Matrix[ROWS][COLS];
    
    int getrandomval();
    
    void fillrandom(Matrix mat)
    {
    	
    	for (int row = 0; row < ROWS; row++)
    	{
    		for (int col = 0; col < COLS; col++)
    		{
    			mat[row][col] = getrandomval();
    		}
    	}
    }
    
    int getrandomval()
    {
    	int x = 0;
    	x = rand() % 9 + 1;
    	return x;
    }
    
    void show(Matrix m)
    {
    	for (int row = 0; row < ROWS; row++)
    	{
    		for (int col = 0; col < COLS; col++)
    		{
    			cout << " " << m[row][col] << " ";
    		}
    		cout << endl;
    	}
    	cout << endl;
    }
    
    int main()
    {
    	srand(time(0));
    	
    	//using our constant
    	cout << "\nGenerating the random number matrix of order" << ROWS << " x " << COLS << "...\n\n\n";
    
    	Matrix mat; //declaring the variable
    	getrandomval();
    	fillrandom(mat); //filling our variable
    	show(mat); //showing whats in the matrix
    	return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Can you not put digits 1-9 in each 9 digit row of the matrix?

    After 9 rows each number will appear 9 times.

    Comment

    • NewCplusplus
      New Member
      • Oct 2016
      • 2

      #3
      That way in case of a larger size matrix (say 100) i will have to keep on punching in values and the coding would be lengthy and repetitive. This way i can substitute any value for rows and cols and get my matrix already filled with random entries. Now i just have to limit the appearance of the entries according to the order of the matrix. Once i have the matrix as desired then i can start using mathematical operations in them. Just an idea for a fun game, although i am a beginner in programming but i kept on trying and got this far.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Pick a Remaining Choice

        NewCplusplus,

        Here is a potential algorithm for selecting random numbers to meet your criteria:
        1) create an array of all the possible numbers (digits).
        1.1) call this array "numberCoun t"
        1.2) numberCount.len gth = 10 for numbers zero through 9.
        2) zero all entries in "numberCoun t".
        3) decide how many of each number will be placed into the
        matrix
        3.1) assign "numberCoun t[number]" to the "how many" count.
        4) compute totalCount as the sum of all numberCount entries.
        4.1) totalCount = numberCount[0]+numberCount[1]+...
        5) pick a number from zero to (totalCount-1)
        5.1) valueCount
        6) span the "numberCoun t" array until the running total of values exceeds valueCount
        6.1) keep the index of interest.
        6.2) decrement the "numberCoun t" element that was selected.
        7) return index of interest.

        In C++, this is my first cut at the algorithm.
        Please note that you are responsible for populating the numberCount array before using this method.
        Code:
        //--histogram of remaining numbers to place
        //--this is the initial state, user must populate
        int numberCount[10] = {0,0,0,0,0,0,0,0,0,0};
        
        // fetch a remaining numbers
        // updates numberCount[].
        int getRandomValue()
        {
          //--remaining number of numbers to place
          int totalCount = 0;
          for (auto count : numberCount)
            totalCount += count;
        
          //--which number do we pick
          int valueCount = rand() % totalCount
        
          //--which number was selected?
          //--march across array from zero to nine.
          int value;
          int incrementCount = 0;
          bool spin = true;
          for (int index; (spin  and  (10 > index)); index++)
          {
            incrementCount += numberCount[index];
            if (incrementCount > valueCount)
            { // found!
              value= index;
              numberCount[index]--;
              spin = false;
            }
          }
        
          //--done
          return value;
        }
        Good luck; I do hope that you create a new, fun game for us!
        Oralloy

        Comment

        Working...