0

I'm having a problem in submitting a problem named "Money Transformation" on Popular coding site codechef..I get runtime error name SIG SEGV every time I submit the solution..after some search on google I found this is some kind of segmentation fault..I tried all the possible solutions given in Codechef's FAQ but could not get it correct..Also this error must be compiler specific I'm using CodeBlock(12.1) on my machine with its minigw compiler and my program is running perfectly fine..Please Help me understand the problem and solve it.. Below is my program (after I edited it as per codechef's faq's solution)

#include <iostream>

using namespace std;
int i=0;
class money
{
  public:
    int a[101],b[101];
    money()
    {
        for(int t=0;t<102;t++)
        {
            a[101]=0;
            b[101]=0;
        }
    }
};
money m;
void swp(int &a,int &b)
{
    int temp=0;
    temp=a;
    a=b;
    b=temp;
}
int maximum()
{
    int x=0;
    m.a[100]=m.a[0];
    m.b[100]=m.b[0];
    for(int j=1;j<=(i+1);j++)
    {
        if(m.a[j]>m.a[100])
       {
            m.a[100]=m.a[j];
            m.b[100]=m.b[j];
            x=j;
       }
        if(m.a[j]==m.a[100])
       {
            if(m.b[i]>m.b[100])
            {
                m.a[100]=m.a[j];
                m.b[100]=m.b[j];
                x=j;
            }

        }
    }
    return x;
}
void moneytransform(int a,int b,int c)
{
    m.a[0]=a;
    m.b[0]=b;
    while(true)
    {
         if(c>m.b[i]&&m.a[i]>0)
        {
            m.a[i+1]=m.a[i]-1;
            m.b[i+1]=m.b[i]+100-c;
        }
        else
        {
            m.a[i+1]=m.a[i];
            m.b[i+1]=m.b[i]-c;
        }
        swp(m.a[i+1],m.b[i+1]);
        if((m.b[i]<c&&m.a[i]==0)||(m.a[i+1]>100||m.b[i+1]>100))
        {
                return;
        }
        i++;
    }

}

int main()
{
    int T,A,B,C,M;
    do
    {
       cin>>T;
     }while(T>40);
     int noc=1;
     while(noc<=T)
    {
        do
    {
        cin>>A>>B>>C;
    }while(A<0&&B<0&&C<0&&A>100&&B>100&&C>100);
    moneytransform(A,B,C);
    M=maximum();
    cout<<M;
    noc++;
    }
    return 0;
 }

and the link to the codechef problem is..http://www.codechef.com/problems/MONTRANS/

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
jerrytom
  • 17
  • 2
  • You need a debugger to find out what's broken (In your case it's most probably an invalid access). Google "gdb". [This](http://vimeo.com/9555732) also looks helpful. – axiom Feb 01 '14 at 07:10

1 Answers1

2

In your money constructor you have

money()
{
    for(int t=0;t<102;t++)
    {
        a[101]=0;
        b[101]=0;
    }
}

You access a[101], which is outside of the bounds of the array. The array is of size 101, so the valid indexes are 0-100. Regardless, you probably meant to do something else, there's no need to write a 0 to the same place over and over again.

Your error may come from a different place entirely, but this mistake stands out. Like axiom pointed out, you should run your program in a debugger.

Vadim
  • 2,847
  • 15
  • 18
  • And maybe OP also needs to know "What can cause segmentation faults in C++?" - http://stackoverflow.com/questions/6923574/what-can-cause-segmentation-faults-in-c (where accessing out of bounds array is one of the possible causes). – SChepurin Feb 01 '14 at 08:41
  • I ran it in dubugger also.. But it reported no error.. I ran it in debugger integrated be default in codeblock 12.1.. – jerrytom Feb 03 '14 at 08:23
  • Also the mistake you pointed out it's typo.. Actually instead of "101" i was to write "t".. Sorry my bad.. – jerrytom Feb 03 '14 at 08:32
  • So, it crashes if you run it but runs OK via a debugger? – Vadim Feb 03 '14 at 10:23