473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what the result of average_time have negative

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);
}

Sep 6 '07 #1
2 1447
On 9 6 , 5 56 , Robert <luoly...@gmail.comwrote:
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

Sep 6 '07 #2
I fix the bug by myself. the main.cpp change as bellow:
/************************* 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;
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";
}
}

Sep 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Elaine Jackson | last post by:
Is there a function that takes a number with binary numeral a1...an to the number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice versa? (For example, the function's value at...
8
by: Steven T. Hatton | last post by:
This is from ISO/IEC 14882:2003(E) §5.6 ¶4 <quote> The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the...
5
by: pankaj tiwary | last post by:
Hello experts, please consider the code fragment:- #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array)) int array = {23,34,12,17,204,99,16}; int main() {
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
48
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The...
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
26
by: Shraddha | last post by:
I got a small programm on net...but there was different initialisation that I saw... It was as follows: for ( i = ~0 ; i ; i>>=1); right shift is ok..But what is meaned by " i = ~0 "...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.