Compare value in array

I'm creating a Sudoku game, and according to the rules of Sudoku, there cannot be the same number in any row or column. My question is this: How do I compare a value with an entire row or column in an array?

For example: I have a 9x9 array that looks like this (a '0' indicates an empty space). Say I place the value '8' in the coordinates 'B2'. How would I check with the entire row '2' and the entire column 'B' to make sure that there isn't a value of '8' already there?

A B C D E F G H I
7 2 3 0 0 0 1 5 9
6 0 0 3 0 2 0 0 8
8 0 0 0 1 0 0 0 2
0 7 0 6 5 4 0 2 0
0 0 4 2 0 7 3 0 0
0 5 0 9 3 1 0 4 0
5 0 0 0 7 0 0 0 3
4 0 0 1 0 3 0 0 6
9 3 2 0 0 0 7 1 4

Much appreciated. :)
Last edited on
you know how for loops work yes? And how to use the equal to operator (==)? If so then I don't see a problem just loop through the row incrementing the column number, then loop through the column incrementing the row number, checking for equality as you go.
How's something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
         for (int col = 0; col < 9; col++)
         {
            for (int row = 0; row < 9; row++)
            {
               if (value == sudokuBoard[row][col])
               {
                  cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
                  cout << "\n";
                  getOption(sudokuBoard);
               }
            }
         }
Last edited on
After some tweaking, I've come up with this. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      {
         int currentColumn = (letter - 65);
         int currentRow = (number - 1);

         for (int i = 0; i < 9; ++i)
         {
            if (sudokuBoard[i][currentColumn] == value)
            {
               cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
               cout << "\n";
               getOption(sudokuBoard);
            }
            else if (sudokuBoard[currentRow][i] == value)
            {
               cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
               cout << "\n";
               getOption(sudokuBoard);
            }
         }
Remember that all numbers must be the distinct in each 3*3 square.
Topic archived. No new replies allowed.