Write a c++ simulation program of the lines at a grocery store. There are multip
ID: 675269 • Letter: W
Question
Write a c++ simulation program of the lines at a grocery store. There are multiple queues instead of one. You might use a vector of queues to simulate the lines. Assume that there are five cashier lines at the grocery store. Customers enter randomly to check out, and then enter the shortest line. If the lines are equal, then the first available line is chosen.
Each transaction takes a random amount of time to complete.
Then if possible, enable the shoppers to:
• Avoid a line if all lines are a certain length
• Leave a line if they have waited beyond a certain time
• Check if another line is shorter at specified time intervals
• Switch lines if another line is shorter
Please see sample program below of a carwash queue to start:
/* FILE: washing.h (part of the namespace main_savitch_8A)
// CLASSES PROVIDED: bool_source, averager, washer.
//
// CONSTRUCTOR for the bool_source class:
// bool_source(double p = 0.5)
// Precondition: 0 <= p <= 1.
// Postcondition: The bool_source has been initialized so that p is the
// approximate probability of returning true in any subsequent activation
// of query( ).
//
// CONSTANT MEMBER FUNCTION for the bool_source class:
// bool query( ) const
// Postcondition: The return value is either true or false, with the
// probability of a true value being approximately p (from the constructor).
//
// CONSTRUCTOR for the averager class:
// averager( )
// Postcondition: The averager has been initialized so that it
// is ready to accept a sequence of numbers to average.
//
// MODIFICATION MEMBER FUNCTION for the averager class:
// void next_number(double value)
// Postcondition: The averager has accepted value as the next
// number in the sequence of numbers which it is averaging.
//
// CONSTANT MEMBER FUNCTIONS for the averager class:
// size_t how_many_numbers( ) const
// Postcondition: The value returned is a count of how many
// times next_number has been activated.
//
// double average( ) const
// Precondition: how_many_numbers > 0.
// Postcondition: The value returned is the average of all the
// numbers which have been given to the averager.
//
// CONSTRUCTOR for the washer class:
// washer(unsigned int s = 60)
// Precondition: The value of s is the number of seconds needed for
// the completion of one wash cycle.
// Postcondition: The washer has been initialized so that all
// other member functions may be used.
//
// MODIFICATION MEMBER FUNCTIONS for the washer class:
// void one_second( )
// Postcondition: The washer has recorded (and simulated) the
// passage of one more second of time.
//
// void start_washing( )
// Precondition: The washer is not busy.
// Postcondition: The washer has started simulating one wash
// cycle. Therefore, is_busy( ) will return true until
// the required number of simulated seconds have occured.
//
// CONSTANT MEMBER FUNCTIONS for the washer class:
// bool is_busy( ) const
// Postcondition: Return value is true if the washer is busy
// (in a wash cycle); otherwise the return value is false.
//
// VALUE SEMANTICS for the bool_source, averager, and washer classes:
// Assignments and the copy constructor may be used with the three classes.
*/
#ifndef WASHING_H
#define WASHING_H
#include <cstdlib> // Provides std::size_t
namespace main_savitch_8A
{
class bool_source
{
public:
// CONSTRUCTOR
bool_source(double p = 0.5);
// CONSTANT function
bool query( ) const;
private:
double probability; // Probability of query( ) returning true
};
class averager
{
public:
// CONSTRUCTOR
averager( );
// MODIFICATION function
void next_number(double value);
// CONSTANT functions
std::size_t how_many_numbers( ) const { return count; }
double average( ) const;
private:
std::size_t count; // How many numbers have been given to the averager
double sum; // Sum of all the numbers given to the averager
};
class washer
{
public:
// CONSTRUCTOR
washer(unsigned int s = 60);
// MODIFICATION functions
void one_second( );
void start_washing( );
// CONSTANT function
bool is_busy( ) const { return (wash_time_left > 0); }
private:
unsigned int seconds_for_wash; // Seconds for a single wash
unsigned int wash_time_left; // Seconds until washer no longer busy
};
}
#endif
// FILE: washing.cxx
// CLASSES implemented: bool_source, averager, washer
// INVARIANT for the bool_source ADT:
// 1. The member variable probability is the appoximate probability that query( ) returns true.
// INVARIANT for the averager ADT:
// 1. The member variable count indicates how many numbers the averager has been given.
// 2. The member variable sum is the sum of all the numbers that the averager has been given.
// INVARIANT for the washer class:
// 1. The member variable seconds_for_wash is the number of seconds required for one wash.
// 2. The member varible wash_time_left is 0 if the washer is not busy; otherwise it is the number of seconds until the washer is free.
#include <cassert> // Provides assert
#include <cstdlib> // Provides rand, RAND_MAX, size_t
#include "washing.h" // Provides bool_source, averager, washer definitions
using namespace std;
namespace main_savitch_8A
{
bool_source::bool_source(double p)
// Library facilities used: cassert
{
assert(p >= 0);
assert(p <= 1);
probability = p;
}
bool bool_source::query( ) const
// Library facilities used: cstdlib
{
return (rand( ) < probability * RAND_MAX);
}
averager::averager( )
{
count = 0;
sum = 0;
}
void averager::next_number(double value)
{
++count;
sum += value;
}
double averager::average( ) const
// Library facilities used: cassert
{
assert(how_many_numbers( ) > 0);
return sum/count;
}
washer::washer(unsigned int s)
{
seconds_for_wash = s;
wash_time_left = 0;
}
void washer::one_second( )
{
if (is_busy( ))
--wash_time_left;
}
void washer::start_washing( )
// Library facilities used: cassert
{
assert(!is_busy( ));
wash_time_left = seconds_for_wash;
}
}
// FILE: carwash.cxx
// A small test program to test the car_wash_simulate function.
#include <iostream> // Provides cout
#include <cstdlib> // Provides EXIT_SUCCESS
#include <queue> // Provides queue
#include "washing.h" // Provides averager, bool_source, washer
using namespace std;
using namespace main_savitch_8A;
void car_wash_simulate
(unsigned int wash_time, double arrival_prob, unsigned int total_time);
// Precondition: 0 <= arrival_prob <= 1.
// Postcondition: The function has simulated a car wash where wash_time is the
// number of seconds needed to wash one car, arrival_prob is the probability
// of a customer arriving in any second, and total_time is the total number
// of seconds for the simulation. Before the simulation, the function has
// written its three parameters to cout. After the simulation, the function
// has written two pieces of information to cout: (1) The number of
// cars washed, and (2) The average waiting time of a customer.
int main( )
{
// Wash takes 240 seconds
// Customers arrive on average once every 400 seconds
// Total simulation time is 6000 seconds
car_wash_simulate(240, 1.0/400, 6000);
return EXIT_SUCCESS;
}
void car_wash_simulate
(unsigned int wash_time, double arrival_prob, unsigned int total_time)
// Library facilities used: iostream, queue, washing.h
{
queue<unsigned int> arrival_times; // Waiting customer’s time stamps
unsigned int next; // A value taken from the queue
bool_source arrival(arrival_prob);
washer machine(wash_time);
averager wait_times;
unsigned int current_second;
// Write the parameters to cout.
cout << "Seconds to wash one car: " << wash_time << endl;
cout << "Probability of customer arrival during a second: ";
cout << arrival_prob << endl;
cout << "Total simulation seconds: " << total_time << endl;
for (current_second = 1; current_second <= total_time; ++current_second)
{ // Simulate the passage of one second of time.
// Check whether a new customer has arrived.
if (arrival.query( ))
arrival_times.push(current_second);
// Check whether we can start washing another car.
if ((!machine.is_busy( )) && (!arrival_times.empty( )))
{
next = arrival_times.front( );
arrival_times.pop( );
wait_times.next_number(current_second - next);
machine.start_washing( );
}
// Tell the washer to simulate the passage of one second.
machine.one_second( );
}
// Write the summary information about the simulation.
cout << "Customers served: " << wait_times.how_many_numbers( ) << endl;
if (wait_times.how_many_numbers( ) > 0)
cout << "Average wait: " << wait_times.average( ) << " sec" << endl;
}
Explanation / Answer
#include #include #include #include "queue.h" using namespace std; int shortest_queue(Queue q[], int queuecount) { int shortest = 0; for (int i = 1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.