Why do I get a SIGSEGV?

Why I am getting SIGSEGV.

https://www.codechef.com/viewsolution/12148465

Why am I getting a SIGSEVG for this code , CodeChef: Practical coding for everyone
Please help!

#include<stdio.h>
int main()
{
long long int t,n,i,j,m,max,k,r;
scanf("%lld",&t);
while(t–)
{
scanf("%lld",&n);
long long int a[n][n],p[n];
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
a[i][j]=0;
}
for(i=1;i<=n;i++)
scanf("%lld",&p[i]);
r=n-1;
for(i=1;i<=r;i++)
{
scanf("%lld%lld",&m,&n);
a[m][n]=1;
}
for(i=1;i<=n;i++)
{
max=0;
for(j=1;j<=n;j++)
{
if(i!=j)
{
if(a[i][j]==0 && a[j][i]==0)
{

							k=p[j];
							if(k>max)
							max=j;
						}
					}
			}
					printf("%  lld ",max);
					}	
				}

return 0;
}
where is the error

WHY DO I GET Runtime Error(SIGSEGV) FOR THIS SOLUTION OF PROBLEM:MIME2 Problem - CodeChef
#include <bits/stdc++.h>
using namespace std;
string s3,ans;
int main() {
int n,q;
cin>>n>>q;
string s1[n],s2[q];
for(int i=0;i<n;i++)
{
cin>>s1[i]>>s2[i];
}
while(q–)
{
ans=“”;
cin>>s3;
int x=s3.length();
int i=0;
while(s3[x-i-1]!=‘.’)
{
if(i==(x-1))
break;
i++;
}
if(s3[x-i-1]==‘.’)
{
while(i>0)
{
i–;
ans+=s3[x-i-1];
}
}
int flag=0;
for(int j=0;j<n;j++)
{
if(s1[j]==ans)
{
flag=1;
cout<<s2[j]<<endl;
break;
}
}
if(flag==0)
cout<<“unknown”<<endl;
}
}

@naman00b its because of condition for(i=1;i<=t;i++). Problem states that t is number of integers in number

Now, in an array, numbers are indexed from 0 to n-1. Meaning, the first number has index 0, second has index 2…and hence n has index n-1. In final iteration, it makes a reference to index n, for which array size should had been n+1. But since array is of size n, its’ maximum index is n-1. So when it calls for index n, it gives fault

(PS: Please upvote if you find the explanation correct and good :slight_smile: )

@shakku

If you can post the problem statement too, I can help. But seeing code, I suspect n is number of elements in array. Now -

for(i=1;i<=n;i++) { for(j=1;j<=n;j++) a[i][j]=0;

In every loop, you made index from 1 to n. But array has indexes from 0 to n-1. Try changing that 1 to n, to 0 to n-1.

(If my answer helps, please upvote ^^)

@bajajyukta

If you can give your submission link, I can have a better look over the code. :slight_smile:

Somebody please help me with this solution. It works on my machine.

But gives a SIGSEGV error here.

P.S. My compiler is in compliance with C99.

How to improvise in my code so as to remove SIGSEVG? Please check in this link CodeChef: Practical coding for everyone

Why am i getting sigsegv in this code
https://www.codechef.com/viewsolution/13362815
Plz help me.

why do i get sigsev for CodeChef: Practical coding for everyone
Really need help…

Please tell me why am I getting SIGSEGV error for this beginner problem of Malvika’s Fibonacci series. View my solution link text

I am Getting SIG SEVG error for this solution

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int trim(char*);

int main(int argc, char** argv) {

int test_Cases, i = 0;

char input[10];

scanf("%d", &test_Cases);

while(i < test_Cases) {
scanf("%s", input);

if(trim(input) == 1) 
puts("yes");
else
puts("no");

i++;

}

return 0;

}

int trim(char* s) {

int i =0, j = 0;

while(*(s+i) != '\0') {
	if(toupper(*(s+i)) == 'C' || toupper(*(s+i)) == 'S' || toupper(*(s+i)) == 'E') {
		*(s+j) = *(s+i);
		j++;
	}
	i++;
}
*(s+j) = '\0';


i = 0;

while(i < strlen(s) - 1) {
	if(s[i] > s[i+1])
		return 0;	
	i++;
}

return 1;

}
Can you tell me why ?

Hi. Please help me with understanding the SIGSEGV error here. Read the thread and some articles, still could not get the issue with the code.
Question: SIMDISH Problem - CodeChef
My solution: CodeChef: Practical coding for everyone
A similar accepted solution:CodeChef: Practical coding for everyone

My solution compiles, runs and gives me the correct answer in cmd compiler, but shows the run time error in codechef

https://www.codechef.com/viewsolution/13938926
why do I get Runtime Error(SIGSEGV) to this code?

https://www.codechef.com/viewsolution/14108863

can anybody pls tell,why am i getting sigsegv error here?it would be a great help.
Question : https://www.codechef.com/JUNE17/problems/CLONEMEa
THnaks

hello everyone, I have recently shifted from C to C++, can someone please tell me why I’m getting sigsegv despite, it runs fine on Dev C++ ide.
Please tell me which should I choose C++(4.9.2),(4.3.2) or 14.

https://www.codechef.com/viewsolution/14296827

Can anyone help me why I am getting sigsegv error here?
Question: https://www.codechef.com/problems/HORSES
My solution: link text

Hello… Am new to CodeChef… And tried one program in beginners… but is showing me Runtime Error(SIGSEGV)… i know that its related to program taking too much memory because of FOR loop… but i don’t know any other way to store that amount of data without FOR loop… so… plz… if Anyone can help with that… MY CODE AT: CodeChef: Practical coding for everyone

Y is this a runtime error??
#include<stdio.h>
#include<conio.h>
int main()
{
int t,n,p,i,j,e,h,s[10];
clrscr();
printf("\nEnter the no. of test cases:");
scanf("%d",&t);
for(i=0;i<t;i++)
{
e=0;
h=0;
printf("\nEnter the no.of problems and no.of people solving 'em:");
scanf("%d%d",&n,&p);
printf(“Enter the no.of people solving the resp problems:”);
for(j=0;j<n;j++)
{
scanf("%d",&s[j]);
if(s[j]>=p/2)
e++;
else if(s[j]<=p/10)
h++;
}
if(e==1&&h==2)
printf("\nYes!");
else
printf("\nNo!");
}
getch();
return 0;
}