d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Please.. C++ > Simulation.
12Next
Add Reply New Topic New Poll
Member
Posts: 18,513
Joined: Dec 17 2005
Gold: 53.50
Apr 5 2014 10:43pm
A queue can be used to set up a simulation for customer checkout at a supermarket or customers handled by tellers in a bank. By running different scenarios, management can tell how many checkout lines to use or tellers to have on duty. The queue will hold an object of a customer in a checkout line with an id number, arrival time, and service time of 1-4 minutes, assigned randomly as the customer enters the queue Use the following algorithm:

Set up empty queue

Set current time = 0

Schedule the arrival for the first customer for some arbitrary value (t_arrival = 0 will be okay)

Loop 0 to maxtime (use 720 minutes – 12 hours)

Check to see if the queue is not empty and current time = t_departure a. remove that customer at the head of the queue and print a message. You can also total wait times and count number of departing customers here for average at end.

b. Set t_departure = current time + service time of customer at the head of the queue.

c. If the queue is still not empty after removing the head, print a message that you are servicing the customer at the head and set t_departure to current time + service time of that customer.

2. If current time = t_arrival

a. increment customer number

b. obtain the service time of the new customer (random 1-4)

c. if queue is empty,

set departure time = to current time + service time of this customer

d. add this customer to queue

e. report the arrival and service time of this customer

f. schedule the arrival time of another customer by obtaining a random number (1-4) and set arrival_ time to current time + this value.

Increment current time

Calculate an average wait time in the queue by adding up all times waited in queue of departing customers

totalWaitTime += departureTime - aqueue.front().getArrivalTime()

and divide by the number of departed customers.

Set up a class of a customer with id number, service time, and arrival time. Add a parameterized constructor, all getters and setters, and a way to print the object(overload the << operator).

For the main method, use:

# include <queue> // before main

queue<Customer> aqueue; //inside main to start the empty queue




-- I've been out of school for a bit due to health related issues and I am just trying to catch up.
Can anyone help me?

I believe I can set up setters/getters(mutators/accessors if I recall that to be the C++ terminology).. I do not know however how to set up this entire program.
I can do bits by bits. The hardest part to me is looping the time from 0-12 hours[or 720 minutes]. Quite frankly, I'm lost and I don't know where to begin. If anyone can help me please I would greatly appreciate it.

E: I just learned how to set up a class and use them myself as well along with overloading operators.

This post was edited by RecenT on Apr 5 2014 10:44pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Apr 6 2014 10:45pm
Code

for(int i = 0; i < 720; i++){
// stuff that happens for the minute i
}
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 7 2014 09:06pm
Quote (KrzaQ2 @ Apr 7 2014 12:45am)
Code
for(int i = 0; i < 720; i++){
    // stuff that happens for the minute i
}


