Skip to main content
I have edited my code. Hope this helps your problem. Just initialized **total_passenger** variable and inside the **for** loop added the new value with the previous one.
Source Link
Raju Ahmed
  • 354
  • 1
  • 6
  • 18

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem.

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger;total_passenger=0; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_ontotal_passenger=total_passenger+get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem.

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem.

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger=0; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=total_passenger+get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 
added 110 characters in body
Source Link
Raju Ahmed
  • 354
  • 1
  • 6
  • 18

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem. 

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem. Alongside you will have to check corner cases as your highest_total variable may provide garbage value.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem. 

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; } 
Source Link
Raju Ahmed
  • 354
  • 1
  • 6
  • 18

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem. Alongside you will have to check corner cases as your highest_total variable may provide garbage value.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring> #include<algorithm> using namespace std; int main() { int turn,get_off,get_on,total_passenger; cin>>turn; int highest_total=-1; for(int i=0;i<turn;i++){ cin>>get_off>>get_on; total_passenger=get_on-get_off; if(total_passenger>highest_total) highest_total=total_passenger; } cout<<highest_total<<endl; return 0; }