# segmentation fault in C program for finding if sudoku is wrong or right

• 09-21-2012
ShubhamDepp
segmentation fault in C program for finding if sudoku is wrong or right
#include<stdio.h>
#include<math.h>
int main()
{
int t,count=1,i,j,k,n,z,l,p;
int a[100][100];
scanf("%d",&t);
for(count=1;count<=t;count++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{
z=0;k=0;
for(j=0;j<n;j++)
z=z+a[i][j];
if(z==(n*(n-1))/2)
k=1;
}
for(j=0;j<n;j++)
{
z=0;l=0;
for(i=0;i<n;i++)
z=z+a[i][j];
if(z==(n*(n-1))/2)
l=1;
}
for(i=0;i<sqrt(n);i+=sqrt(n))
{
z=0;
for(j=0;j<sqrt(n);i+=sqrt(n))
z=z+a[i][j];
if(z==(n*(n-1))/2)
p=1;
}
if(k==1&&l==1&&p==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
• 09-21-2012
qny
Code:

` ... for(j=0;j<sqrt(n);i+=sqrt(n)) ...`
maybe here: loop with j, but incrementing i
• 09-21-2012
Salem
Learn how to use a debugger.
Code:

```\$ gcc -g foo.c -lm \$ ./a.out 1 3 1 2 3 4 5 6 7 8 9 Segmentation fault \$ gdb ./a.out GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.  Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: <http://bugs.launchpad.net/gdb-linaro/>... Reading symbols from /home/sc/Documents/a.out...done. (gdb) run Starting program: /home/sc/Documents/a.out 1 3 1 2 3 4 5 6 7 8 9 Program received signal SIGSEGV, Segmentation fault. 0x0000000000400831 in main () at foo.c:34 34                                        z=z+a[i][j]; (gdb) print z \$1 = -725259189 (gdb) print i \$2 = 110 (gdb) print j \$3 = 0 (gdb)```
Mmm, i = 110
That doesn't look like a valid subscript.
• 09-21-2012
Salem
Mmmm. is this your homework profile?
Kiss Your Weekend Goodbye - Cheezburger