Thread: Removing Duplication from Matrix Rows.

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Removing Duplication from Matrix Rows.

    What I am trying to do is create a random 9x9 matrix. Ultimately working towards randomly generating a Sudoku puzzle. I am very new to array usage/manipulation so this problem is baffling me.

    If you know of Sudoku basically you can't have duplicate numbers in any rows/columns.


    My problem is figuring out how to remove duplication of the randomly generated numbers in the rows.

    Thus far, I have been able to create a randomly generated 9x9 matrix. That is about as far as I have made it though.

    I was wondering, at the very least if someone had an idea on how to remove duplication within each of the rows.

    Here is what I have so far.


    Code:
    #include <iostream>
    #include <ctime> 
    
    using namespace std;
    
    void matrixOne(int & x, int & y, int matrix[][9]);
    
    
    
    
    
    
    int main(void)																
    {
    
     int matrix[9][9];
     int x;
     int y;
    
    
     
     matrixOne(x,y,matrix);
    
    
    
    
    
    
    
     return 0;
    }
    
    
    
    void matrixOne(int & x, int & y, int matrix[][9])
    {
    
    
    
     
    
    for(x = 0; x < 9; x++)
     {
      cout << endl;
    
    
      for(y=0; y < 9; y++)
      {	   
    	 matrix[x][y] = rand()%9+1;
    	 cout << " " << matrix[x][y];
    
    
      }
    	 
    }
      cout << endl;
    
    
     return;
    }

    My output looks like this :

    Code:
    
     6 9 8 5 9 2 4 1 8
     3 9 3 8 7 8 6 8 9
     4 1 1 7 6 1 5 8 7
     6 9 6 3 1 3 1 7 5
     9 2 8 4 3 7 3 4 7
     3 4 8 3 2 6 6 2 7
     4 8 3 4 8 5 5 3 6
     7 1 2 5 6 5 5 6 1
     6 7 8 6 4 7 4 3 1
    
    Press any key to continue

    Any help or suggestions would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To get a row of the 9 digits without a repeat, I would add the numbers 1-9 to the array, and then use random_shuffle to shuffle them. You could do this for each of the 9 rows. The problem is that some of the columns and/or squares will have duplicates, even though the rows don't.

    You can search for Sudoku puzzle generation to get some ideas on algorithms that would work. Also, this thread might give you some ideas, even though you wouldn't want to use the code because it was a brevity contest: http://www.cpp-home.com/forum/viewtopic.php?t=12313

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    What I would do, is to fill the matrix by row. Go across the entire row, and then to the next one down and so on.

    At the beginning of each row, I would have 9 boolean values.

    IE bool one,two,three,...etc


    when I generated a number I would check to see if the bool is true or false. if it is false then enter the number into the position and set the bool to true. If it is false generate another number and run check again.

    At the end of the row, when moving to a new one, reset all of the bools to true.

    bool == true means that the number is avaialable
    bool == false the number has been used

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    when I generated a number I would check to see if the bool is true or false. if it is false then enter the number into the position and set the bool to true. If it is false generate another number and run check again.


    Not really sure how to implement this. I guess that is where I am running into issues. I for some reason am having trouble conceptually understanding what needs done.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM