0

I am creating a program to solve sudoku which contains a struct for each cell that contains the value of the cell and all possible values for the cell as an int with each bit corresponding to a possible value.

To update every cell I have a function called applyMask which removes the bit corresponding to the number of the cell from all other cells that are affected by this cell. This function works properly in testing however when I loop through all cells a large number is passed instead of the correct number. For example, it will pass x is 0 and y is 2 properly but then instead of passing x is 0 and y is 3 next it will pass x is 4199111 and y is 1. In gdb, the function is passed x is 0 and y is 3 however upon stepping into the function it says that x is 4199111 and y is 1. The function called:

typedef struct Cell
{   int values;
    int value;
} cell;

void getSection(int pos, int *section1, int *section2)
{   switch(pos % 3){
    case 0:
        *section1 = pos + 1;
        *section2 = pos + 2;
    case 1:
        *section1 = pos - 1;
        *section2 = pos + 1;
    case 2:
        *section1 = pos - 2;
        *section2 = pos - 1;
    }
}

void applyMask(cell sudokuBoard[9][9], int x, int y)
{   int mask = ~(1<<(sudokuBoard[x][y].value-1));

    for(int maskP = 0; maskP < 9; maskP++)
    {   sudokuBoard[maskP][y].values &= mask;
        sudokuBoard[x][maskP].values &= mask;
    }

    int sectionX1;
    int sectionX2;
    getSection(x, &sectionX1, &sectionX2);
    int sectionY1;
    int sectionY2;
    getSection(y, &sectionY1, &sectionY2);

    sudokuBoard[sectionX1][sectionY1].values &= mask;
    sudokuBoard[sectionX1][sectionY2].values &= mask;
    sudokuBoard[sectionX2][sectionY1].values &= mask;
    sudokuBoard[sectionX2][sectionY2].values &= mask;
}

And is called by

for(int i = 0; i < 9; i++)
    for(int j = 0; j < 9; j++)
        applyMask(sudokuBoard, i, j);
Joe
  • 13
  • 1

2 Answers2

0

Are you familiar with the keyword break and its use in the case parts of a switch()?

This:

switch(pos % 3){
 case 0:
     *section1 = pos + 1;
     *section2 = pos + 2;
 case 1:
     *section1 = pos - 1;
     *section2 = pos + 1;
 case 2:
     *section1 = pos - 2;
     *section2 = pos - 1;
 }

will always execute all the case:s code, since they don't have breaks in them. This is known as "fall-through" behavior, and can be very handy when intended.

You should add breaks, so it looks like this:

switch(pos % 3){
case 0:
    *section1 = pos + 1;
    *section2 = pos + 2;
    break;
case 1:
    *section1 = pos - 1;
    *section2 = pos + 1;
    break;
case 2:
    *section1 = pos - 2;
    *section2 = pos - 1;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
0

Add this line to the end of getSection and you'll see that the missing breaks are causing *section1 and *section2 to go negative:

printf("pos: %d, *sec1: %d, *sec2: %d\n", pos, *section1, *section2);

Here's the beginning of these outputs:

pos: 0, *sec1: -2, *sec2: -1
pos: 0, *sec1: -2, *sec2: -1
pos: 0, *sec1: -2, *sec2: -1
pos: 1, *sec1: -1, *sec2: 0
pos: 0, *sec1: -2, *sec2: -1
pos: 2, *sec1: 0, *sec2: 1
pos: 0, *sec1: -2, *sec2: -1

This then makes these lines in applyMask access outside the bounds of sudokuBoard:

sudokuBoard[sectionX1][sectionY1].values &= mask;
sudokuBoard[sectionX1][sectionY2].values &= mask;
sudokuBoard[sectionX2][sectionY1].values &= mask;
sudokuBoard[sectionX2][sectionY2].values &= mask;