Retrieve and store elements (according to conditions) from an array
Question
Given an array of 81 elements (meant to represent a 9x9 grid) how can I go over each element, grabbing the three around it and then performing an operation on those, then continuing to the next three of each row, column, or submatrix. Look below or at a sudoku grid to see the layout.
define COL(n) ((n) % 9)
define ROW(n) ((n) / 9)
define SUB(n) ((n / 3) % 9)
For example, I have
int grid[81];
and
int array_x[9], array_y[9], array_s[9];
Since the total 9x9 grid can be split into 9 of the following categories, there are nine elements in each array, and I hope to take the elements of each column (the x axis) in groups of threes, perform
r = ((a = ~a) & (b = ~b)) | ((b | a) & ~c);
// or
r = ((~a & ~b)) | ((~b | ~a) & ~c);
on them, take the three resultant numbers, and perform it on them, and then store it into the array.
If this sounds impossible, sorry, I'd like a different way to do this. Definitely open to suggestions...
Solution
Another try:
void applyThingy(int *grid, int xPitch, int yPitch)
{
int row, column;
int *rowPointer = grid;
for(row = 0; row < 3; ++row)
{
int *columnPointer = rowPointer;
for(column = 0; column < 3; ++column)
{
doOperation(columnPointer);
columnPointer += xPitch;
}
rowPointer += yPitch * 9;
}
}
applyThingy(&grid[SUB(n)], 1, 1); // Perform on 3x3 subgrid
applyThingy(&grid[ROW(n)], 1, 0); // Perform on row
applyThingy(&grid[COL(n)], 0, 1); // Perform on column
OTHER TIPS
I'm not sure what you want to do, but is it something like this?:
#define IDX(row, col) ((row)*9+(col))
int m = some_column;
for (int n = 0; n < 9; n += 3) {
a = grid[IDX(n, m)];
b = grid[IDX(n+1, m)];
c = grid[IDX(n+2, m)];
r = ...;
...
}
Also, I'm not sure what you want to do with your operation
r = ((a = ~a) & (b = ~b)) | ((b | a) & ~c);
You're assigning ~a
to a
, is that what you want? How is a
defined, and what are you trying to set it to? What are you trying to achieve?