Link to home
Start Free TrialLog in
Avatar of edelossantos
edelossantos

asked on

DMV Line Queue Simulator

>> ok...this code compiles and run,...would you please check if the code below is what is being asked,...and if not...would you please help in the modification.

*************************************Code***********************************************

# Makefile:
#           It uses the C++ Compiler with all warnings and
#           debugging enabled. A single executable will
#           be produced.
# ---------------------------------------------------------------
CPP = g++
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9
DFLAGS = -g
LD = g++
LLIBS = -lm
#
# Linker Flags:
# set "-lm" if we need to use the "math" functions
CLIBS= -lm
RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of date
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# In general ".cpp" (unlike ".c") is not automatically recognized as a
# reserved suffix, so we should inform "make" in case we use this later:
.SUFFIXES:      .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
      ${CPP} -c ${DFLAGS} ${CFLAGS} $<

OFILES=            main.o queue.o server.o citizen.o

HFILES=            queue.h server.h citizen.h  

# dependencies
#
default:      all
all:              main      
#
main:           $(OFILES)
            ${LD} ${CFLAGS} ${LLIBS} $(OFILES) -o $@

main.o:            main.cpp

queue.o:      queue.cpp queue.h

server.o:      server.cpp server.h

citizen.o:      citizen.cpp citizen.h

#
clean:
      ${RM} *.o
      ${RM} core
#
veryclean:      clean
      ${RM}  main  


***************************************

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>

using namespace std;

const int MAXSIZE = 100;         //Maximum size of the queue.
      
class Queue
{
friend ostream& operator<< (ostream&, Queue&);
        
public:
   Queue ();
            
   void push           (int n);      //Add person to the queue
   void pop            ();           //Remove person from beginning of queue
   int  getFirstInLine ();           //Get a copy of the first person in line
   bool isEmpty        ();
   
private:
   int size;                   //Queue length
   int line [MAXSIZE];
};
        
#endif

********************************

#include <iomanip>

#include "queue.h"
      
const int WIDTH = 5;  //Print field size
      
Queue::Queue ()
{
    size = 0;
}
        
void Queue::push (int n)
{
   if (size < MAXSIZE)
   {
        line [size] = n;
        size++;
   }
}
        
void Queue::pop ()
{  
    if (size > 0)
    {    
    //Remove the person at the beginning by moving
    //  everyone else forward one place.
      
       for (int n = 0; n < size - 1; n++)
          line [n] = line [n + 1];
             
       size--;
    }
}

int Queue::getFirstInLine ()
{
    //Return a copy of the first person in line.
   
    int temp = 0;
   
    if (size > 0)
       temp = line [0];
       
    return temp;
}

bool Queue::isEmpty ()
{
   return size == 0;
}
              
ostream& operator<< (ostream& out, Queue& Q)
{
    for (int n = 0; n < Q.size; n++)
        out << setw (WIDTH) << Q.line [n];
         
    return out;
}      

*****************************************



#include "queue.h"

#ifndef SERVER_H
#define SERVER_H
      
#include <iostream>
      
using namespace std;
      
//class invariantfor the service class:

//A server object represents a station where a person receives
//  a service (banking, haircut, etc., etc.).

//A server object contains:
//  1)  a person number, that represents the person getting the service.
//  2)  a timer, that records how long the person has been getting the servic.
//  3)  a service time, that is the limit of the time a person gets service.
//         When the timer reaches the service time, then the person is removed.

class server
{
friend ostream& operator<< (ostream&, server&);
       
public:
    server (int);                 //The n is the time to service a person.
         
    bool isAvailable  ();         //True if noone is present, otherwise false
   
    void StartService (int n);    //n is the person to service
   
    void Update       ();         //Ups the timer, kicks the person
                                  //  out when the time is up.
       
private:
    int person;
    int timer;
    int serviceTime;
};

#endif

**********************************************


#include <iomanip>
     
#include "server.h"
     
const int WIDTH  = 5;           //Width of print fields
const int NOBODY = 0;

server::server (int time)      
{
//Initialize the person to empty, the time to 0, and set the length of
//  the service time.

    person      = NOBODY;
    timer       = 0;
    serviceTime = time;
}
       
bool server::isAvailable  ()
{
    bool returnVal = false;
   
    if (person == NOBODY)
       returnVal = true;
     
    return returnVal;
}
     
void server::StartService (int n)
{
//n is the number of the person to be serviced.  Since service is
//  starting, set the timer to 0.

    person = n;
    timer  = 0;
}
       
void server::Update ()
{
//Add a minute to the timer.  If the service time is reached, then
//  the person being serviced is done, so remove him/her, and reset
//  the time to 0.

    timer++;
         
    if (timer == serviceTime)  
    {
        person = NOBODY;
        timer  = 0;
    }
}
       
ostream& operator<< (ostream& out, server& service)
{
    if (!service.isAvailable ())
        out << setw (WIDTH) << service.person;
       
    else
        cout << setw (WIDTH) << "   ";
         
    return out;
}

**********************************************


#include "server.h"

#ifndef CITIZEN_H
#define CITIZEN_H

//class invariant for the arrivalGenerator class:

//An arrivalGenerator object generates the number of the next person arriving
//  for service, if there is one.  Whether or not a person arrives is
//  determined at random according to a set probability.

//The arrivalGenerator object contains:
//  1)  The probability that is used to determine if an arrival occurs.
//  2)  The number of the next arrival, as an identifier for that person.
 
