473,941 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(l ong 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(in t 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::queuecou nt() 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(dou ble x); //is there a new customer?

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

srand(static_ca st<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(mp c))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle) ;
line.enqueue(te mp);
}
}

if (wait_time <= 0 && !line.isempty() )
{
line.dequeue(te mp);
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_wa it / 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_b ase::fixed,ios_ base::floatfiel d);
cout.setf(ios_b ase::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(dou ble x)
{
return ( rand() * x / RAND_MAX < 1);
}

Sep 6 '07 #1
2 1486
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(l ong 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(in t 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::queuecou nt() 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(dou ble x); //is there a new customer?

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

srand(static_ca st<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(mp c))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle) ;
line.enqueue(te mp);
}
}

if (wait_time <= 0 && !line.isempty() )
{
line.dequeue(te mp);
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_wa it / 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_b ase::fixed,ios_ base::floatfiel d);
cout.setf(ios_b ase::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(dou ble 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(dou ble x); //is there a new customer?
void main()
{
const int MIN_PER_HR = 60;
using namespace std;
using namespace QUEUE;
srand(static_ca st<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(mi n_per_cust))
{
if (line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle) ;
line.enqueue(te mp);
}
}

if (wait_time <= 0 && !line.isempty() )
{
line.dequeue(te mp);
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(te mp);
if (served)
{
if ( line_wait < 0 || served < 0 )
int t = 100;
average_wait = (double)line_wa it / 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_b ase::fixed,ios_ base::floatfiel d);
cout.setf(ios_b ase::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
17581
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 18 would be 13 .) I thought this was what the tilde operator (~) did, but when I went to try it I found out that wasn't the case. I discovered by experiment (and verified by looking at the documentation) that the tilde operator takes n to -(n+1)....
8
5297
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 second. If the second operand of / or % is zero the behavior is undefined; oth- erwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnega- tive; if not, the sign of the remainder is implementation-defined)....
5
3113
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
15721
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
31
10370
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 value c. Now, I want to obtain the difference between the two, that is, the value c. Which type must I use for c? I searched around and did not find a definitive answer (not even in the
48
3410
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 "toupper" function imposes a further constrait in that the value passed to it must be representable as a unsigned char. (If C does not require all character values to be positive, then again, this constrait too escapes me... ) Let's say we have the...
6
2374
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; int reversed = 0; while (num != 0) {
26
2019
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
5899
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 right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11112
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6298
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.