Click here to Skip to main content
15,885,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
I use C to make sudoku ,but my program runs until first row and prints numbers from 1 to 9 successfully,but the second row is never done,because program sticks.
I checked the code twice,but I can not understand where the fault is,so I want your help to figure it out.

Output:

Initializing sudoku with zeros...


0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0



Trying to solve sudoku...


1 5 3 4 6 8 2 9 7
'and here the program sticks(for ever) I suppose...'


Code:

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int check_all_laws(int (*array)[9] , int row , int column , int number);
void initialization_array(int (*array)[9]);
int check_rows(int (*array)[9] , int column , int number);
int check_columns(int (*array)[9] , int row , int number);
int check_square_3_3(int (*array)[9] , int number);
int check_all_laws(int (*array)[9] , int row , int column , int number);
void solve_array_random(int (*array)[9]);

int main(void)
{
    srand(time(NULL));
    int array[9][9] = {{0}};
    printf("\n\nInitializing sudoku with zeros...\n\n\n");
    initialization_array(array);
    printf("\n\n\n");
    printf("Trying to solve sudoku...\n\n\n");
    solve_array_random(array);
    printf("\n\n\nSudoku is solved succesfully !\n\n\n");
}

void initialization_array(int (*array)[9])
{
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        {
            printf("%d " , array[i][j]);
        }
        printf("\n");
    }
}

void solve_array_random(int (*array)[9])
{
    int random;
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        { 
            random = (rand() % 9) + 1;
            while(check_all_laws(array , i , j , random) == 0)
            {
                random = (rand() % 9) + 1;
            }
            array[i][j] = random;

            printf("%d " , array[i][j]);
        }
        printf("\n");
    }
}

int check_rows(int (*array)[9] , int column , int number)
{
    int is_ok = 1;
    for(int i = 0; i < 9; i++)
    {
        if(array[i][column] == number)
        {
            is_ok = 0;
        }
    }
    return is_ok;
}


int check_columns(int (*array)[9] , int row , int number)
{
    int is_ok = 1;
    for(int i = 0; i < 9; i++)
    {
        if(array[row][i] == number)
        {
            is_ok = 0;
        }
    }
    return is_ok;
}


int check_square_3_3(int (*array)[9] , int number)
{
    int is_ok = 1;
    int k = 3;
    int l = 0;
    int m = 0;
    int n = 3;

    for(int i = l; i < k; i++)
    {
        for(int j = m; j < n; j++)
        {
            if(array[i][j] == number)
            {
                is_ok = 0;
            }
        }
        k += 3;
        l += 3;
        if(k == 9)
        {
            m += 3;
            n += 3;
            l = 0;
            k = 3;
        }
    }
    return is_ok;
}

int check_all_laws(int (*array)[9] , int row , int column , int number)
{
    int all_ok = 0;
    if(check_rows(array , column , number) && check_columns(array , row , number) && check_square_3_3(array , number))
    {
        all_ok = 1;
    }
    return all_ok;
}



Thanks in advance! :)
Posted
Updated 20-Oct-20 17:25pm
v2
Comments
jeron1 20-Oct-20 18:39pm    
Sounds like a great time learn how to use a debugger, where you can step through your code and see exactly what it is doing. Visual studio has wonderful debugging capabilities.
Rick York 20-Oct-20 19:57pm    
Using the literal value of 9 everywhere is a bad idea. The use of literal values should be avoided. What if you were asked to write a sudoku game that works in hexadecimal format? or used values from 0 to 9? Yikes.

You should define a const value like GameSize to hold that number - the 9 or 10 or what ever you want to use for it. You could try your program on smaller sizes like 4 or 5 if you want to, just to get started. The point is why should it be fixed to values of 1 to 9 or 0 to 8? What about 0 to 9 (10 items) or 0-9 & A-F (16 items) or to be even weirder 0-9 & A-Z (36 items)? Since the machine solves them you should give it a shot.

BTW - one way to deal with this is to define a separate array for display characters that translates values internal to the game into values for display. This will make it easier to handle arbitrary sizes.

1 solution

Quote:
I checked the code twice,but I can not understand where the fault is,so I want your help to figure it out.

Rather simple, you code to solve_array_random fall in a trap (endless loop) when it reach a point where the next cell is impossible to fill because of previous cells.
Look at this
1 2 3 4 5 6 7 8 9
4 5 6 1 2 3 x

x is impossible to fill while respecting the rules.
Guilty is:
C++
void solve_array_random(int (*array)[9])
{
    int random;
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        { 
            random = (rand() % 9) + 1;
            while(check_all_laws(array , i , j , random) == 0)
            { // this loop never ends when a cell is impossible to fill
                random = (rand() % 9) + 1;
            }
            array[i][j] = random;

            printf("%d " , array[i][j]);
        }
        printf("\n");
    }
}

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Nick_is_asking 21-Oct-20 9:19am    
Thank you,I will try ,cause I don't understand why is this happening...
Patrice T 21-Oct-20 9:23am    
Look at my sample, at x, no number is possible bacause of rules, and in this case your code loop endlessly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900