Sudoku solver problem.

Ok when I try and run it it seg faults and I can't seem to find out where. Also if you see any places in my code that would cause a problem please let me know.

solve function: I have to use this function because that is what my teacher will call when he test it. So that is why I call solve_sudoku2, it makes more sence for me to solve it.
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
void solve_sudoku(int board[])
{
	int newBoard[9][9];
	bool writeable[9][9];
	int posx = 0, posy = 0, boardIndex;
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			newBoard[i][j] = board[boardIndex];
			if(board[boardIndex] != 0)
				writeable[i][j] = false;
			else
				writeable[i][j] = true;
			boardIndex++;
		}
	}
	solve_sudoku2(newBoard, writeable, posx, posy);
	boardIndex = 0;
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			board[boardIndex] = newBoard[i][j];
			boardIndex++;
		}
	}
}


solve_sudoku2
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
void solve_sudoku2(int newBoard[9][9], bool writeable[9][9], int posx, int posy)
{
	bool canGetInt = true;
	int number;
	for(posy; posy < 9; posy++)
	{
		for(posx; posx < 9; posx++)
		{
			if(writeable[posy][posx])
			{
				number = newBoard[posy][posx];
				canGetInt = get_int(newBoard, number, posx, posy);
			}
			if(!canGetInt)
			{
				while(!writeable[posy][posx])
				{
					newBoard[posy][posx] = 0;
					if(posx == 0)
					{
						posy--;
						posx = 8;
					}
					else
						posx--;
				}
				solve_sudoku2(newBoard, writeable, posx, posy);
			}
			else
			{
				newBoard[posy][posx] = number;
			}
		}
		posx = 0;
	}
}


get_int function:
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
bool get_int(int newBoard[9][9], int& number, int posx, int posy) 
{
	bool works = false;
	while(!works)
	{
		if(number == 9)
			return false;
		number++;
		for(int i = 0; i < 9; i++)
		{
			if(number == newBoard[posy][i])
			{
				works = false;
				break;
			}
			else
				works = true;
		}
		for(int i = 0; i < 9; i++)
		{
			if(!works)
				break;
			if(number == newBoard[i][posx])
			{
				works = false;
				break;
			}
			else
				works = true;
		}
		for(int i = (posy/3)*3; i < i+2; i++)
		{
			if(!works)
				break;
			for(int j = (posx/3)*3; j < j+2; j++)
			{
				if(number == newBoard[i][j])
				{
					works = false;
					break;
				}
				else
					works = true;
			}
		}
	}
	return true;
}


The puzzle I am trying to solve in case you need to know is:
1
2
3
4
5
6
7
8
9
0, 1, 9, 0, 0, 2, 0, 0, 0,
8, 0, 0, 6, 9, 0, 0, 0, 0,
0, 5, 7, 4, 0, 0, 0, 0, 0,
0, 0, 3, 0, 0, 0, 0, 0, 7,
0, 0, 0, 3, 2, 5, 0, 0, 0,
4, 0, 0, 0, 0, 0, 8, 0, 0,
0, 0, 0, 0, 0, 8, 4, 5, 0,
0, 0, 0, 0, 5, 3, 0, 0, 8,
0, 0, 0, 9, 0, 0, 6, 1, 0


Any help will be greatly appretiated. Thanks
Last edited on
It would help to see the rest of the code; in particular, the main function.

If you compile this with debug symbols on (under gcc, that's with the -g switch) and run under a debugger, such as gdb, it will present you with the line that cause the segfault.
Topic archived. No new replies allowed.