Anakinskywalker
Anakinskywalker

Reputation: 3

Error message saying Runtime Error (SEG) keeps coming up

I submitted my solution for a problem( http://opc.iarcs.org.in/index.php/problems/WORDLIST ) to the site .It is served by an online judge.Every time i submit it, it says runtime error and shows the following message :

Runtime Error: SEG Description: Segmentation fault Runtime: 0

What is this fault and how do i fix it? Note:My knowledge of c++ is intermediate and i would be grateful if you could explain it in a simple way This is my code for the problem:

#include<iostream>
#include<ctype.h>
#include<string.h>
using namespace std;
class problem
{
public:
int nol,k,j,w,cp;
char inp[1000][80],qed[1000][80];

problem()
{
k=j=w=cp=0;
}

void input() 
{
    cin>>nol;cin.get();
    for(int i=0;i<nol;i+=1)
    {   
    cin.getline(inp[i],80);
    cin.clear();
    }
    cout<<endl;
    lineprocess();
}

void lineprocess() 
{
    if(k<nol)
    wordprocess();
    else
    display();
}

void wordprocess()
{
    for(;;)
    {
        if(ch(inp[k][cp]))
        {qed[w][j]=0;break;}

        else 
        {
        qed[w][j]=tolower(inp[k][cp]);
        j+=1;cp+=1;
        }
    }
    decide();
}

void decide()
{
    if(inp[k][cp]==0)
    {
        w+=1;
        cp=0;
        j=0;
        k+=1;
        lineprocess();
    }

    else
    {
        w+=1;
        j=0;
        cp+=1;
        wordprocess();
    }
}

int ch(char a)
{
    if(!isalpha(a))
    return 1;
    else return 0;
}

void display()
{
    char dumm[80]="";
    qed[w][0]='\0';
    sortarray();
    removerep();
    for(int i=0;i<=w;i+=1)
    if(strcmp(qed[i],dumm)!=0)
    if(strcmp(qed[i],qed[i+1])!=0)
    cout<<qed[i]<<endl;
}

void sortarray()
{
    char ub[80];
     for(;checksort();)
     {
        for(int q=0;q<w;q++)
        {
            if(strcmp(qed[q],qed[q+1])>0)
            {   
                strcpy(ub,qed[q]);
                strcpy(qed[q],qed[q+1]);
                strcpy(qed[q+1],ub);
            }
        }
     }

}


int checksort()
{
    int t=0;
    for(int i=0;i<w;i+=1)
    {
        if(strcmp(qed[i],qed[i+1])>0)
        {t+=1;break;}
    }
    return t;
}

void removerep()
{
    int nodw=0;
    for(int i=0;i<w;i+=1)
    if(strcmp(qed[i],qed[i+1])!=0)
    nodw+=1;
    cout<<nodw<<endl;
}
};

int main()
{
problem r2d2;
r2d2.input();
return 0;
}

Upvotes: 0

Views: 59

Answers (1)

R Sahu
R Sahu

Reputation: 206577

From the linked page:

You may assume that N ≤ 10000

Since you are using

char inp[1000][80],qed[1000][80];

you are likely accessing memory beyond the valid range, which will cause you problems. Change them to:

char inp[10000][80],qed[10000][80];

Upvotes: 1

Related Questions