Why i am getting RE(SIGSEGV)?

#include
using namespace std;

int main() {
int test, no_s, tym[no_s-1];
cin>>test;
for(int i=0;i<test;i++){
cin>>no_s;
for (int j=0;j<no_s-1;j++){
cin>>tym[j];}
int p=tym[no_s-2];
for (int j=0;j<no_s-1;j++){
p+=tym[j];
}
cout<<p<<"\n";
}
return 0;
}

well if you see in code line 1 and 2

for (int j=0;j<no_s-1;j++){ //1
cin>>tym[j];}. //2
int p=tym[no_s-2];
//3

if your no_s-1 <2 then your p variable would be trying to access the negative index and I don’t know about the constraints so you might wanna fix that .

also I would suggest using while instead of for loop as it seems really intuitive .

cheers

1 Like

image