Home > Blockchain >  How to create a Sudoku grid with the following pattern in C without arrays?
How to create a Sudoku grid with the following pattern in C without arrays?

Time:10-30

I have to create a Sudoku grid with the following pattern in C:

1 2 3 4
3 4 1 2
2 3 4 1
4 1 2 3

The first number in the top left corner (here: 1) must be an editable variable for a start value. There is another variable to create the grid with by the square size, in this example the square size is 2 and the 1 2 3 4 are in one square. 3 4 1 2 are in another square and so on...

If the start value is e.g. 3, the grid looks like this:

3 4 1 2
1 2 3 4
4 1 2 3
2 3 4 1

I noticed that there is a pattern: If the row number is odd, the new start value of the next row is the second last one. If the row number is even, the new start value of the next row is the last one. I tried to do it in C, but the even rows are cloning themselves. Note that arrays and pointers are not allowed here, only loops and other control-structures.

I tried the following approach, but the even rows are cloning themselves:

#include <stdio.h>

const int intSquareSize = 2;
const int intFieldLength = intSquareSize * intSquareSize;
int intStartValue = 3;

int main() {
    int a = 0;
    int b = 0;
    int m = 0;
    for (int intRowCounter = 1; intRowCounter <= intFieldLength; intRowCounter  ) {
        m = intFieldLength - 1;
            for (int intColumnCounter = 1; intColumnCounter <= intFieldLength; intColumnCounter  ) {
            a = intStartValue   (intColumnCounter - 1);
            b = a;
            if (a > intFieldLength) {
                a = intFieldLength - m;
                m--;
            }
            if (intRowCounter % 2 == 0 && intColumnCounter == intFieldLength) {
                intStartValue = a;
            } else if (intRowCounter % 2 == 1 && intColumnCounter == (intFieldLength - 1)) {
                intStartValue = b;
            }
            printf("%d\t", a);
        }
        printf("\n");
    }
    return 0;
}

What did I wrong and how can I fix it?

CodePudding user response:

If the row number is odd, the new start value of the next row is the second last one. If the row number is even, the new start value of the next row is the last one.

I don't think it is helpful to think in terms of odd and even. The involved numbers are just symbols, and one could even replace them with distinct colors (for example). Odd/even is not a significant thing here, and it would certainly not play the same role in other board sizes.

The pattern I see is this:

In the first intSquareSize rows, the values shift horizontally (compared to the previous row) by intSquareSize. For example with intSquareSize=3 the first three rows could be:

3   4   5   6   7   8   9   1   2
6   7   8   9   1   2   3   4   5
9   1   2   3   4   5   6   7   8

Notice the shift of 3 positions to the left at each next row.

Then the pattern for the next chunks of intSquareSize rows would be the same, but with one shift. So the complete 9x9 sudoku would look like this:

3   4   5   6   7   8   9   1   2
6   7   8   9   1   2   3   4   5
9   1   2   3   4   5   6   7   8
4   5   6   7   8   9   1   2   3
7   8   9   1   2   3   4   5   6
1   2   3   4   5   6   7   8   9
5   6   7   8   9   1   2   3   4
8   9   1   2   3   4   5   6   7
2   3   4   5   6   7   8   9   1

This is just one possible pattern you could follow. There could be others. But the following code will apply the above logic. Note I used your variables, but I prefer to use 0-based logic, so loop variables start at 0, and the value of a is also 0-based. Only at the time of printing 1 is added to that value, so it becomes 1-based:

    int a = intStartValue - 1; // Move from 1-based to 0-based
    for (int intBlockCounter = 0; intBlockCounter < intSquareSize; intBlockCounter  ) {
        for (int intRowCounter = 0; intRowCounter < intSquareSize; intRowCounter  ) {
            for (int intColumnCounter = 0; intColumnCounter < intFieldLength; intColumnCounter  ) {
                printf("%d\t", (a   1)); // back to 1-based
                a = (a   1) % intFieldLength;
            }
            printf("\n");
            a = (a   intSquareSize) % intFieldLength; // Shift within a block
        }
        a = (a   1) % intFieldLength; // Shift between blocks
    }
  • Related