2 Replies - 652 Views - Last Post: 25 March 2013 - 10:54 AM Rate Topic: -----

#1 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Queue Simulation Program wrong output and how to add multiple servers

Posted 23 March 2013 - 05:38 PM

So I have this queue simulation program that I've almost finished except for two problems. First problem is that according to the example on the handout my average wait time (avg wait time being the total wait time of customers who have made it through the line / by the number of customers who made it through the line) is wrong. The example on the hand out had this as it's input and output:

Input:
Simulation Length: 100
Average Transaction Time: 5
Number of Servers: 1
Average Time Between Arrivals: 3

Output:
Average wait time: 24.00 minutes
Number of customes still waiting: 16



Now here is what I am getting with same input:

Average wait time: 21.00
Number of customers still waiting: 14



I can see that I'm just a little off on customers still waiting and/or total wait time but I'm not sure how to fix it.
Now the second problem I'm having is I don't know how to implement having more than one server. The code I have works for one server but it doesn't change anything when I enter 2 or more. I've been trying to figure it out all day but with no luck. I've gotten only about 20 hours of sleep total over the past 9 days because my doctor changed my sleeping medication since the old medicine had stopped working and the transition has been really hard, and I also have a migraine too so my ability to think is kind of crippled right now.

Anyway here is the driver program, the server class and the customer class:

Customer.h
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

class Customer
{
public:
	Customer();                                                       // Default constructor
	Customer(int a, int tTime);                                       // Sets arrivalTime, transTime, and customer number to input values
	int getArrivalTime() const;                                       // Returns arrival time of a customer object
	int decrementTransTime();                                         // Decrements transaction time will be used while a customer is at teller window
	bool Served();                                                    // Determines if customer has been served by teller or not

private:
	int arrivalTime;                                                  // Time (in minutes) when customer entered the queue
	int transTime;                                                    // Transaction time 
};



Customer.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Customer.h"

using namespace std;

Customer::Customer() 
// Function: Constructs a default Customer object
// Pre: None
// Post: arrivalTime, transTime, and customer are set to 0
{
	arrivalTime = 0;
	transTime = 0;
}
Customer::Customer(int a, int tTime)
// Function: Constructs a Customer object using input from user
// Pre: None
// Post: arrivalTime is set to parameter a, transTime is set to 
// parameter tTime, and customer is set to parameter c
{
	arrivalTime = a;
	transTime = tTime;
}
int Customer::getArrivalTime() const
// Function: Retrieves arrival time of a customer object
// Pre: A Customer object has been intialized
// Post: returns arrivalTime
{
	return arrivalTime;
}

int Customer::decrementTransTime()  
// Function: Decrements the transaction time by 1
// Pre: transTime has been intialized
// Post: transTime has been decremented by 1
{
	transTime--;
	return transTime;
}

bool Customer::Served()  
// Function: Determines whether a Customer object is finished
// at the teller window
// Pre: Customer object has been intialized
// Post: Returns true if transTime is 0, otherwise it returns false.
{
	if (transTime == 0)
		return true;
	else
		return false;
}



Server.h
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Customer.h"

using namespace std;

class Server
{
public:
	Server();                                             // Default constructor
	void setCustomer(const Customer &c);                         // Assigns current customer at window to cAtWindow
	bool isAvailable(bool served);                                   // Determines if server is available or not

private:
	Customer cAtWindow;                                   // Customer object
	bool available;                                       // True if server is available, false if server is not available
};



Server.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Server.h"

using namespace std;

Server::Server()
// Function: Constructs a Server object
// Pre: None
// Post: available is set to true
{
	available = true;
}
void Server::setCustomer(const Customer &c)
// Function: Assigns the Customer object currently at
// teller window to cAtWindow
// Pre: Customer object has been intialized
// Post: cAtWindow will contain the current customer being served
// and available will be set to false.
{
	cAtWindow = c;
	available = false;
}
bool Server::isAvailable(bool served)
// Function: Determines if server is available or not
// Pre: available variable, and Server and Customer objects have been intialized
// Post: If available is true then return true, otherwise if 
// customer is done set available to true and return available.
{
	if (available == true)
		return true;
	if (served)
		available = true;


	return available;
}



Driver.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Queue.h"
#include "Server.h"

using namespace std;

const int MAX_SERVERS = 10;
const int MAX_SIM_LNGTH = 36000;
const int MIN_TRANS_TIME = 2;
const int MAX_TRANS_TIME = 600;
const int MAX_TIME_ARVLS = 100;

void PrintHyphens();
void Simulation(int, int, int, int);

int main()
{
	int simLngth;                                                                   // Stores simulation length entered by user
	int avgTransTime;                                                               // Stores average transaction time entered by user
	int numServers;                                                                 // Stores number of servers entered by user
	int avgTmeBtwnArvls;                                                            // Stores average time between arrivals entered by user
	
	PrintHyphens();
	cout << "- Please Enter Values for the Following Parameters -" << endl;
	PrintHyphens();
	cout << endl;

	// Prompt for simulation length and store user input into
	// simLngth variable
	cout << "Simulation Length: ";
	cin >> simLngth;

	// Input validation for simulation length
	while (simLngth <= 0 || simLngth > MAX_SIM_LNGTH)
	{
		cout << "You have entered an invalid number!" << endl;
		cout << "Please enter a number between 1 and 36000: ";
		cin >> simLngth;
	}

	// Prompt for average transaction time and store user input into
	// avgTransTime variable
	cout << "Average Transaction Time: ";
	cin >> avgTransTime;

	// Input validation for average transaction time
	while (avgTransTime < MIN_TRANS_TIME || avgTransTime > MAX_TRANS_TIME)
	{
		cout << "You have entered an invalid number!" << endl;
		cout << "Please enter a number between 2 and 600: ";
		cin >> avgTransTime;
	}

	// Prompt for number of servers and store user input into 
	// numServers variable
	cout << "Number of Servers: ";
	cin >> numServers;

	// Input validation for number of servers
	while (numServers <= 0 || numServers > MAX_SERVERS)
	{
		cout << "You have entered an invalid number!" << endl;
		cout << "Please enter a number between 1 and 10: ";
		cin >> numServers;
	}

	// Prompt user for average time between arrivals and store user input into
	// avgTmeBtwnArvls
	cout << "Average Time Between Arrivals: ";
	cin >> avgTmeBtwnArvls;

	// Input validation for average time between arrivals
	while (avgTmeBtwnArvls <= 0 || avgTmeBtwnArvls > MAX_TIME_ARVLS)
	{
		cout << "You have entered an invalid number!" << endl;
		cout << "Please enter a number between 1 and 100: ";
		cin >> avgTmeBtwnArvls;
	}
	cout << endl;

	Simulation(simLngth, avgTransTime, numServers, avgTmeBtwnArvls);

	

	return 0;
}
void Simulation(int sLngth, int avgTrT, int nServers, int avgTBA)
{
	int timeInLine = 0;                                    // Counter for every minute waited in line
	int totalWait = 0;                                     // Total wait for customers that have made it through line (used in avgWait formula)
	int numCustomers = 0;                                  // Number of customers that made it through line (used in avgWait formula)
	int totalCustomers = 0;                                // Total number of customers 
	int nCustomersLeft;                                    // Number of customers left waiting in line after simulation
	int exitTime;                                          // Records the time when a customer leaves the line
	Customer frontCust;                                    // Holds customer in front of line 
	Queue<Customer> line;                                  // Queue object for line 
	Customer c;                                            // Customer object
	Server s;                                              // Server object

	for (int mins = 0; mins <= sLngth; mins++)
	{	
		// If avergae time between arrival divides evenly with current minute
		// then add a new customer to the queue.
		if (mins % avgTBA == 0)
		{
			c = Customer(mins, avgTrT);
			line.enqueue(c);
			totalCustomers++;
		}
		
		// If server is free and if the line is not empty, customer at front of line
		// advances to server window
		if (s.isAvailable(frontCust.Served()) && !line.isEmpty())
		{
			frontCust = line.queueFront();
			s.setCustomer(frontCust);
			line.dequeue(c);
			
			exitTime = mins;

			if (exitTime < frontCust.getArrivalTime())
				totalWait = (totalWait + (frontCust.getArrivalTime() - exitTime));
			else
				totalWait = (totalWait + (exitTime - frontCust.getArrivalTime()));
		}
		
		// If a customer is at the server window time remaining for that customer
		// to be serviced is decremented
		if (!s.isAvailable(frontCust.Served()))
		{
			frontCust.decrementTransTime();
			if (frontCust.Served())
				numCustomers++;
		}

		//If there are customers in line the additional minute that they have remained
		// in the queue is recorded.
		if (!line.isEmpty())
		{
			timeInLine++;
		}
	}
	
	// These values are for debugging
	cout << "Time in line: " << timeInLine << endl;
	cout << "Total Wait time: " << totalWait << endl;
	cout << "Number of customers who made it through line: " << numCustomers << endl;
	cout << "Total Customers (both stll waiting and those served): " << totalCustomers << endl;
	cout << endl;

	double avgWait = totalWait / numCustomers;
	
	cout << "Average wait time: " << fixed << setprecision(2) << avgWait << endl;

	if (numCustomers > totalCustomers)
		nCustomersLeft = numCustomers - totalCustomers;
	else 
		nCustomersLeft = totalCustomers - numCustomers;
	
	cout << "Number of customers still waiting: " << nCustomersLeft << endl;

	line.MakeEmpty();
}

void PrintHyphens()
{
	for (int i = 0; i <= 51; i++)
	{
		cout << "-";
	}
	cout << endl;
}




Thanks in advance for any and all help! I really appreciate it!

This post has been edited by HKCHaN92: 24 March 2013 - 01:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Queue Simulation Program wrong output and how to add multiple servers

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Queue Simulation Program wrong output and how to add multiple servers

Posted 24 March 2013 - 08:17 PM

I haven't figured exactly what is wrong, but you use a temporary object when the Customer object you should use is in the Server object at the window.

frontCust.decrementTransTime();



I also don't like the Server function isAvailable :

bool isAvailable(bool served);


The Server object should check the whether the Customer in its class (at the window) has been served.

So all you would need is :

bool isAvailable();


Of course you want to decrement the transaction time of the Customer that is being served. So why not add a serve function to the Server class that calls the decrementTransTime function on its Customer object cAtWindow.
Was This Post Helpful? 1
  • +
  • -

#3 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Queue Simulation Program wrong output and how to add multiple servers

Posted 25 March 2013 - 10:54 AM

View Post#define, on 24 March 2013 - 09:17 PM, said:

I haven't figured exactly what is wrong, but you use a temporary object when the Customer object you should use is in the Server object at the window.

frontCust.decrementTransTime();



I also don't like the Server function isAvailable :

bool isAvailable(bool served);


The Server object should check the whether the Customer in its class (at the window) has been served.

So all you would need is :

bool isAvailable();


Of course you want to decrement the transaction time of the Customer that is being served. So why not add a serve function to the Server class that calls the decrementTransTime function on its Customer object cAtWindow.


It works now! Thanks again #define for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1