Sudoku Checker: Accessing 3x3 subgrid in C

I've created a program that lets a user input all the values of a sudoku puzzle that is 9x9, stores those values in an array, and can check if the values in all of the rows and columns are different, but I'm having trouble understanding how i would implement code to focus on each sub grid of 3x3. I'm thinking i would have to have one last nested for loop that gets divided by maybe 3 but I'm completely stuck on this part.

int main (void) 
{
int n, i, j, x, array[9][9];
int check=0, sum;

//Enter how many puzzles user wants to solve.
scanf("%d", &n);

/*First for loop going from x to n is used so the user can enter
n amounts of puzzles and so the code doesn't try to
check n amount of puzzle inputs as one massive puzzle.*/
for(x=0;x<n;x++)
    {

    //Loop goes through all of the rows in the puzzle.
    for(i=0;i<9;i++)
        {
            //Loop that goes through all of the columns in the puzzle.
            for(j=0;j<9;j++)
            {
                scanf("%d", &array[i][j]);

            }
        }
    //Checker for loops

    for(i=0;i<9;i++){

        /*Sum initalized to 0 through every iteration so that the
        sum doesn't collect unneeded data from previous calculations
        to determine if inputs are valid. */

        sum=0;

        /*Loop that goes through all columns individually and adds them up
        totals of sum should equal 45 b/c (1+2+3+4...=45). If not check
        is changed. */

        for(j=0;j<9;j++)
            sum+=array[i][j];
        if(sum!=45){
            check=1;
            break;
            }
        }
    /* Same loop but switches the i and j in the array[i][j] to check the rows */
    for(i=0;i<9;i++){
        sum=0;
        for(j=0;j<9;j++)
            sum+=array[j][i];
        if(sum!=45){
            check=1;
            break;
            }
    }
        for (row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++){
            sum=0;
            for (col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
                sum+=array[i][j];
            if(sum!=45){
                check=1;
                break;
            }
        }


    //Check used to see if the inputs failed
    if(check==0)
        printf("YES\n");
    else
        printf("NO\n");
}
return 0;

}


Solution 1:

It will help if you break up your solution into functions handling discrete parts of the problem. Then you could write a function to handle each sub-grid. Makes the problem much more manageable. For example:

#include <stdio.h>

void process_sub_grid(int array[9][9], unsigned int start_row,
                      unsigned int start_col)
{
    unsigned int r, c;

    printf("Processing sub-array rows %d-%d, cols %d-%d\n",
           start_row, start_row + 2, start_col, start_col + 2);

    for (r = start_row; r < start_row + 3; r ++) {
        for (c = start_col; c < start_col + 3; c++) {
            printf("Accessing array[%d][%d]\n", r, c);
        }
    }
}

int main(int argc, char *argv[]) 
{
    int array[9][9];
    unsigned int start_row, start_col;

    for (start_row = 0; start_row < 9; start_row += 3) {
        for (start_col = 0; start_col < 9; start_col += 3) {
            process_sub_grid(array, start_row, start_col);
        }
    }
}

Running that program gives:

Processing sub-array rows 0-2, cols 0-2
Accessing array[0][0]
Accessing array[0][1]
Accessing array[0][2]
Accessing array[1][0]
Accessing array[1][1]
Accessing array[1][2]
Accessing array[2][0]
Accessing array[2][1]
Accessing array[2][2]
Processing sub-array rows 0-2, cols 3-5
Accessing array[0][3]
Accessing array[0][4]
Accessing array[0][5]
Accessing array[1][3]
Accessing array[1][4]
Accessing array[1][5]
Accessing array[2][3]
Accessing array[2][4]
Accessing array[2][5]
Processing sub-array rows 0-2, cols 6-8
Accessing array[0][6]
Accessing array[0][7]
Accessing array[0][8]
Accessing array[1][6]
Accessing array[1][7]
Accessing array[1][8]
Accessing array[2][6]
Accessing array[2][7]
Accessing array[2][8]
Processing sub-array rows 3-5, cols 0-2
Accessing array[3][0]
Accessing array[3][1]
Accessing array[3][2]
Accessing array[4][0]
Accessing array[4][1]
Accessing array[4][2]
Accessing array[5][0]
Accessing array[5][1]
Accessing array[5][2]
Processing sub-array rows 3-5, cols 3-5
Accessing array[3][3]
Accessing array[3][4]
Accessing array[3][5]
Accessing array[4][3]
Accessing array[4][4]
Accessing array[4][5]
Accessing array[5][3]
Accessing array[5][4]
Accessing array[5][5]
Processing sub-array rows 3-5, cols 6-8
Accessing array[3][6]
Accessing array[3][7]
Accessing array[3][8]
Accessing array[4][6]
Accessing array[4][7]
Accessing array[4][8]
Accessing array[5][6]
Accessing array[5][7]
Accessing array[5][8]
Processing sub-array rows 6-8, cols 0-2
Accessing array[6][0]
Accessing array[6][1]
Accessing array[6][2]
Accessing array[7][0]
Accessing array[7][1]
Accessing array[7][2]
Accessing array[8][0]
Accessing array[8][1]
Accessing array[8][2]
Processing sub-array rows 6-8, cols 3-5
Accessing array[6][3]
Accessing array[6][4]
Accessing array[6][5]
Accessing array[7][3]
Accessing array[7][4]
Accessing array[7][5]
Accessing array[8][3]
Accessing array[8][4]
Accessing array[8][5]
Processing sub-array rows 6-8, cols 6-8
Accessing array[6][6]
Accessing array[6][7]
Accessing array[6][8]
Accessing array[7][6]
Accessing array[7][7]
Accessing array[7][8]
Accessing array[8][6]
Accessing array[8][7]
Accessing array[8][8]