check 2 D array columns then rows for duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kylie991
    New Member
    • Feb 2009
    • 41

    #16
    thanks so much I am still confused as ever... I have spent hours tryign to work it out. Whenever I make an error in the grammar the compiler gets stuck saying that float list (from function display file) is not being used....

    The above doesnt seem to work for me as what I have to do is read the char array file.. we have a txt file we need to open....The task is to determine if the the file has duplicates in.
    a) columns
    b) rows
    c) minigrids

    and if there are duplicates in either one of the above we have to say for eg.
    column 8 has duplicates

    I can get the program to check the WHOLE array for duplicates. but I cannot get it to check each row or each column.. I am stuck on how to separate it...... as the data is stored in array char data [9][9]

    And everytime I try to separate it I get that stupid error saying that float list is not being used or something...... ....

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #17
      Originally posted by kylie991
      thanks so much I am still confused as ever... I have spent hours trying to work it out. Whenever I make an error in the grammar the compiler gets stuck saying that float list (from function display file) is not being used....
      <snip>
      And everytime I try to separate it I get that stupid error saying that float list is not being used or something...... ....
      OK, let's focus on that. First of all, are you sure this is an Error message? It sounds a lot more like a Warning to me.

      Please search your source code for the word "list". It only occurs once in the code you've posted here; but there may be other instances in your actual source code.

      The only place I see "list" is in function loadFile; where the line
      Code:
      float list = 0;
      occurs. The warning message appears to be correct -- a value is written into "list" but nobody ever seems to read that variable. What do you intend to be the purpose of "list"?

      Comment

      • kylie991
        New Member
        • Feb 2009
        • 41

        #18
        Oh I can remove that one - i dont need it anymore... Hopefully that will help me with my troubleshooting .
        Thanks for your patience everyone. Will let you know if I get anywhere with it :)

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #19
          I can get the program to check the WHOLE array for duplicates. but I cannot get it to check each row or each column.. I am stuck on how to separate it...... as the data is stored in array char data [9][9]
          I am focusing on the OP`s above comment.
          If you have a 2 dimensional array of 9 rows and 9 columns (int array[i][j] where i=9 and j=9 ) and you were to scan it, the 2 loops they would look like:
          for(int i=0;i<9;i++)//for rows
          for(int j=0;j<9;j++)//for columns
          {
          //the instructions would go here
          }
          i would keep track of the rows and j would keep track of the columns.
          The table would look like this:
          00 01 02 03 04 05 06 07 08 //where i is = 0 and j =0 to 8
          10 11 12 13 14 15 16 17 18 //where i is = 1 and j = 0 to 8
          20 21 22 23 24 25 26 27 28 // where i is = 2 and j = 0 to 8
          ....... and so on for the rest of the table down to:
          80 81 82 83 84 85 86 87 88
          To iterate through row#2 your loop would look like:
          for(i=1,j=0;i<2 ,j<9;j++)//i does not change from 1 but j iterates from 0 to 8
          To iterate through column 5 your loop would look like:
          for(i=0,j=4;i<9 ,j<5;i++)//j does not change from 4 but i iterates from 0 to 8
          Hope this helps.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            Are you all going to ignore my reply #12 and write crappy loops without thinking?

            kind regards,

            Jos

            Comment

            • kylie991
              New Member
              • Feb 2009
              • 41

              #21
              Hi Jos
              I had a read of your sudoku program and it looks great. I just wouldnt know how to implement the bool functions with my program. As I have to set it to read a file and then check the actual file for duplicates..... ..

              Can you help cus yours looks like a great idea.. but since I am fairly new to c++ I wouldnt know how to make it work by opening a file..

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by kylie991
                Hi Jos
                I had a read of your sudoku program and it looks great. I just wouldnt know how to implement the bool functions with my program. As I have to set it to read a file and then check the actual file for duplicates..... ..

                Can you help cus yours looks like a great idea.. but since I am fairly new to c++ I wouldnt know how to make it work by opening a file..
                You have your board itself and an arrray of bit flags for the rows and the same for the columns and the same for the subsquares:

                Code:
                int board[9][9];
                unsigned short rows[9];
                unsigned short cols[9];
                unsigned short squares[9];
                If you want to store a value > 0, say 'val' at position i (row) and j (column) you have to do the following simple checks:

                Code:
                board[i][j] == 0
                i.e. if there is a value at that position already the expression above will be false and you can't store a value there; otherwise:

                Code:
                int isSet(unsigned short flags[], int i, int val) {
                   return flags[i]&(1<<val) != 0;
                }
                The above little function checks whether the 'val'th bit is set in flags[i], so you can test if a value 'val' is already used in row i as follows:

                Code:
                isSet(rows, i, val)
                The above will be != 0 if value 'val' is already used in row i. The same goes for the columns and the subsquares. Setting a bit (that was zero) in an array is easy:

                Code:
                void set(unsigned short flags, int i, int val) {
                   flags[i]|= 1<<val;
                You can use the code in the Sudoku article as a cheat sheet and glue the above all together.

                kind regards,

                Jos

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #23
                  I don't understand this new-fangled C++ ">>" twaddle, but the fact that the OP declared the suduko board as a 2D char array makes me wonder if it contains characters '1' through '9' rather than integers 1 to 9. The equations cited above by JosAH presupposes integer values. It is a simple matter to convert characters to integers.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by donbock
                    I don't understand this new-fangled C++ ">>" twaddle, but the fact that the OP declared the suduko board as a 2D char array makes me wonder if it contains characters '1' through '9' rather than integers 1 to 9. The equations cited above by JosAH presupposes integer values. It is a simple matter to convert characters to integers.
                    I don't see any "new-fangled C++ >> twaddle" but let's settle the definitions:

                    - the board is a char board[9][9] and contains the characters '0' ... '9'.

                    this definition changes the two little functions a bit:

                    Code:
                    int isSet(unsigned short flags[], int i, char val) { 
                       return flags[i]&(1<<val-'0') != 0; 
                    } 
                    
                    void set(unsigned short flags[], int i, char val) { 
                       flags[i]|= 1<<val-'0'; 
                    }
                    kind regards,

                    Jos

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #25
                      Originally posted by JosAH
                      I don't see any "new-fangled C++ '>>' twaddle" ...
                      The OP uses the following function to load the 2D suduko array:
                      Code:
                      int loadFile(char data[9][9]) 
                      { 
                          ifstream infile; 
                          string filename; 
                          float list = 0; 
                          cout << "Enter file name " << endl; 
                          cin >> filename; 
                          infile.open(filename.c_str()); 
                          if (infile.fail()) 
                          { 
                              cout << "File not found " << endl; 
                              return EXIT_FAILURE; 
                          } 
                          for(int i=0; i<9; i++) 
                              for(int j=0; j<9; j++) 
                                  infile >> data[i][j]; 
                          return EXIT_SUCCESS; 
                      }
                      There is a lot of new-fangled C++ twaddle here that an unrepentent C curmudgeon like me only vaguely understands, but I was specifically referring to line 16. There is no blatant conversion here, so I suppose the array is loaded with characters from a text file. (Only the OP knows what the file format is.)

                      As you pointed out, converting from single-digit characters to integers is trivial.

                      If it were me, I'd do the conversion to integer in the loadFile function and modify the printFile function accordingly. That way the persistent data (in the 2D array) is in the most intuitive format [integer]. [On the other hand, what's intuitive to me might be obscure to everybody else.]

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #26
                        Originally posted by donbock
                        There is a lot of new-fangled C++ twaddle here that an unrepentent C curmudgeon like me only vaguely understands, but I was specifically referring to line 16. There is no blatant conversion here, so I suppose the array is loaded with characters from a text file. (Only the OP knows what the file format is.)
                        Ah, that piece of code ... I simply discarded it as non-interesting twaddle ;-)

                        Originally posted by donbock
                        As you pointed out, converting from single-digit characters to integers is trivial.

                        If it were me, I'd do the conversion to integer in the loadFile function and modify the printFile function accordingly. That way the persistent data (in the 2D array) is in the most intuitive format [integer]. [On the other hand, what's intuitive to me might be obscure to everybody else.]
                        Yep, but the OP wants chars in that array so we'd better stick to that convention if we don't want to raise levels of confusion. The two core functions were indeed trivial to change.

                        kind regards,

                        Jos

                        Comment

                        • kylie991
                          New Member
                          • Feb 2009
                          • 41

                          #27
                          suppose the array is loaded with characters from a text file. (Only the OP knows what the file format is.)
                          yes it is coming from a text file :) It might confuse me if we try to change it to integers. Sorry I was away for a few days so have missed your help. Am goign to work on this tonight.. I appreciate EVERYONE's HELP :) Now Jos I think I might read your code to see if that will help. Because what I have is kind of working for now but is a lot longer than it probably can be... :)

                          Would anyone know how I can check the minigrids.. arghh its doing my head in..

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #28
                            Originally posted by kylie991
                            Now Jos I think I might read your code to see if that will help. Because what I have is kind of working for now but is a lot longer than it probably can be... :)

                            Would anyone know how I can check the minigrids.. arghh its doing my head in..
                            Read my (Java) code: the mini squares are indexed by the numbers 0 ... 9 as well, just like the rows and columns; a bit of index fiddling with the row and column numbers easily finds the index number of a mini square.

                            kind regards,

                            Jos

                            Comment

                            • donbock
                              Recognized Expert Top Contributor
                              • Mar 2008
                              • 2427

                              #29
                              Originally posted by kylie991
                              Would anyone know how I can check the minigrids.. arghh its doing my head in..
                              In general, you iterate through the three types of sets (rows, columns, minigrids). Within each type of set you iterate through nine sets (for rows, its the 9 rows; etc.). Within each set you iterate through nine cells.

                              Suppose you have a cell lookup function with inputs type-of-set, set-within-that-type, and cell-within-that-set; and that returned the row and column of the cell. Then one check-for-duplicates function could be used for all three types of sets.

                              How do you implement such a lookup function? Step 1: assign an index value to each type-of-set (0:2). Step 2: assign an index value to each set within each type-of-set (0:8). Step 3: assign an index value to each cell within each set (0:8). Step 4: Examine each of the 27 sets to see how row and column vary as you step through the cells of that set. Use the results of that examination to define your lookup algorithms (you need a different algorithm for each type-of-set). Step 5: only after you have a clear idea what has to happen do you start writing code.

                              Even if you don't write a single global lookup function, the steps described above will help you.

                              Comment

                              • kylie991
                                New Member
                                • Feb 2009
                                • 41

                                #30
                                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...

                                Comment

                                Working...