This is by far the hardest program i decided to challenge thus far, so bear with me.
First of all, for the first row and first column i need to enter two zero's. I understand this is because the array starts with 0 and goes all the way to 8, but when i try to modify the loop to something like this:
Code:
for(i = 1; i < (COLUMNS + 1); i++)
...it doesnt work...
If you do so, consider adding -1 after i when you are accessing your array, i.e
Another problem that i found is if i overwrite the same "space" more than once, then the program ends without covering the whole field (i.e. changing ALL zero's to a user desired number)...
It does so because told it to do so... that's it, you'll be ask for ROW*COLUMN (or ROW˛) times to enter a value.. so if you "overwrite" a space, than there will be one space who's still at zero. One way to solve this is by having a better program logic . I'll give you two prototypes, you may want to implement the functions.
Code:
#include <stdio.h>
#define COLUMNS 9
#define ROWS 9
typedef enum {FALSE = 0, TRUE} Bool;
/* Defines a boolean */
void printSudoku(int sudoku[COLUMNS][ROWS]);
/* Print to the screen a ROWS by COLUMNS sudoku */
Bool hasZeros(int sudoku[COLUMNS][ROWS]);
/* Return TRUE if sudoku has at least one "space" who's
zero, else return FALSE */
int main() {
int sudoku[COLUMNS][ROWS];
int i, j, a, b;
int num;
for(i = 0; i < COLUMNS; i++) {
for(j = 0; j < ROWS; j++) {
sudoku[i][j] = 0;
}
}
while (hasZeros(sudoku) == TRUE)
{
printf("Enter column: ");
scanf("%d", &i);
printf("Enter row: ");
scanf("%d", &j);
printf("What number? ");
scanf("%d", &num);
sudoku[i][j] = num;
printSudoku(sudoku);
}
}
There's also better way of doing this, like defining a type "Sudoku", but i won't go into details... except if you ask...
Last thing, to be more "C correct", you should define your 2x2 array as
Code:
int sudoku[ROWS][COLUMNS]; // not int sudoku[COLUMNS][ROWS];