2

I have been trying this problem on SPOJ.

I have been getting run-time error (SIGSEGV) but the code works perfectly on my computer can someone tell me what is my mistake?

#include<stdio.h>
#include<stdlib.h>
struct node
{
    struct node * next;
    int x;
};

int main()
{
    struct node *head,*temp,*temp2;
    int i,a[400],top=2;
    a[0]=2;a[1]=3;
    head=(struct node*) malloc(sizeof(struct node));
    head->x=5;
    head->next=NULL;
    temp=head;
    for(i=7;i<3000;i++)
    {
        if(i%3!=0)
        {
            temp->next=(struct node*) malloc(sizeof(struct node));
            temp=temp->next;
            temp->x=i;
            temp->next=NULL;

        }

        i++;
    }
    temp=head;

    while(head!=NULL)
    {
        temp=head;
        while(temp!=NULL)
        {

            for(i=1;i<head->x;i++)
            { 
                if(temp==NULL)
                {break;}
                else
                    temp=temp->next;

            }

            if(temp!=NULL)
            {


                temp2=temp->next;
                if(temp2!=NULL)
                {
                    temp->next=temp->next->next;
                    free(temp2);
                }
            }
        }
        a[top]=head->x;
        top++;
        temp2=head;
        head=head->next;
        free(temp2);

    }
    while(1)
    {
        scanf("%d",&i );
        if(i!=0)
            printf("%d\n",a[i-1]);
        else
            break;
    }

    return 0;
}
Natasha Dutta
  • 3,242
  • 21
  • 23
  • Are you aware that you are incrementing `i` twice in the initial loop? – Eugene Sh. May 19 '15 at 14:35
  • Where does it segfault? If you're not sure, compile with debugging symbols enabled (e.g., "gcc -g"), run "gdb ", and then inside gdb enter the command "run". When it segfaults, enter the command "where". This will tell you where the segfault occurs. – Wouter Verhelst May 19 '15 at 15:08
  • Read this first: [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and check for success of `malloc()`. – Natasha Dutta May 19 '15 at 15:11

1 Answers1

1

Your array dimensions are wrong. From the contest page:

Input Specification

The input contains several test cases. Each test case consists of an integer n. You may assume that 1<=n<=3000. A zero follows the input for the last test case.

Output Specification

For each test case specified by n output on a single line the n-th lucky number.

n is the index of the lucky number. That means that your array a, must be able to hold at least 3001 values. You have dimensioned it with 400 and don't check top for overflow. When SPOJ's test suite tests it with values greater than 400, you get undefined behaviour and quite likely a crash.

How many items must you add to the list in the first loop? Try some numbers and see when you overflow top (which you should check, even if the array is now big enough.) Then check the value of a[3001] and use that or a slightly higher value for the first loop.

You increment i twice in that loop, which is deliberate, but confusing. Consider making the intention clearer by icrementing by 2 in each step:

for (i = 7; i < 33900; i +=2)
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • my array is of size 400 as i hav only stored the lucky nos, in the array and as to the 3001 values i hav stored them in a linkedlist an deleted the unessry values in a loop and stored the lucky nos in the in the array `a[]` , and when i run this on my pc the code works perfectly wit no error. Thank you for your help im a beginer – Rikith Reddy May 20 '15 at 07:46
  • I understand what your program does. And I have explained the error you have made: 3001 is the size of the array of lucky numbers, not the loop range for fiinding them. When you have run the program on your PC, you probably haven't tested the 3000th lucky number. Or have you? – M Oehm May 20 '15 at 08:32