0

help me this code gives runtime NZEC error.

#include <iostream> #include<vector> using namespace std; int main() { int n,q,l,m,o,sum; cin >>n; cin>>q; vector <int>v(n,0); for(int i=0;i<n;i++) { cin>>v[i]; } for(int i=0;i<q;i++) { cin>>l>>m>>o; if(l==1&&m<n) { v[m]=o; } if(l==2&&m<n&&o<n) { sum=0; for(int j=m;j<=n;j++) { sum=sum+v[j]; } cout<<sum<<endl; } else//if i remove this else statement it works fine otherwise it gives runtime error NZEC return -1; } }

2nd Jun 2018, 9:07 AM
Bahubali
Bahubali - avatar
8 ответов
4th Jun 2018, 2:19 AM
John Wells
John Wells - avatar
+ 4
Your input line of '1 0 3' for l m o in first set of values goes to the return statement. You are missing an else. Make: if(l==2&&m<n&&o<n) this: else if(l==2&&m<n&&o<n) Therefore, that set is processed by: if(l==1&&m<n) only and not the return afterwards.
4th Jun 2018, 2:13 AM
John Wells
John Wells - avatar
+ 2
Need to know your inputs to help fully. Your statement of removing the else, does that include the return? Where are you running it? A return of non-zero on some systems will cause an error message.
4th Jun 2018, 1:13 AM
John Wells
John Wells - avatar
+ 1
What inputs did you use? Did you remove the return statement?
4th Jun 2018, 1:51 AM
John Wells
John Wells - avatar
+ 1
oh yes ,thank u sir for helping me
4th Jun 2018, 2:21 AM
Bahubali
Bahubali - avatar
0
sir I am running on online website. and I realise that outer for loop is running only once but in input there are more number that causes that error.if I am wrong correct me.
4th Jun 2018, 1:35 AM
Bahubali
Bahubali - avatar
0
input 5 5 2 3 4 8 9 1 0 3 2 0 1 2 0 4 1 2 5 2 0 3
4th Jun 2018, 1:52 AM
Bahubali
Bahubali - avatar
0
yes sir I have to remove else statement.
4th Jun 2018, 1:53 AM
Bahubali
Bahubali - avatar