-1
int check_row;
    for (n=0; n<9; n++) {
 used_numbers[n] = n+1;
}
for (row=0; row<3; row++) {
    for (check_row=0; check_row<3; check_row++) {
        used_numbers[(sudoku[row][check_row]-1)] = 0;
    }
...

int sudoku[9][9] declared as global variable and used_numbers[9] as int. In sudoku matrix for row from 0 to 2 and col from 0 to 2 for each row, has in it, numbers > 0

At this point I get "Floating point exception", how resolve this? sorry for my bad english...

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
genesisxyz
  • 778
  • 3
  • 14
  • 29
  • 1
    have you tried using a debugger ? – Tom Dec 15 '09 at 15:33
  • You can't get a floating point exception in that code, there don't appear to be any floating point values. Post more code and include the part containing the error (look for the line number in the error). Post the error as well. – KernelJ Dec 15 '09 at 15:36
  • 1
    You have an error on line 42. :-) – Alok Singhal Dec 15 '09 at 15:37
  • main.c http://pastebin.com/f635d1550 sudoku.h http://pastebin.com/f2bbcfc50 http://qkpic.com/55cf4 < terminal – genesisxyz Dec 15 '09 at 15:45
  • You divide by zero through variable m (integer division) on line 66: number = rand()%m; Btw: What is the intention? Generate Sudoku games? – MaR Dec 15 '09 at 16:15

2 Answers2

5

It is a very bad idea to have function/variable definitions in a header file, like you have done. Put the definitions in a C file, and the declarations in a header file for other C files to use.

Your floating-point error is on line 66 of sudoku.h, not where you think it is.

number = rand()%m;

Since m is zero here, dividing by it results in the error.

I haven't looked at the whole code in detail.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
1

@Alok has of course told you what the error is (and pointed out problems with your .h file), but I want to show you how you can find it yourself.

  1. First, you want to build with debugging. I ran: gcc -g sudoku.c -o sudoku
  2. Second, I ran it and confirmed it does indeed crash with Floating point exception.
  3. I ran gdb sudoku to start the debugger.
  4. I typed 'run', and shortly thereafter:

GDB output:

(gdb) run
Starting program: /tmp/t/sudoku 

Program received signal SIGFPE, Arithmetic exception.
0x000000000040098f in sudoku_init () at sudoku.h:66
66                              number = rand()%m;

So, we have the line number it's at. Using l more context can be printed. Using p m we can see that m==0, which would indeed be a problem.

derobert
  • 49,731
  • 15
  • 94
  • 124