Write a program that emulates the behavior of a bus. The bus must have a schedul
ID: 3554131 • Letter: W
Question
Write a program that emulates the behavior of a bus. The bus must have a schedule ( a finite number of times for it to stop) and a certain capacity (number of passengers). In this program the bus has two states: stopped and going. Over time, events take place, such as stopping the bus ( for which the bus must already be going ), loading passengers, unloading passengers, or starting the bus. For each event, the current state of the bus must change accordingly. Each time the bus stops, it must load and unload passengers. The number of passengers that will be unloaded at every bus stop must be generated randomly. The number of passengers waiting for the bus at each stop is also generated randomly. The bus should load as many waiting passengers as possible without exceeding the bus capacity. Write a program that implements this model, with the following operation:
void stop_bus(); /* Stop the bus */
void start_bus(); /* Start the bus */
void load_pass(); /* Load passengers */
void unload_pass(); /* Unload passengers */
Note: These function prototypes lack parameters; add whatever parameters you feel are necessary so that the program works without global variables. The program's output should reflect the bus's actions by reporting each change in state, along with the number of passengers loaded and unloaded.
Explanation / Answer
CODE:
//description and function of each variable is defined in the comment line
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
int bus_stop(int flag)
{
if(flag==0) //checks the status of the flag
cout<<"Bus is already stopped"<<endl;
if(flag==1) //checks the status of the flag
{
cout<<"Bus is moving and now it will stop"<<endl;
flag=0; //changes the status of the flag
}
return(flag);
}
int bus_start(int flag)
{
if(flag==1) //checks the status of the flag
cout<<"Bus is already moving"<<endl<<endl;
if(flag==0) //checks the status of the flag
{
cout<<"Bus is in stop position and now it will start"<<endl;
flag=1; //changes the status of the flag
}
return(flag);
}
int bus_load(int total,int cap)
{
int c;
c=rand()%cap;
if(c<=total)
{
total=total+c;
cout<<"Number of passengers loaded is "<<c<<endl;
}
else
{
cout<<"Number of passenger loaded is "<<cap-total<<endl;
total=cap;
}
return(total);
}
int bus_unload(int total,int cap)
{
int b;
b=rand()%cap;
if(b>0 && b<total)
{
total=total-b;
cout<<"Number of passengers unloaded is "<<b<<endl;
}
else
if(b==0)
cout<<"No passenger is their to unload ";
else //when the number of passengers to be uloaded is more than total number of passengers sitting in the bus
{
cout<<"Number of passengers unloaded is "<<total<<endl;
total=0;
}
return(total);
}
int main()
{
int i=1;
int flag=0; //used to denote the status of bus ie if bus is moving or stop
int cap,stp; //cap stores the total capacity of the bus and stp stores the total number of stopages of the bus
int total=5;//indicates the initial number of passengers when the bus starts n stores the current number of passengers in the bus after loading and unloading
cout<<"Enter the capacity of bus"<<endl;
cin>>cap;
cout<<"Enter the number of stopages"<<endl;
cin>>stp;
cout<<"Initially bus is loaded with 5 passengers ";
flag=bus_start(flag);
cout<<" ";
while(i<=stp)
{
flag=bus_stop(flag);
total=bus_unload(total,cap);
total=bus_load(total,cap);
flag=bus_start(flag);
cout<<"Number of passengers present in the bus is "<<total<<endl<<endl;
i++;
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.