diff --git a/2 b/2 new file mode 100644 index 0000000..a185cf5 --- /dev/null +++ b/2 @@ -0,0 +1,55 @@ +#include +#include +#include +#define SIZE 9 +void printMatrix(char matrix[SIZE][SIZE]) { +for (int i = 0; i < SIZE; i++) { +for (int j = 0; j < SIZE; j++) { +printf("%c ", matrix[i][j]); +} +printf("\n"); +} +} +int isUsed(int used[], int num) { +for (int i = 0; i < SIZE; i++) { +if (used[i] == num) return 1; +} +return 0; +} +void generateMatrix(char matrix[SIZE][SIZE]) { +int used[SIZE]; +int count, num; +for (int block = 0; block < 3; block++) { +for (int i = 0; i < SIZE; i++) { +used[i] = 0; +} +for (int i = block * 3; i < (block + 1) * 3; i++) { +count = 0; +while (count < 3) { +num = rand() % 9 + 1; +if (!isUsed(used, num)) { +int position; +do { +position = rand() % SIZE; +} while (matrix[i][position] != '.'); +matrix[i][position] = num + '0'; +used[count] = num; +count++; +} +} +} +} +} + +int main() { +srand(time(NULL)); +char matrix[SIZE][SIZE]; +for (int i = 0; i < SIZE; i++) { +for (int j = 0; j < SIZE; j++) { +matrix[i][j] = '.'; +} +} +generateMatrix(matrix); +printMatrix(matrix); +return 0; +} \ No newline at end of file