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

Write a c++ simulation program of the lines at a grocery store. There are multip

ID: 3675086 • 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.

Use the car wash simulation in page 401-411

The program provides a simulation of the lines at a grocery store. The program is similar to the car wash simulation from the text but utilizes 5 queues instead of 1 to represent five cashiers with checkout lines. Customers enter the shortest line (queue) to check out.
If the lines are of equal size then the customer enters the first line. Transaction times (ringing up customers) are random (within the range of 10 to 600 seconds). When the program is finished running it prints out the number of customers served and the average wait time in seconds.

Input

    The probability (use percentage scale from 1 to 100 where 100 means 1 customer per second) that a new customer arrives during any given second (we assume that, at most, one customer arrives in a second)
    The total length of time to be simulated (in seconds)

Output

    The number of customers serviced in the simulation
    The average time that a customer spent in line

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 // 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 hasbeen 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 // Provides assert

#include // 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 // Provides cout

#include // Provides EXIT_SUCCESS

#include // 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 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;

}

the above code are in the book. For this assignment you should create probably 3 files, main.cpp, function.h, and function.cpp.

Please make sure all your code are correct and will compile on visual studio 2015, thank you, really appreciate.

Explanation / Answer

#include #include #include struct queueNode { /* self-referential structure */ int data; struct queueNode *nextPtr; }; typedef struct queueNode QueueNode; typedef QueueNode *QueueNodePtr; /* function prototypes */ void printQueue( QueueNodePtr ); int isEmpty( QueueNodePtr ); char dequeue( QueueNodePtr *, QueueNodePtr * ); void enqueue( QueueNodePtr *, QueueNodePtr *, int ); int main() { int t_time = 0; //track global time in secs int t_arrival = 0; //track arrival time int t_depart = 0; //track departure and hence time needed to serve last customer int customer_n = 0; QueueNodePtr headptr = NULL,tailptr = NULL; srand((unsigned int)time(NULL)); while(t_time != 50) { if(t_time == t_arrival) { //customer arrives customer_n++; enqueue(&headptr,&tailptr,customer_n); //customer joins the Q t_arrival += rand()%4 + 1; printf(" time = %d,customer %d joins Q, next customer scheduled for %d ",t_time,customer_n,t_arrival); printQueue(headptr); getchar(); } if(t_time == t_depart) { if(!isEmpty(headptr)) { dequeue(&headptr,&tailptr); //customer gets serviced t_depart += rand()%4 + 1; //time customer leaves service printf("time = %d,customer %d ON, next service session at %d sec ",t_time,customer_n,t_depart); } else { //if Q empty schedule service at a later time t_depart += rand()%4 + 1; printf("time = %d,next service session at %d sec ",t_time,t_depart); } printQueue(headptr); getchar(); } t_time++; } printQueue(headptr); return 0; } void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,int value ) { QueueNodePtr newPtr; newPtr = malloc( sizeof( QueueNode ) ); if ( newPtr != NULL ) { newPtr->data = value; newPtr->nextPtr = NULL; if ( isEmpty( *headPtr ) ) *headPtr = newPtr; else ( *tailPtr )->nextPtr = newPtr; *tailPtr = newPtr; } else printf( "%c not inserted. No memory available. ", value ); } char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr ) { char value; QueueNodePtr tempPtr; value = ( *headPtr )->data; tempPtr = *headPtr; *headPtr = ( *headPtr )->nextPtr; if ( *headPtr == NULL ) *tailPtr = NULL; free( tempPtr ); return value; } int isEmpty( QueueNodePtr headPtr ) { return headPtr == NULL; } void printQueue( QueueNodePtr currentPtr ) { if ( currentPtr == NULL ) printf( "Queue is empty. " ); else { printf( "The queue is: " ); while ( currentPtr != NULL ) { printf( "%d
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote