Given a list of numbers, stop processing input after cummulative sum of all the input becomes negative
Solution
One of the test case is giving no output
#include
using namespace std;
int main()
{
int n;
cin>>n;
int cs;
cs=n;
if(n>0)
cout<<n<<endl;
while(cs>0)
{
int t;
cin>>t;
cs=cs+t;
if(cs>0)
cout<<t<<endl;
}
return 0;
}
Simple input(cummulative sum)
You should check for cs>=0, instead of cs>0
As the question demands to stop processing input after cumulative sum of all the input becomes negative…
After using cs>=0 its giving wrong answer…as it works till cs doesnot become negative…how to stop when there is no input but cs>=0?
#include
using namespace std;
int main()
{
int n;
cin>>n;
int cs;
cs=n;
if(n>=0)
cout<<n<<endl;
while(cs>=0)
{
int t;
cin>>t;
cs=cs+t;
if(cs>=0)
cout<<t<<endl;
}
return 0;
}
Please post the actual complete problem statement alongwith the constraints…