I wrote the following programme which will store the occurrence of every letter of a string in an array and upon seeing a letter repeated , it will take the data from previous element of array.
Segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access(eg: reading array elements over its limit )
Look at the constraints on inputs and then typecast the variables according to contraints.
It’s not segmentation fault seg fault it SIGSEGV
This one is when your code gets suspended due to illegal operation like decrementing an unsigned variable beyond zero, etc
You messed Up ig.
In line 19: for(k=0;k<d;i++)
It works fine with --> for(k=0;k<d;k++)
In your code :
variable k is not updated in the for loop.
Also variable i is updated so it goes greater than d and in line 30 of your code it goes out of bound.
19:15: warning: variables 'k' and 'd' used in loop condition not
modified in loop body [-Wfor-loop-analysis]
for (k = 0; k < d; i++)
#include<stdio.h>
int main()
{
long int testcases ;
scanf("%li",&testcases);
long long int n,k,i=0;
//scanf("%llu %llu “,&n,&k);
while(testcases–)
{ int flag = 1;
scanf(”%llu %llu “,&n,&k);
long long int sum=0,a;
for( i=0 ;i<n;i++)
{
scanf(”%lld",&a);
sum+=a-k;
//printf(" %lld “,sum);
if(sum<0)
{
sum = i+1;
break;
}
}
if(flag=1)
printf(”%lld",(n+a/k));
else
printf("%lld",sum);
}
}
this is my code for the problem CHEFEZQ
This worked for me ,in my compiler but when i compiled it in codechef it came run time error
I am facing the same error. I cannot find where this runtime error is coming. It would be great if someone could pinpoint the error.
Link to my code: Code