class Citizen
{
public:
    Citizen (int, double);  //The int is a seed for the random
                            //  number generation.
                            //The double is the probability to
                            //  be usedfor generating random    
                            //  numbers.
       
    bool isArrival ();      //True if an arrival occurs.
   
    int  getArrival ();     //The number of the last person arriving.
           
private:
    double probability;
    int    nextCustomer;
};
     
#endif

****************************************


#include "citizen.h"

#include <cstdlib>       //For srand and rand

Citizen::Citizen(int    s,   //srand seed
                                    double P)   //Probability of arrival
{
   srand (s);             //seed the random sequence
   
   probability   = P;
   nextCustomer  = -1;
}

bool Citizen::isArrival ()
{
   bool returnVal = false;
       
   double event = rand () % 100 + 1;   //Get a random number betw
                                       //  1 and 100
       
   if (event / 100.0 <= probability)   //Determine if an arrival
                                       //  occurred
   {      
       returnVal = true;
       nextCustomer++;
   }
         
   return returnVal;
}

int Citizen::getArrival ()
{
   return nextCustomer;
}

***********************************


#include <iostream>
#include <iomanip>
      
#include "queue.h"
#include "server.h"
#include "citizen.h"
      
const int   SERVICETIME   = 3;
const int   SEED          = 6;
const float PROBABILITY   = .333;
const int   WIDTH         = 6;
const int   MAXTIME       = 50;
      
int main ()
{
                 
      server teller (SERVICETIME);      //Set up the teller with the
                                        //  given service time.
        
      Queue line;                  
      
      Citizen arrivalMachine (SEED, PROBABILITY);
               
      cout << "Minute    " << "Serving    "       //Table headings
           << "Queue"      << endl;
              
      //Run throught the minutes of the simulation
      
      for (int minute = 0; minute <= MAXTIME; minute++)
      {
         //Teller advances its timer, removes customer if
         //   service is finished.
        
         teller.Update ();
                      
         //Generate an arrival (or not).  If there is an arrival put the
         //   person in line.
            
         if (arrivalMachine.isArrival ())          
             line.push (arrivalMachine.getArrival ());
        
         //If the teller is open, give first person in line to the
         //   teller for service, and remove the person from the line.
        
         if (teller.isAvailable ())  
         {
           teller.StartService (line.getFirstInLine ());
                   
           line.pop ();
         }
          
         //Display the current state of the teller and the line.
        
         cout << setw (WIDTH) << minute   << " > "
              << teller       << "     "  << line << endl;
      }
            
      return 0;
}         

**************************************************

output:

$ main
Minute    Serving    Queue
     0 >
     1 >     1
     2 >     1
     3 >     1
     4 >
     5 >
     6 >
     7 >
     8 >
     9 >
    10 >     2
    11 >     2
    12 >     2         3
    13 >     3
    14 >     3
    15 >     3
    16 >
    17 >     4
    18 >     4         5
    19 >     4         5    6
    20 >     5         6
    21 >     5         6
    22 >     5         6
    23 >     6
    24 >     6         7
    25 >     6         7    8
    26 >     7         8    9
    27 >     7         8    9
    28 >     7         8    9
    29 >     8         9   10
    30 >     8         9   10   11
    31 >     8         9   10   11
    32 >     9        10   11
    33 >     9        10   11
    34 >     9        10   11
    35 >    10        11
    36 >    10        11
    37 >    10        11   12
    38 >    11        12
    39 >    11        12
    40 >    11        12   13
    41 >    12        13   14
    42 >    12        13   14
    43 >    12        13   14   15
    44 >    13        14   15
    45 >    13        14   15   16
    46 >    13        14   15   16   17
    47 >    14        15   16   17   18
    48 >    14        15   16   17   18
    49 >    14        15   16   17   18
    50 >    15        16   17   18
edelossa@helios  test $ $

>> Is this code close and exact to what is being asked for below?

****************************************

Write a program that simulates a line at the Department of Motor Vehicles (DMV). The line is a queue object.  Citizens (i.e., citizen objects) arrive in random regular intervals of 1 – 10 minutes. Also, each citizen is served in random integer intervals of 1 – 10 minutes. Obviously, the rates need to be balanced. If the average arrival rate is larger than the average service rate, the queue will grow infinitely. Even with “balanced” rates, randomness can still cause long lines. Run the DMV simulation for an 8-hour day (480 minutes) using the following algorithm:

 

Choose a random integer between 1 and 10 to determine the minute at which the first citizen arrives.
At the first citizen’s arrival time:
Determine the citizen’s service time (random integer from 1 to 10);
Begin servicing the citizen;
Schedule arrival time of the next citizen (random integer 1 to 10 added to the current time).
For each minute of the day:
If the next citizen arrives,
                                                              i.      Announce the arrival of citizen N;

                                                            ii.      Enqueue the citizen;

                                                          iii.      Schedule the arrival time of the next citizen;

If service was completed for the last citizen,
                                                              i.      Announce service complete for citizen X;

                                                            ii.      Dequeue the next citizen to be serviced;

                                                          iii.      Determine the citizen’s service completion time (random integer from 1 to 10 added to the current time).

Now run your simulation for 480 minutes and answer each of the following questions:

>> Please advise.  Del
SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edelossantos
edelossantos

ASKER

Thank you Alex,...I will keep this open for tomorrow.  Del
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A billion and one thank yous' Alex.  I wish I could give you more points.  Del