Transform for loop into while loop

Hello!
I am trying to resolve a sudoku and i have got this code that is working well.
I would like to transform all the for loop into a while loop, I'm trying for hours now and it can't work.

Here is the original program :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>

#define SIZE 9

int matrix[9][9] = {
    {8,0,0,0,0,0,0,0,0},
    {0,0,3,6,0,0,0,0,0},
    {0,7,0,0,9,0,2,0,0},
    {0,5,0,0,0,7,0,0,0},
    {0,0,0,0,4,5,7,0,0},
    {0,0,0,1,0,0,0,3,0},
    {0,0,1,0,0,0,0,6,8},
    {0,0,8,5,0,0,0,1,0},
    {0,9,0,0,0,0,4,0,0}
};

void print_sudoku()
{
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            printf("%d\t",matrix[i][j]);
        }
        printf("\n\n");
    }
}

int number_unassigned(int *row, int *col)
{
    int num_unassign = 0;
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            if(matrix[i][j] == 0)
            {
                *row = i;
                *col = j;
                num_unassign = 1;
                return num_unassign;
            }
        }
    }
    return num_unassign;
}
int is_safe(int n, int r, int c)
{
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        if(matrix[r][i] == n)
            return 0;
    }
    for(i=0;i<SIZE;i++)
    {
        if(matrix[i][c] == n)
            return 0;
    }
    int row_start = (r/3)*3;
    int col_start = (c/3)*3;
    for(i=row_start;i<row_start+3;i++)
    {
        for(j=col_start;j<col_start+3;j++)
        {
            if(matrix[i][j]==n)
                return 0;
        }
    }
    return 1;
}
int solve_sudoku()
{
    int row;
    int col;
    if(number_unassigned(&row, &col) == 0)
        return 1;
    int n,i;
    for(i=1;i<=SIZE;i++)
    {
        if(is_safe(i, row, col))
        {
            matrix[row][col] = i;
            if(solve_sudoku())
                return 1;
            matrix[row][col]=0;
        }
    }
    return 0;
}

int main()
{
    if (solve_sudoku())
        print_sudoku();
    else
        printf("Error\n");
    return 0;
}







and here is what I tried but that is not working :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


#include <stdio.h>
#define SIZE 9

int matrix[9][9] = {
    {8,0,0,0,0,0,0,0,0},
    {0,0,3,6,0,0,0,0,0},
    {0,7,0,0,9,0,2,0,0},
    {0,5,0,0,0,7,0,0,0},
    {0,0,0,0,4,5,7,0,0},
    {0,0,0,1,0,0,0,3,0},
    {0,0,1,0,0,0,0,6,8},
    {0,0,8,5,0,0,0,1,0},
    {0,9,0,0,0,0,4,0,0}
};

void print_sudoku()
{
    int i;
    int j;
    
    i = 0;
    while(i < SIZE)
    {
        i++;
        j = 0;
        while(j < SIZE)
        {
            j++;
            printf("%d\t",matrix[i][j]);
        }
        printf("\n\n");
    }
}

int number_unassigned(int *row, int *col)
{
    int num_unassign = 0;
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            if(matrix[i][j] == 0)
            {
                *row = i;
                *col = j;
                num_unassign = 1;
                return num_unassign;
            }
        }
    }
    return num_unassign;
}


int is_safe(int n, int r, int c)
{
    int i;
    int j;
    
    i = 0;
    while(i < SIZE)
    {
        
        if(matrix[r][i] == n)
        i++;
            return 0;
    }
    i = 0;
    while(i < SIZE)
    {
        i++;
        if(matrix[i][c] == n)
            return 0;
    }
    int row_start;
    i = row_start;
    row_start = (r/3)*3;
    
    row_start = (c/3)*3;
    
    while(i < row_start + 3)
    {
        i++;
        int col_start;
        j = col_start;
        while(j<col_start + 3)
        {
            j++;
            if(matrix[i][j]==n)
                return 0;
        }
    }
    return 1;
}  

int solve_sudoku()
{
    int row;
    int col;
    int i;
    int n;
    i = 1;
    
    if (number_unassigned(&row, &col) == 0)
        return 1;
    while (i <= SIZE)
    {
        i++;
        if (is_safe(i, row, col))
        {
            matrix[row][col] = i;
            if(solve_sudoku())
                return (1);
                
            matrix[row][col] = 0;
        }
    }
    return (0);
}


int main()
{
    if (solve_sudoku())
        print_sudoku();
    else
        printf("erreur\n");
    return 0;
}



could you help me?
Thank you very much !
Last edited on
I would like to transform all the for loop into a while loop
Why?
Topic archived. No new replies allowed.