check 2 D array columns then rows for duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #31
    Originally posted by kylie991
    thanks Donbock I understand that part. I have even written out the sudoku grid and worked out the rows and columns I need per mini grid.. its just the code that is getting me.. I have to do the same for the minigrid as I did for the others.... I have to work out how to get it to check the squares...
    As I wrote: use my code as a cheat sheet;

    kind regards,

    Jos

    Comment

    • kylie991
      New Member
      • Feb 2009
      • 41

      #32
      :) I saw the squares calcultion.. its just a matter of checking for duplicates and getting the program to tell the computer which column/row has the duplicate..

      I'm still stuck.. arghhh...... so tired.....

      i am assuming you were referring to (3*(i/3)+j/3][val].. and thanks so much :) :)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #33
        Originally posted by kylie991
        :) I saw the squares calcultion.. its just a matter of checking for duplicates and getting the program to tell the computer which column/row has the duplicate..

        I'm still stuck.. arghhh...... so tired.....

        i am assuming you were referring to (3*(i/3)+j/3][val].. and thanks so much :) :)
        So if you have nine unsigned shorts (that store 9 bits each) for the rows, columns and the squares, you have this:

        Code:
        unsigned short rows[9];
        unsigned short cols[9];
        unsigned short squares[9];
        
        int isSet(unsigned short flags[], int n, char val) {  
           return flags[n]&(1<<val-'0') != 0;  
        }  
          
        void set(unsigned short flags[], int n, char val) {  
           flags[n]|= 1<<val-'0';  
        } 
        
        void reset(unsigned short flags[], int n, char val) {
           flags[n]&= ~(1<<val-'0');
        }
        If you want to check a row i for a value val, do this: isSet(rows, i, val);
        For a column j you do this: isSet(columns, j, val); and for a square at location (i,j) you do this: isSet(squares, (3*(i/3)+j/3, val).

        The same values are used for the other small functions.

        kind regards,

        Jos

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #34
          Originally posted by kylie991
          thanks Donbock I understand that part. I have even written out the sudoku grid and worked out the rows and columns I need per mini grid.. its just the code that is getting me.. I have to do the same for the minigrid as I did for the others.... I have to work out how to get it to check the squares...
          You're making me guess: are you having trouble writing code that computes the row and column values for each successive cell of a minigrid?

          Let's get oriented. Suppose columns count 0:8 as you move from left to right; and rows count 0:8 as you move down. Let's plan on moving through the minigrid by rows: left to right, "carraige return", etc. Finally, we will label the top-right cell as the reference cell of the minigrid -- the coordinates of the reference cell are (R,C).

          Here is a list of the cells in any and all minigrids:
          Code:
          Index    Row    Column    Index/3    Index%3
            0      R+0      C+0        0          0
            1      R+0      C+1        0          1
            2      R+0      C+2        0          2
            3      R+1      C+0        1          0
            4      R+1      C+1        1          1
            5      R+1      C+2        1          2
            6      R+2      C+0        2          0
            7      R+2      C+1        2          1
            8      R+2      C+2        2          2
          That is, the coordinates for every cell in a minigrid are (R + Index/3), (C + Index%3), where R and C are different for each minigrid.

          Notice that this could just as easily have been (R + Index%3), (C + Index/3) if we traversed the minigrid by columns.

          JosAH has given you clues for how to compute R and C from the minigrid number.

          Comment

          • kylie991
            New Member
            • Feb 2009
            • 41

            #35
            I can see what you are trying to say.. and I can mostly read your code however I think it is a bit beyond what we have learnt... I am trying to make it work even if it is a bit slow.... remember I am reading from a char data file [9] [9].

            this is how long and messy I am right now with my minigrid checker.. and it isnt even doing it right... I know I have got it wrong in parts but I am at least trying to check the first minigrid and I cant even get that...

            the equation is doing my head in...

            int checkMini(char data[NROWS][NCOLS])
            {
            for (int column=0; column<NCOLS; column++)
            {
            for (int row=0; row<NROWS; row++)
            {
            const char thisCell = data[row][column];

            for (int checkRow=row+1; checkRow<(NROWS/3); checkRow++)
            {
            for (int checkCol=column +1; checkCol<(NCOLS/3); checkCol++)
            {
            //compare this cell to the grid
            cout << "this is row data " << row << " and this is column " << column << endl;
            cout << "this is the duplicate value " << data[row][column] << endl;
            row ++;
            column++;
            if (thisCell == data[checkRow][checkCol] )
            //if (squares[3*(i/3)+j/3][val])
            {
            //if (squares[3*(row/3)]+[column/3]) //'val' is already present in this square
            cout << "Minigrid starting at row " << row << " column " << column << " is incorrect " << endl;
            return 1;
            }
            }
            }
            }
            }
            cout << "All minigrids are correct " << endl;
            }

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #36
              Stop writing code! Describe in words how to check the minigrids. You have to understand the algorithm you wish to use before you write the first line of source code.

              Comment

              • kylie991
                New Member
                • Feb 2009
                • 41

                #37
                ok.. I am getting myself confused....

                I understand I need to check the following..
                for minigrid 1
                00 01 02 10 11 12 20 21 22
                minigrid 2
                03 04 05 13 14 15 23 24 25 and so on....

                So I know that I need to come up with an array or something that can check each minigrid....

                Comment

                • kylie991
                  New Member
                  • Feb 2009
                  • 41

                  #38
                  i worked out the grids !! yaaaaaaay thanks everyone for ur help !!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #39
                    Originally posted by kylie991
                    i worked out the grids !! yaaaaaaay thanks everyone for ur help !!
                    How did you solve it? Did you use any of the existing code either in the examples here or in the existing Java code?

                    kind regards,

                    Jos

                    Comment

                    • kylie991
                      New Member
                      • Feb 2009
                      • 41

                      #40
                      ah no it is a very very long code. But what you guys mentioned did help my understanding. I just have to be careful that I dont start using codes that I have not learnt yet. We only started learning vectors last night!! They supposedly are going to replace arrays for the rest of our course this semester.. And we only really started on classes too.

                      It definately needs a lot of work but it was the only way that made any sense to me... I still need to add in some more functions to check if there are any errors in the txt file.. such as missing characters to be replaced with '1' or when the letter "A" is in a file and again need to replace it with a '1'.. but I havent even looked at that just yet... ...

                      Comment

                      • Ralfclark2009
                        New Member
                        • Mar 2009
                        • 1

                        #41
                        Please help

                        Hi Kylie,

                        Can you paste the minigrid and the extra functions you used as well?

                        I am really stuck in this part. The rest of my code sort of matches your code, but my program is not working at all... your help will be really appreciated.

                        Comment

                        Working...