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

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    8

    Post 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;
    }
    Last edited by Salem; 09-25-2012 at 02:35 PM. Reason: restored

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Code:
     ... for(j=0;j<sqrt(n);i+=sqrt(n)) ...
    maybe here: loop with j, but incrementing i

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmmm. is this your homework profile?
    Kiss Your Weekend Goodbye - Cheezburger
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where i'm wrong ? getting a segmentation fault
    By dayanike in forum C Programming
    Replies: 2
    Last Post: 05-23-2012, 12:30 PM
  2. Replies: 7
    Last Post: 03-17-2011, 10:55 AM
  3. CS 1 Program segmentation fault but can't see the problem
    By DaNxTh3xMaNx in forum C Programming
    Replies: 1
    Last Post: 09-29-2010, 08:23 PM
  4. Can anyone show me what is wrong here? Segmentation fault
    By BigFish21 in forum C Programming
    Replies: 17
    Last Post: 11-22-2007, 01:23 AM
  5. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM

Tags for this Thread