i have been getting SIGSEV error since last 4 to 5 submissions,its so annoying…the recent one was for the
http://www.codechef.com/problems/TLG
please help me out…thanks in advance
this is my code
#include<stdio.h>
main(){
int round;
int p1[15];
int p2[15];
int a=0;
int i=0;
int k=0;
scanf("%d",&round);
for(i=0;i<round;i++){
scanf("%d",&p1[i]);
scanf("%d",&p2[i]);
}
for(i=0;i<round;i++){
if(p1[i]-p2[i]>0){if(a<p1[i]-p2[i]) {a=p1[i]-p2[i];k=1;}}
else {if(a<p2[i]-p1[i]) a=p2[i]-p1[i];}
}
if(k==1) printf(“1 %d”,a);
else printf(“2 %d”,a);
return 0;
}
You may refer this for knowing what SIGSEGV means. Why-do-I-get-a-SIGSEGV
In this question, the limits for number of rounds in the game is given to be 10000. You are taking those 10^4 values in array p1
and p2
of size 15 which is not sufficient and so is causing this error.
Increase the array size to 10^5.If it doesn’t work then check your logic again.
Happy coding!!
#include<stdio.h>
int main()
{
int lead[2],points[2],totalpoints[2],rounds;
points[0]=points[1]=totalpoints[0]= totalpoints[1]=lead[0]=lead[1]=0;
scanf("%d",&rounds);
while(rounds>0)
{
scanf("%d %d",&points[0],&points[1]);
totalpoints[0] = totalpoints[0] + points[0];
totalpoints[1] = totalpoints[1] + points[1];
if(totalpoints[0] > totalpoints[1] && lead[0] < totalpoints[0] - totalpoints[1])
lead[0] = totalpoints[0] - totalpoints[1];
else if(totalpoints[1] > totalpoints[0] && lead[1] < totalpoints[1] - totalpoints[0])
lead[1] = totalpoints[1] - totalpoints[0];
rounds–;
}
if(lead[0] > lead[1])
printf(“1 %d”,lead[0]);
else
printf(“2 %d”,lead[1]);
return 0;
}
Please refer this:
SIGSEV errors answers here