I am a beginner in C++ programming language. I have written a code and i'm getting a runtime error with the following error code: SIGTSTP
I have no clue what to do now. I tried to look for a solution online, but all i found was that this might happen on division by zero or trying to allocate memory blocks which are otherwise not accessible to the program. Please look into the code once. And here is the question link btw, which i was trying to solve: https://www.codechef.com/problems/TRICHEF#
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int const l=n;
int a[l];
int b[l];
while(n--)
{
cin>>a[n]>>b[n];
}
n=l;
int p=n-1;
int q=p-1;
float sum=0;
while(n--)
{
while(p--)
{
while(q--)
{
float ar=abs((a[q]*(b[p]-b[n]))+(a[p]*(b[n]-b[q]))+(a[n]*(b[q]-b[p])))/2;
sum+=ar;
}
}
}
cout<<sum<<endl;
}
return 0;
}
If you are going to nest your n, p, q loops like that then you either need to reset them individually to their maximum values before running, or ... considerably better ... use for-loops instead.
At the moment, your loop variables (and, hence, array indices) are going into negative territory (and then continuing getting more negative for ever).
Put in some debugging lines; e.g. cout << n << " " << p << " " << q << endl;
just before your float ar = ... line. This will make the problem obvious.
You will be scuppered by integer division in finding that area.
I suspect you will be heading for a time-limit exception as well.
Your lines
1 2
int a[l];
int b[l];
are also illegal in standard C++, because size l is not known until run-time.
Also for the love of God, don't use l as a variable name.
As lastchance said, your array indices goes into the negatives because you have nested while loops that operator on (n--), (p--), and (q--). This translates to while (p-- != 0), so p = -1 will return true, which probably isn't what you want.
When did this code chef thing start constantly happening?
Thankyou lastchance and Ganado for your replies. And yes I'm also getting a time-limit exception as you pointed out. I've understood my mistake with the array indices, but i still have a doubt regarding the dynamic allocation part. Like on the codechef site, they use C++14 (gcc 6.3). So will it still create a problem if i declare an array with a user-defined size, or would it create problems only on the old versions of C++ ?
If you had a completely free choice of x coordinates then your method would be fair enough. However, I think CodeChef is expecting you to note that x can only take values 1, 2 or 3 ... and find a different algorithm.
(This is a practice question, not live competition, so OK to ask for help)
the abs of a float is fabs()
and the division to make a float is 2.0
you may be doing inadvertent integer mistakes/math here that may affect your answer for whatever the heck that is doing.
prefer doubles to floats. There is no reason to use a float usually.