Thread: Segementation fault help needed.

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Segementation fault help needed.

    I was trying to solve this problem when my code would pass for most of the test cases but throw segmentation error for the rest.

    Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operations. Each operation is described by 3 integers a, b and k. Here, a and b are indices of the jars, and k is the number of candies to be added inside each jar whose index lies between a and b (both inclusive). Can you tell the average number of candies after M operations?

    Input Format
    The first line contains two integers N and M separated by a single space.
    M lines follow. Each of the M lines contain three integers a, b and k separated by single space.

    Output Format
    A single line containing the average number of candies across N jars, rounded down to the nearest integer.

    Note
    Rounded down means finding the greatest integer which is less than or equal to given number. Eg, 13.65 and 13.23 are rounded down to 13, while 12.98 is rounded down to 12.
    Constraints
    3 <= N <= 107
    1 <= M <= 105
    1 <= a <= b <= N
    0 <= k <= 106

    Sample Input #00
    5 3
    1 2 100
    2 5 100
    3 4 100
    Sample Output #00
    160
    Explanation
    Initially each of the jar contains 0 candies

    0 0 0 0 0
    First operation
    100 100 0 0 0
    Second operation
    100 200 100 100 100
    Third operation
    100 200 200 200 100
    Total = 800, Average = 800/5 = 160



    I am attaching the code here as well.
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        unsigned long long int n,m;
        int loop;
        scanf("%llu %llu",&n,&m);
        int a[m],b[m],k[m];
        int i=0;
        int final[n];
        unsigned long long int sum =0;
        for(i=0;i<n;i++)
        {
            final[i]=0;
        }
        for(i=0;i<m;i++)
        {
            scanf(" %d %d %d",&a[i],&b[i],&k[i]);
        }
        for (i=0;i<m;i++)
            {
                int j;
                for(j=a[i]-1;j<=b[i]-1;j++)
                {    
                    final[j] += k[i];
                }
            }
            for(loop=0;loop<n;loop++)
                {
                    sum += final[loop];
                }
            printf("%lld\n",sum/n);
            
        return 0;
    }
    The reason why I put everything as unsigned long long int's is because I though that it might resolve the segmentation fault error.

    I am wondering as to why it shows such a problem. Any pointers on how to solve the same would be greatly appreciated

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Do you know which line generates the segmentation fault?

    If you don't, then I suggest you start there.

    The best way to find out is to use your debugger. You don't say which platform you are on, but if it is Windows, then your IDE should have one built in for you. On Linux you'll probably have to resort to gdb.

    You can also use printf to see how far you get before you get the segmentation fault. This is "the poor mans" approach (or the lazy approach in some sense).

    If you are serious about learning C programming, then debugging these issues are something you should learn fast, because there will be a lot of them. And the debugger is a very handy tool so I suggest you learn to use it ASAP.

  3. #3
    Registered Superuser nul's Avatar
    Join Date
    Nov 2014
    Location
    Earth
    Posts
    53
    Can you provide the sample input that causes the segfault?

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Code:
        int a[m],b[m],k[m];
    Is not valid C. For that you need to allocate the memory manually.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Can you state in words why you think the value of J will not be outside the allowed range of defined values?

    Code:
                for(j=a[i]-1;j<=b[i]-1;j++)
                {   
                    final[j] += k[i];
                }
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Yarin View Post
    Code:
        int a[m],b[m],k[m];
    Is not valid C. For that you need to allocate the memory manually.
    In C99, I think it is valid.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by stahta01 View Post
    In C99, I think it is valid.
    Oh, so it is.

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    51
    I use ubuntu and then compile it using gcc. Could you suggest how to use the debugger with that? So that i can find out where the segmentation fault occours?

    The sample test cases for which it fails are huge, so here's the link

    https://hr-testcases.s3.amazonaws.co...e=text%2Fplain

  9. #9
    Registered Superuser nul's Avatar
    Join Date
    Nov 2014
    Location
    Earth
    Posts
    53
    Compile with the -g option to enable your executable for debugging.

    You should probably go through a gdb tutorial to get a hold of the different commands. GDB: The GNU Project Debugger

    As for the test cases; the first test case seems to violate the constraints you laid out in your original post (n value of 10000000 which is great than 107, m value greater than 105).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Replies: 7
    Last Post: 03-12-2013, 09:48 AM
  3. Help needed
    By adrian_ang in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2005, 02:49 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM
  5. Iserting Segementation fault in Circular List
    By yescha in forum C Programming
    Replies: 0
    Last Post: 11-28-2001, 01:07 AM