Sum In a Triangle SIGSEGV Error

Hey guys,
I have been trying to solve the sums in a triangle problem. Here’s the code:

#include<iostream>
using namespace std;
int a[10][10],j,k;
int func(int &n,int &r)
{
int large[10];
int sum=0;
for(j=1;j<r;j++)
for(k=0;k<=j;k++)
if(large[j] < a[j][k])
large[j] = a[j][k];
j=1;
while(j<r)
{
sum = sum + large[j];
++j;
}
return sum;
}
int main()
{
int s1[10];
int n,i=0,r;
cin>>n;
while(i<n)
{
cin>>r;
for(j=0;j<r;j++)
for(k=0;k<=j;k++)
cin>>a[j][k];
s1[i] = func(n,r);
++i;
}
for(i=0;i<n;i++)
cout<<s1[i]<<endl;
return 0;
}

I run this in the gcc compiler, it works(also in TurboC++), but on the codechef compiler it gives SIGSEGV error.
I know what it is but i don’t know what the mistake is in my code. Please, I need urgent help.
Thanks in advance

are you sure that u have seen the constraints of the problem properly…in your code the arrays are declared of size 10 or 10 * 10…whereas they should be 100 and 100 * 100…as the var. can range upto …hope this helps…:slight_smile:

SIGSEGV Error is caused by an invalid memory reference or segmentation fault. The most common causes are accessing an array element out of bounds, or using too much memory. Check this link for more clarity.

In your case, you are declaring array of size 10 * 10 whereas according to problem statement you must declare 100*100. So, you are accessing the array which is out of bound and that is cause of your problem.

also no need of storing all ans and then printing them together…you can directly print them…!!!