Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

rite a program that emulates the behavior of a bus. The bus must have a schedule

ID: 3576397 • Letter: R

Question

rite 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 or 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

Explanation / Answer

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;

void stop_bus(int);
int unload_pass(int);
int load_pass(int);
void start_bus();
int main()
{
   int stops;
   cout<<"eneter stops of the bus"<<endl;
   cin>>stops>>endl;
    srand(time(0));
    int currentStop = 1;
    int maxPass = 50;

    load_pass(maxPass);
    while(currentStop < stops)
    {
        currentStop++;
        stop_bus(currentStop);
        unload_pass(maxPass);
        load_pass(maxPass);
    }


    system("pause");
    return 0;
}
void stop_bus(int currentStop)
{
    cout << "Currently at stop #" << currentStop << endl;
    cout << "Please disembark the bus in an orderly fashion." << endl;
    cout << endl;
}
int unload_pass(int maxPass)
{
    int passOff;
    int currentPass;
    currentPass = load_pass(maxPass);

    passOff = 1 + rand() % 50;
    while (currentPass - passOff > 0 )
    {
        cout << currentPass << ": That number is not in the range." << endl;
        passOff = 1 + rand() % 50;
        cout << endl;
    }

    cout << "People leaving the bus: " << passOff << endl;
    currentPass = currentPass - passOff;
    cout << "Current bus load is " << currentPass << endl;
    cout << endl;
    cout << currentPass << ": That number is not in the range" << endl;
    cout << endl;
    return currentPass;
}
int load_pass(int maxPass)
{
    int passOn;
    int currentPass;
    currentPass = unload_pass(maxPass);

    passOn = 1 + rand() % 50;
    while (currentPass + passOn > maxPass)
    {
        cout << currentPass << ": That number is not in the range" << endl;
        passOn = 1 + rand() % 50;
        cout << endl;
    }
    cout << "People boarding the bus: " << passOn << endl;
    currentPass = currentPass + passOn;
    cout << "Current bus load is " << currentPass << endl;
    cout << endl;
    return currentPass;
}