1
I’m implementing a Sudoku, but I’m having a problem filling it.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <stdio_ext.h>
typedef struct grid_info {
int size;
int fields_total;
int* grid;
int rows_mistakes;
int column_mistakes;
int sectors_mistakes;
} grid;
void welcome_screen(grid*);
grid* fill_the_(grid*);
int main(int argc, const char* argv[]) {
grid* sudoku = (grid*)malloc(sizeof(grid));
welcome_screen(sudoku);
fill_the_(sudoku);
printf("Sudoku size: %d\n", sudoku->size);
free(sudoku);
return 0;
}
grid* fill_the_(grid* sudoku) {
sudoku->grid = malloc(sizeof(sudoku->fields_total));
for(int row = 0; row < sudoku->size; row += (sudoku->fields_total / sudoku->size)) {
for(int column = row; column = row + sudoku->size; column++) {
scanf("%d", &sudoku->grid[column]);
}
}
}
void welcome_screen(grid* sudoku) {
printf("Welcome to the sudoku game\n"
"Please enter the number of sudoku: ");
scanf("%d", &sudoku->size);
while (sudoku->size < 2 || sudoku->size >= 10) {
printf("The sudoku grid can only have size of 2 to 9\n"
"Please enter the number of sudoku: ");
scanf("%d", &sudoku->size);
}
sudoku->fields_total = pow(sudoku->size, 2);
}
When filling in the struct
, looks like he walks into a loop infinite, I don’t know if I did it right to get a index in grid within the struct
.
I tried to make an answer, but it’s a bit confusing and complex the code I advise you to use matrices to represent rows and columns. Example of an array would be matrix[Row][col], and you create two is encapsulated, one to traverse the rows and one to traverse the columns. And you have a variable within the whole type structure called grid that has the same name as the grid structure.
– Skywalker