Let's incrementally build this code in a chain of posts
one tiny step at a time
Code
int main() {
for(int i = 0; i < 720; i++){
// stuff that happens for the minute i
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 7 2014 09:16pm
lets do it.
Code
# include <queue> // before main
int main() {
for(int i = 0; i < 720; i++){
// stuff that happens for the minute i
}
}


This post was edited by carteblanche on Apr 7 2014 09:17pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 7 2014 09:37pm
Code
# include <queue> // before main
int main() {
std::queue<some_class *> myQueue; //must be nice having this shit
for(int i = 0; i < 720; i++){
// stuff that happens for the minute i
}
}


This post was edited by Eep on Apr 7 2014 09:40pm
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 8 2014 03:06pm
Code
# include <queue> // before main

class some_class { //we picked an excellent name for this
};

int main() {
std::queue<some_class *> myQueue; //must be nice having this shit
int current_time
for(int i = 0; i < 720; i++){
// stuff that happens for the minute i
}
}


This post was edited by poodaH on Apr 8 2014 03:12pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 8 2014 07:05pm
About all i have the patience for doing. Should be a decent start though. Not sure how accurate / correct this code is however.


Code
#include <iostream>
#include <queue>
#include <ostream>

/*
* 720 minutes (12 hours)
*/
#define MAX_TIME 720

/*
* typedefs cos' lazy & less typing
*/
typedef unsigned short USHORT;
typedef const USHORT cUSHORT;

class CCustomer
{
public:
/*
* Parameterized ctor
*/
CCustomer(cUSHORT id, cUSHORT arrival_time, cUSHORT service_time):
m_id(id),
m_arrival_time(arrival_time),
m_service_time(service_time) {};

/*
* ostream overload output
*/
friend std::ostream& operator<<(std::ostream& out, const CCustomer& customer) {
out << "ID: " << customer.m_id
<< "Arrival time: " << customer.m_arrival_time
<< "Service time: " << customer.m_service_time;

return out;
}

/*
* Setters
*/
void set_arrival_time(cUSHORT arrival_time) {
m_arrival_time = arrival_time;
}

void set_service_time(cUSHORT service_time) {
m_service_time = service_time;
}

void set_id(cUSHORT id) {
m_id = id;
}

/*
* Getters
*/
USHORT get_arrival_time() const {
return m_arrival_time;
}

USHORT get_service_time() const {
return m_service_time;
}

USHORT get_id() const {
return m_id;
}


private:
/*
* Encapsulate private variables
*/
USHORT m_arrival_time;
USHORT m_service_time;
USHORT m_id;
};

int main(int argc, const char * argv[]) {
/*
* std::queue for customer class objects
*/
std::queue<CCustomer*> customers;

for(USHORT minute = 0; minute < MAX_TIME; ++minute) {
/*
* Do shit that im to lazy to
*/
}

return 0;
}


This post was edited by SelfTaught on Apr 8 2014 07:06pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Apr 9 2014 12:23am
Lol, this is quickly becoming a caricature of good programming techniques, quite like https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

Don't use ugly defines, this is C++, not C.

Don't use raw pointers, unless you're only observing the data, which isn't the case here (the customer is supposed to be removed at departure).
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 9 2014 10:06am
Quote (SelfTaught @ Apr 8 2014 05:05pm)
About all i have the patience for doing. Should be a decent start though. Not sure how accurate / correct this code is however.


Code
#include <iostream>
#include <queue>
#include <ostream>

/*
* typedefs cos' lazy & less typing
*/
typedef unsigned short USHORT;
typedef const USHORT  cUSHORT;

class CCustomer
{
public:
    /*
    * Parameterized ctor
    */
    CCustomer(cUSHORT id, cUSHORT arrival_time, cUSHORT service_time):
        m_id(id),
        m_arrival_time(arrival_time),
        m_service_time(service_time) {};
   
    /*
    * ostream overload output
    */
    friend std::ostream& operator<<(std::ostream& out, const CCustomer& customer) {
        out << "ID: " << customer.m_id
            << "Arrival time: " << customer.m_arrival_time
            << "Service time: " << customer.m_service_time;
       
        return out;
    }
   
    /*
    * Setters
    */
    void set_arrival_time(cUSHORT arrival_time) {
        m_arrival_time = arrival_time;
    }
   
    void set_service_time(cUSHORT service_time) {
        m_service_time = service_time;
    }
   
    void set_id(cUSHORT id) {
        m_id = id;
    }
   
    /*
    * Getters
    */
    USHORT get_arrival_time() const {
        return m_arrival_time;
    }
   
    USHORT get_service_time() const {
        return m_service_time;
    }
   
    USHORT get_id() const {
        return m_id;
    }
   
   
private:
    /*
    * Encapsulate private variables
    */
    USHORT m_arrival_time;
    USHORT m_service_time;
    USHORT m_id;
};

int main(int argc, const char * argv[]) {
  cUSHORT MAX_TIME = 720;
    /*
    * std::queue for customer class objects
    */
    std::queue<CCustomer> customers;
   
    for(USHORT minute = 0; minute < MAX_TIME; ++minute) {
        /*
          * Do shit that im to lazy to
          */
    }
   
    return 0;
}


Quote (KrzaQ2 @ Apr 8 2014 10:23pm)
Lol, this is quickly becoming a caricature of good programming techniques, quite like https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

Don't use ugly defines, this is C++, not C.

Don't use raw pointers, unless you're only observing the data, which isn't the case here (the customer is supposed to be removed at departure).


Betta?

This post was edited by SelfTaught on Apr 9 2014 10:14am
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Apr 9 2014 11:20am
A lot :D

Not sure about the typedefs since you have cstdint in the standard, but whatever you like. The same goes about setters/getters that don't do anything, but that can be attributed to your personal/project guideline and aren't "simply wrong/right".
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll