what the result of average_time have negative

R

Robert

dear sir:
I have read the c++ primer plus 5 book, and do the
chapter 12 programming exercises question 5, but the average_wait have
negative result. I don't understand why? here is the code,
thank you anyone replay me.:)

/************************* customer.h *************************/
#ifndef CUSTOMER_H_
#define CUSTOMER_H_

class Customer
{
private:
long arrive; //arrival time for customer
int processtime; //process time for customer
public:
Customer() { arrive = processtime = 0; }
void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime; }
};

#endif

/************************* customer.cpp *************************/
#include <iostream>
#include "customer.h"

//when is the time at which the customer arrives
//the arrival times is set to when and the processing
//time set to a random value in the range 1 - 3
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;
}

/************************* queue.h *************************/
#ifndef QUEUE_H_
#define QUEUE_H_

#include "customer.h"

namespace QUEUE
{
typedef Customer Item;

class Queue
{
private:
//class scope definitions
//Node is a nested structure definition local to this class
struct Node { Item item; struct Node * next;};
enum { Q_SIZE = 10};
//private class members
Node * front; //pointer to front of Queue
Node * rear; //pointer to rear of Queue
int items; //current number of items in Queue
const int qsize; //maximum number of items in Queue
//preemptive definitiond to prevent public copying
Queue(const Queue & q):qsize(0) { }
Queue & operator=(const Queue & q) { return *this;}
public:
Queue(int qs = Q_SIZE); //create queue with a qs limit
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item & item); //add item to end
bool dequeue(Item & item); //remove item from front
};
}

#endif

/************************* queue.cpp *************************/
#include "queue.h"
#include <cstdlib>

namespace QUEUE
{
//Queue methods
Queue::Queue(int qs):qsize(qs)
{
front = rear = NULL;
items = 0;
}

Queue::~Queue()
{
Node * temp;
while (front != NULL) //while queue is not yet empty
{
temp = front; //save address of front item
front = front->next;//reset pointer to next item
delete temp; //delete former front
}
}

bool Queue::isempty() const
{
return items == 0;
}

bool Queue::isfull() const
{
return items == qsize;
}

int Queue::queuecount() const
{
return items;
}

//Add item to queue

bool Queue::enqueue(const Item & item)
{
if (isfull())
return false;

Node * add = new Node; //create node

if (add == NULL)
return false; //quit if none available

add->item = item; //set node pointers
add->next = NULL;
items++;

if (front == NULL) //if queue is empty,
front = add; //place item at front
else
rear->next = add; //else place at rear

rear = add; //have rear point to new node
return true;
}

//Place frony item into item variable and remove fron a queue
bool Queue::dequeue(Item &item)
{
if (front == NULL)
return false;

item = front->item; //set item to first item in queue
items--;
Node * temp = front;//save location of first item
front = front->next;//reset front to next item
delete temp; //delete former first item

if ( items == 0)
rear = NULL;

return true;
}
}

/************************* main.cpp *************************/
#include <iostream>
#include <cstdlib> //for rand() and srand()
#include <ctime> //for time()

#include "queue.h"
bool newcustomer(double x); //is there a new customer?

void main()
{
const int MIN_PER_HR = 60;
using namespace std;
using namespace QUEUE;

srand(static_cast<unsigned int>(time(0)));

cout << "Case Study: Bank of Healther Automatic Teller\n";
cout << "Enter maximum size of quiue: ";
int qs;
cin >> qs;

Queue line(qs);

cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;

long cyclelimit = MIN_PER_HR * hours;

//cout << "Enter the average number of customers per hour: ";
double perhour;
//cin >> perhour;
double min_per_cust;

Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;
long double average_wait = 0;
for (long mpc = 0; mpc < LONG_MAX; mpc++)
{
average_wait = 0;

for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(mpc))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle);
line.enqueue(temp);
}
}

if (wait_time <= 0 && !line.isempty())
{
line.dequeue(temp);
wait_time = temp.ptime();
line_wait += cycle - temp.when();
served++;
}

if (wait_time > 0)
wait_time--;

sum_line += line.queuecount();
}

if (served)
average_wait = (double)line_wait / served;


if (served && average_wait <= 1.00)
{
perhour = mpc;
break;
}
}

if (customers > 0)
{
cout << "customers accepted: " << customers << endl;
cout << " customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout << "average queue size: ";
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout << (double) sum_line / cyclelimit << endl;
cout << " average wait time: "
<< (double) line_wait / served << "minutes\n";
cout << "the average number of customers per hour" << perhour <<
endl;
}
else
cout << "No customers!\n";
}

bool newcustomer(double x)
{
return ( rand() * x / RAND_MAX < 1);
}
 
R

Robert

dear sir:
I have read the c++ primer plus 5 book, and do the
chapter 12 programming exercises question 5, but the average_wait have
negative result. I don't understand why? here is the code,
thank you anyone replay me.:)

/************************* customer.h *************************/
#ifndef CUSTOMER_H_
#define CUSTOMER_H_

class Customer
{
private:
long arrive; //arrival time for customer
int processtime; //process time for customer
public:
Customer() { arrive = processtime = 0; }
void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime; }

};

#endif

/************************* customer.cpp *************************/
#include <iostream>
#include "customer.h"

//when is the time at which the customer arrives
//the arrival times is set to when and the processing
//time set to a random value in the range 1 - 3
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;

}

/************************* queue.h *************************/
#ifndef QUEUE_H_
#define QUEUE_H_

#include "customer.h"

namespace QUEUE
{
typedef Customer Item;

class Queue
{
private:
//class scope definitions
//Node is a nested structure definition local to this class
struct Node { Item item; struct Node * next;};
enum { Q_SIZE = 10};
//private class members
Node * front; //pointer to front of Queue
Node * rear; //pointer to rear of Queue
int items; //current number of items in Queue
const int qsize; //maximum number of items in Queue
//preemptive definitiond to prevent public copying
Queue(const Queue & q):qsize(0) { }
Queue & operator=(const Queue & q) { return *this;}
public:
Queue(int qs = Q_SIZE); //create queue with a qs limit
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item & item); //add item to end
bool dequeue(Item & item); //remove item from front
};

}

#endif

/************************* queue.cpp *************************/
#include "queue.h"
#include <cstdlib>

namespace QUEUE
{
//Queue methods
Queue::Queue(int qs):qsize(qs)
{
front = rear = NULL;
items = 0;
}

Queue::~Queue()
{
Node * temp;
while (front != NULL) //while queue is not yet empty
{
temp = front; //save address of front item
front = front->next;//reset pointer to next item
delete temp; //delete former front
}
}

bool Queue::isempty() const
{
return items == 0;
}

bool Queue::isfull() const
{
return items == qsize;
}

int Queue::queuecount() const
{
return items;
}

//Add item to queue

bool Queue::enqueue(const Item & item)
{
if (isfull())
return false;

Node * add = new Node; //create node

if (add == NULL)
return false; //quit if none available

add->item = item; //set node pointers
add->next = NULL;
items++;

if (front == NULL) //if queue is empty,
front = add; //place item at front
else
rear->next = add; //else place at rear

rear = add; //have rear point to new node
return true;
}

//Place frony item into item variable and remove fron a queue
bool Queue::dequeue(Item &item)
{
if (front == NULL)
return false;

item = front->item; //set item to first item in queue
items--;
Node * temp = front;//save location of first item
front = front->next;//reset front to next item
delete temp; //delete former first item

if ( items == 0)
rear = NULL;

return true;
}

}

/************************* main.cpp *************************/
#include <iostream>
#include <cstdlib> //for rand() and srand()
#include <ctime> //for time()

#include "queue.h"
bool newcustomer(double x); //is there a new customer?

void main()
{
const int MIN_PER_HR = 60;
using namespace std;
using namespace QUEUE;

srand(static_cast<unsigned int>(time(0)));

cout << "Case Study: Bank of Healther Automatic Teller\n";
cout << "Enter maximum size of quiue: ";
int qs;
cin >> qs;

Queue line(qs);

cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;

long cyclelimit = MIN_PER_HR * hours;

//cout << "Enter the average number of customers per hour: ";
double perhour;
//cin >> perhour;
double min_per_cust;

Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;
long double average_wait = 0;
for (long mpc = 0; mpc < LONG_MAX; mpc++)
{
average_wait = 0;

for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(mpc))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle);
line.enqueue(temp);
}
}

if (wait_time <= 0 && !line.isempty())
{
line.dequeue(temp);
wait_time = temp.ptime();
line_wait += cycle - temp.when();
served++;
}

if (wait_time > 0)
wait_time--;

sum_line += line.queuecount();
}

if (served)
average_wait = (double)line_wait / served;

if (served && average_wait <= 1.00)
{
perhour = mpc;
break;
}
}

if (customers > 0)
{
cout << "customers accepted: " << customers << endl;
cout << " customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout << "average queue size: ";
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout << (double) sum_line / cyclelimit << endl;
cout << " average wait time: "
<< (double) line_wait / served << "minutes\n";
cout << "the average number of customers per hour" << perhour <<
endl;
}
else
cout << "No customers!\n";

}

bool newcustomer(double x)
{
return ( rand() * x / RAND_MAX < 1);

}

The program execute result as bellow:
--------------------------------------------
Case Study: Bank of Healther Automatic Teller
Enter maximum size of quiue: 10
Enter the number of simulation hours: 100
customers accepted: 10821
customers served: 10821
turnaways: 6100
average queue size: 24.42
average wait time: -0.88minutes
the average number of customers per hour3.00
 
R

Robert

I fix the bug by myself. the main.cpp change as bellow:
while (average_wait > 1)
{
perhour = rand() % 50 + 1;
min_per_cust = MIN_PER_HR / perhour;
line_wait = 0;

for (long cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(min_per_cust))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle);
line.enqueue(temp);
}
}

if (wait_time <= 0 && !line.isempty())
{
line.dequeue(temp);
wait_time = temp.ptime();
line_wait += cycle - temp.when();
if (line_wait < 0 )
{
cout << "line_wait overfolw " << endl;
break;
}
served++;
}

if (wait_time > 0)
wait_time--;

sum_line += line.queuecount();
}

while (!line.isempty())
line.dequeue(temp);


if (served)
{
if ( line_wait < 0 || served < 0 )
int t = 100;
average_wait = (double)line_wait / served;
cout << " line wait: " << line_wait << endl;
cout << " customers served: " << served << endl;
cout << " average wait time: " << average_wait << "minutes\n";
}

//cout << " average wait time: " << average_wait << endl;

served = 0;
turnaways = 0;
sum_line = 0;
wait_time = 0;
line_wait = 0;
}

if (customers > 0)
{
cout << "customers accepted: " << customers << endl;
cout << " customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout << "average queue size: ";
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout << (double) sum_line / cyclelimit << endl;
cout << " average wait time: " << average_wait << "minutes\n";
cout << "the average number of customers per hour" << perhour <<
endl;
}
else
cout << "No customers!\n";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top