473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int ROWS = 9; const int COLS = 9;   //no more magic numbers. Named type now
  8. typedef int Matrix[ROWS][COLS];
  9.  
  10. int getrandomval();
  11.  
  12. void fillrandom(Matrix mat)
  13. {
  14.  
  15.     for (int row = 0; row < ROWS; row++)
  16.     {
  17.         for (int col = 0; col < COLS; col++)
  18.         {
  19.             mat[row][col] = getrandomval();
  20.         }
  21.     }
  22. }
  23.  
  24. int getrandomval()
  25. {
  26.     int x = 0;
  27.     x = rand() % 9 + 1;
  28.     return x;
  29. }
  30.  
  31. void show(Matrix m)
  32. {
  33.     for (int row = 0; row < ROWS; row++)
  34.     {
  35.         for (int col = 0; col < COLS; col++)
  36.         {
  37.             cout << " " << m[row][col] << " ";
  38.         }
  39.         cout << endl;
  40.     }
  41.     cout << endl;
  42. }
  43.  
  44. int main()
  45. {
  46.     srand(time(0));
  47.  
  48.     //using our constant
  49.     cout << "\nGenerating the random number matrix of order" << ROWS << " x " << COLS << "...\n\n\n";
  50.  
  51.     Matrix mat; //declaring the variable
  52.     getrandomval();
  53.     fillrandom(mat); //filling our variable
  54.     show(mat); //showing whats in the matrix
  55.     return 0;
  56. }
  57.  
Oct 3 '16 #1

✓ answered by Oralloy

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 "numberCount"
1.2) numberCount.length = 10 for numbers zero through 9.
2) zero all entries in "numberCount".
3) decide how many of each number will be placed into the
matrix
3.1) assign "numberCount[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 "numberCount" array until the running total of values exceeds valueCount
6.1) keep the index of interest.
6.2) decrement the "numberCount" 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.
Expand|Select|Wrap|Line Numbers
  1. //--histogram of remaining numbers to place
  2. //--this is the initial state, user must populate
  3. int numberCount[10] = {0,0,0,0,0,0,0,0,0,0};
  4.  
  5. // fetch a remaining numbers
  6. // updates numberCount[].
  7. int getRandomValue()
  8. {
  9.   //--remaining number of numbers to place
  10.   int totalCount = 0;
  11.   for (auto count : numberCount)
  12.     totalCount += count;
  13.  
  14.   //--which number do we pick
  15.   int valueCount = rand() % totalCount
  16.  
  17.   //--which number was selected?
  18.   //--march across array from zero to nine.
  19.   int value;
  20.   int incrementCount = 0;
  21.   bool spin = true;
  22.   for (int index; (spin  and  (10 > index)); index++)
  23.   {
  24.     incrementCount += numberCount[index];
  25.     if (incrementCount > valueCount)
  26.     { // found!
  27.       value= index;
  28.       numberCount[index]--;
  29.       spin = false;
  30.     }
  31.   }
  32.  
  33.   //--done
  34.   return value;
  35. }
Good luck; I do hope that you create a new, fun game for us!
Oralloy

3 1197
weaknessforcats
9,208 Expert Mod 8TB
Can you not put digits 1-9 in each 9 digit row of the matrix?

After 9 rows each number will appear 9 times.
Oct 3 '16 #2
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.
Oct 3 '16 #3
Oralloy
988 Expert 512MB
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 "numberCount"
1.2) numberCount.length = 10 for numbers zero through 9.
2) zero all entries in "numberCount".
3) decide how many of each number will be placed into the
matrix
3.1) assign "numberCount[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 "numberCount" array until the running total of values exceeds valueCount
6.1) keep the index of interest.
6.2) decrement the "numberCount" 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.
Expand|Select|Wrap|Line Numbers
  1. //--histogram of remaining numbers to place
  2. //--this is the initial state, user must populate
  3. int numberCount[10] = {0,0,0,0,0,0,0,0,0,0};
  4.  
  5. // fetch a remaining numbers
  6. // updates numberCount[].
  7. int getRandomValue()
  8. {
  9.   //--remaining number of numbers to place
  10.   int totalCount = 0;
  11.   for (auto count : numberCount)
  12.     totalCount += count;
  13.  
  14.   //--which number do we pick
  15.   int valueCount = rand() % totalCount
  16.  
  17.   //--which number was selected?
  18.   //--march across array from zero to nine.
  19.   int value;
  20.   int incrementCount = 0;
  21.   bool spin = true;
  22.   for (int index; (spin  and  (10 > index)); index++)
  23.   {
  24.     incrementCount += numberCount[index];
  25.     if (incrementCount > valueCount)
  26.     { // found!
  27.       value= index;
  28.       numberCount[index]--;
  29.       spin = false;
  30.     }
  31.   }
  32.  
  33.   //--done
  34.   return value;
  35. }
Good luck; I do hope that you create a new, fun game for us!
Oralloy
Oct 3 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
4
by: MLH | last post by:
I never quite figured out how to reconfigure it to automatically delete redundant entries. Of course, one cannot always blatantly blow redundant records away w/o regard to which one it is that you...
4
by: sri2097 | last post by:
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file...
5
by: Chris Lasher | last post by:
Hello Pythonistas! I'm looking for a way to duplicate entries in a symmetrical matrix that's composed of genetic distances. For example, suppose I have a matrix like the following: A B ...
1
by: calebm12 | last post by:
Quick Question. I gotta a database with fields firstname, lastname, and hobby, etc. I dont want to allow duplicate entries for the name. For instance....no john smith twice....but there can be a...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
12
by: joestevens232 | last post by:
Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input into an array. Write...
7
by: php_mysql_beginer911 | last post by:
Hi .. i am trying to update a table where if field contents any duplictaed entries than one of the field should be updated with random number which is unique so i can make all entries unique i...
3
by: poolboi | last post by:
hey guys, i'm stuck with finding a sql statement for filtering out duplicate entry i got like thousands of entry but i would like to put a filter on so i can see all the duplicate entries ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.