After a lot of trial & errors, I've managed to create an array as follow :
xxx xxx xxx
xxx 000 000
xxx 000 000
x00 000 000
x00 000 000
x00 000 000
x00 000 000
x00 000 000
x00 000 000
Now, when a user enters a value in any of the 0, I'll need to check to make sure that there isn't any repetition within the same row, column and box.
Can someone give me a basic idea on how to check the boxes and what did I do wrong with my code while checking the row & column?(The checking part is done is the void user_input( int ar[arraySize][arraySize]) function).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const int arraySize = 9;
void initarrays( int ar[arraySize][arraySize]); //to initilize the first box of the array
void scramble( int ar[arraySize][arraySize]); //to scramble the first box of the array
void initarrays_row( int ar[arraySize][arraySize]); //to initilize the first row of the array
void scramble_row( int ar[arraySize][arraySize]); //to scramble the first row of the array
void initarrays_column( int ar[arraySize][arraySize]); //to initilize the first column of the array
void scramble_col( int ar[arraySize][arraySize]); //to scramble the first column of the array
void showgrid( int ar[arraySize][arraySize]); //printing the array
void user_input( int ar[arraySize][arraySize]); //user inputting a value into a blank array
int main ()
{
srand( time( NULL ) );
int grid[9][9]={};
initarrays( grid);
printf("Starting grid:\n");
showgrid(grid);
scramble( grid);
initarrays_row( grid);
scramble_row( grid);
initarrays_column( grid);
printf("\nScrambled grid:\n");
showgrid( grid);
user_input( grid);
showgrid( grid);
}
void user_input( int ar[arraySize][arraySize])
{
int x, y, num,row,col;
int check_x;
int check_y;
int count = -1;
printf("Please enter the x axis :");
scanf("%d", &x);
printf("\nPlease enter the y axis :");
scanf("%d", &y);
printf("\nPlease enter the number :");
scanf("%d", &num);
printf("\n\nx=%d\t\ty=%d",x,y);
for(row=0; row<9; col++){
for(col=0; col<9; col++){
check_x = ar[row][y];
check_y = ar[x][col];
if(check_x == num || check_y == num)
{
count++;
}
}
if(count<=0)
{
ar[x][y]=num;
printf("\n%d\n",ar[x][y]);
break;
}
}
}
void initarrays( int ar[arraySize][arraySize]) //to initilize the first box of the array
{
int num = 1;
int row, col;
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
ar[row][col] = num;
num++;
}
}
}
void scramble( int ar[arraySize][arraySize]) //to scramble the first box of the array
{
int tmp, swprw, swpcl, row, col;
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
swprw = rand() % 3;
swpcl = rand() % 3;
tmp = ar[swprw][swpcl];
ar[swprw][swpcl] = ar[row][col];
ar[row][col] = tmp;
}
}
}
void initarrays_row( int ar[arraySize][arraySize]) //to initilize the first row of the array
{
int row, col, newcol=3;
int temp;
for (row = 1; row < 3; row++) {
for (col = 0; col < 3; col++){
temp = ar[row][col];
ar[0][newcol] = temp;
newcol++;
}
}
}
void scramble_row( int ar[arraySize][arraySize]) //to scramble the first row of the array
{
int tmp,swpcl,col;
for (col = 3; col < 9; col++) {
swpcl = rand() % 9;
if(swpcl!=0 && swpcl!=1 && swpcl!=2)
{
tmp = ar[0][swpcl];
ar[0][swpcl] = ar[0][col];
ar[0][col] = tmp;
}
}
}
void initarrays_column( int ar[arraySize][arraySize]) //to initilize the first column of the array
{
int row, col, newrow=3;
int temp;
for (row = 0; row < 3; row++) {
for (col = 1; col < 3; col++){
temp = ar[row][col];
ar[newrow][0] = temp;
newrow++;
}
}
}
void scramble_col( int ar[arraySize][arraySize]) //to scramble the first column of the array
{
int tmp, swprw, row;
for (row = 3; row < 9; row++) {
swprw = rand() % 9;
if(swprw!=0 && swprw!=1 && swprw!=2)
{
tmp = ar[swprw][0];
ar[swprw][0] = ar[row][0];
ar[row][0] = tmp;
}
}
}
void showgrid( int ar[arraySize][arraySize]) //printing the array
{
int row, col;
for (row = 0; row < 9; row++) {
for (col = 0; col < 9; col++) {
printf("%d",ar[row][col]);
if(col==8)
printf("\n");
if(col==2 || col==5)
printf(" ");
if(row==2 && col == 8 || row==5 && col == 8)
printf("\n");
}
}
}