| | |
Queues. Help required in project
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
hello everyone.
i need a bit of help in this project of mine which is due in 2 weeks.. here's what needs to be done, but i have no idea about the logic
if somebody can help me with the pseudocode or algorithm, i could write the code maself..
Write a program to simulate the working of a utility store. The utility store has three counters for the gents and one for the ladies. At any given time if all the counters are free, then the customer entering the utility store can go to any of the counters. If ladies counter is empty then the gents can use it and similarly if any of the gents counter is empty then the ladies can also use it provided the queues allocated for their gender is occupied). Lets say, the time required to serve any customer is t minutes. If a customer enters the store at time t1 then he should be out of the store at t+t1 provided that there are no queues in front of the counters. In case there are customers being served at all the counters then queues will be maintained in front of each counter and the new customer will go to the shortest queue. In the case of queues, a customer will be out of the store at “t+t1+the time he spent waiting in the queue”.
Our goal is to calculate the average time spent by each customer in the utility store.
Inputs
User should press m/f at any time for telling the program that a male or a female customer has entered in the store.
Outputs
Program should output following
• On each arrival the status of the queues should be updated and displayed
• Should mention the number of customers serviced and average time spent by the customers in the store
looking ahead to some help..regards
niitian
i need a bit of help in this project of mine which is due in 2 weeks.. here's what needs to be done, but i have no idea about the logic
if somebody can help me with the pseudocode or algorithm, i could write the code maself..Write a program to simulate the working of a utility store. The utility store has three counters for the gents and one for the ladies. At any given time if all the counters are free, then the customer entering the utility store can go to any of the counters. If ladies counter is empty then the gents can use it and similarly if any of the gents counter is empty then the ladies can also use it provided the queues allocated for their gender is occupied). Lets say, the time required to serve any customer is t minutes. If a customer enters the store at time t1 then he should be out of the store at t+t1 provided that there are no queues in front of the counters. In case there are customers being served at all the counters then queues will be maintained in front of each counter and the new customer will go to the shortest queue. In the case of queues, a customer will be out of the store at “t+t1+the time he spent waiting in the queue”.
Our goal is to calculate the average time spent by each customer in the utility store.
Inputs
User should press m/f at any time for telling the program that a male or a female customer has entered in the store.
Outputs
Program should output following
• On each arrival the status of the queues should be updated and displayed
• Should mention the number of customers serviced and average time spent by the customers in the store
looking ahead to some help..regards
niitian
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
•
•
Join Date: May 2008
Posts: 89
Reputation:
Solved Threads: 9
The key to these and all problems is to not get overwhelmed...break it down into steps, solve first one step, then continually add more functionality....
So first we need to think about how we can keep track of the customers, both male and female, and in what queue they are waiting...We also need to think about how we can keep track of the time they are in the store...
Let's think about the second question first. One (simple) way of doing this is to just use the function GetTickCount(), which I believe is in the <windows.h> header file...(if you have another/better way of doing this, use it). GetTickCount() simply returns the number of milliseconds that have ellapsed since the system (computer) started...
So you create a variable for each customer that stores "time_entered_store" as just returning GetTickCount() at the time they enter...make sense?
Now you need to think about implementing some sort of data structure to encapsulate the time, and the number of customers...I think it would be natural to create a queue data structure for this question (search google)...one for each queue...I would create something like a queue of customers, where each customer structure keeps track of the time they entered and gender, and then the queue itself points to the customers, and keeps track of the number in each queue....
once you have this sort of functionality, adding the extra logic (to determine which queue a customer enters) should not be too difficult...
Anyway, hopefully that helps a bit, gets the ideas flowing...
So first we need to think about how we can keep track of the customers, both male and female, and in what queue they are waiting...We also need to think about how we can keep track of the time they are in the store...
Let's think about the second question first. One (simple) way of doing this is to just use the function GetTickCount(), which I believe is in the <windows.h> header file...(if you have another/better way of doing this, use it). GetTickCount() simply returns the number of milliseconds that have ellapsed since the system (computer) started...
So you create a variable for each customer that stores "time_entered_store" as just returning GetTickCount() at the time they enter...make sense?
Now you need to think about implementing some sort of data structure to encapsulate the time, and the number of customers...I think it would be natural to create a queue data structure for this question (search google)...one for each queue...I would create something like a queue of customers, where each customer structure keeps track of the time they entered and gender, and then the queue itself points to the customers, and keeps track of the number in each queue....
once you have this sort of functionality, adding the extra logic (to determine which queue a customer enters) should not be too difficult...
Anyway, hopefully that helps a bit, gets the ideas flowing...
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
Alright, I tried making up the logic, couldn’t manage how to write the code though. Having trouble in it, but it’s what I thought could work. Hope to get some help in the issues I’m lacking in.
To manage the time, I came across this function clock( ) in the header file time.h. what it does is that takes the time and assigns it to a clock type variable say start. The when I’m done with the operations, I assign clock( ) to another clock type variable say end, and the difference between both the variables would give the time taken.
Now to maintain the queues, I would create a class queue having four objects. Each object serving as a counter. Three objects for male counters and one for female. Now when a key is pressed, a counter would be allotted.
To allot counters, first check whether the respective counter for each gender is free or not. To do this, a function is called which returns a value if the first node of that particular counter’s queue is null. If it is NULL, then the function to create a node is called. If not, then the remaining 3 queues are checked. If none is empty, then the function for createnode is called for the respective gender. In each node creation, a “count” variable which is a private data member of the class, is incremented, this helps to keep track of the number of people in the queue and helps to chose queues comparing their lengths when none of the queues are empty.
When a node is created, the timer starts, the pointer is moved forward till the node becomes the first node in the queue and then the timer is stopped. After time t = say 60secs, which is the time required to process each customer, the node is popped out of the queue. The total time for the customer is calculated and displayed at that very instant, also mentioning the position of the other customers. The total time is calculated by adding t to the difference of the times when the timer started and stopped.
I think that should do, however I have little idea how to code it. Thought I could do the coding part better, but seem to be struggling with it.
I don’t have too much idea of working with linked lists. I know how to create nodes and pop then out, but how to keep track of them.?? The coding thing seems a mess ryt now
plz help!
To manage the time, I came across this function clock( ) in the header file time.h. what it does is that takes the time and assigns it to a clock type variable say start. The when I’m done with the operations, I assign clock( ) to another clock type variable say end, and the difference between both the variables would give the time taken.
Now to maintain the queues, I would create a class queue having four objects. Each object serving as a counter. Three objects for male counters and one for female. Now when a key is pressed, a counter would be allotted.
To allot counters, first check whether the respective counter for each gender is free or not. To do this, a function is called which returns a value if the first node of that particular counter’s queue is null. If it is NULL, then the function to create a node is called. If not, then the remaining 3 queues are checked. If none is empty, then the function for createnode is called for the respective gender. In each node creation, a “count” variable which is a private data member of the class, is incremented, this helps to keep track of the number of people in the queue and helps to chose queues comparing their lengths when none of the queues are empty.
When a node is created, the timer starts, the pointer is moved forward till the node becomes the first node in the queue and then the timer is stopped. After time t = say 60secs, which is the time required to process each customer, the node is popped out of the queue. The total time for the customer is calculated and displayed at that very instant, also mentioning the position of the other customers. The total time is calculated by adding t to the difference of the times when the timer started and stopped.
I think that should do, however I have little idea how to code it. Thought I could do the coding part better, but seem to be struggling with it.
I don’t have too much idea of working with linked lists. I know how to create nodes and pop then out, but how to keep track of them.?? The coding thing seems a mess ryt now
plz help! here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
When you add a node, I would suggest
Then for each clock tick, all you need to do is
Post your 'list' class if you're having problems with that aspect. Apart from that, your analysis looks OK so far.
node.expire = clock() + t; Then for each clock tick, all you need to do is
if ( clock() >= node.expire ) then remove that node from the list (it will be at the head, or tail) of the list depending on which order you add them.Post your 'list' class if you're having problems with that aspect. Apart from that, your analysis looks OK so far.
Last edited by Salem; May 17th, 2008 at 11:43 am. Reason: tag it before the whining starts
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
exams cameup n the deadline was delayed.now ive got about 6days to do it. just rote code, but have done it just for two counters yet..just want to make the time logic first, but stuck
helpp plz...
also i dunno how to keep track of the number of each person.n then print it..count would only tell the number of ppl in queue
helpp plz...also i dunno how to keep track of the number of each person.n then print it..count would only tell the number of ppl in queue

C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<time.h> struct node { node *next; int time_start,expire; }; class queue { private: node *head; node *tail; int t1; node n; public: int count; queue(); void addnode(); void print(); void dequeue(); }; queue::queue() { head=NULL; tail=NULL; count=0; t1=5; } void queue::addnode() { int T; node *loc=new node; clock_t t=clock(); n.time_start=(t/CLK_TCK); loc->next=NULL; if(head==NULL) {head=loc;} else {tail->next=loc; tail=loc;} n.expire=n.time_start+t1; t=clock(); T=(t/CLK_TCK); if (T==n.expire) { dequeue(); print();} count++; } void queue::print() { cout<<"STATUS OF QUEUES:"<<endl; } void queue::dequeue() { node *temp; temp=head; while(temp->next->next!=NULL) temp=temp->next; delete temp->next; count--; temp->next=NULL; tail=temp; } int main() { queue q1,q2; char gender; cout<<"ENTER YOUR GENDER: (M/F)"<<endl; cin>>gender; switch(gender) { case 'M': {q1.addnode(); break;} case 'F': {q2.addnode(); break; } } return 0; }
Last edited by Ancient Dragon; Jun 4th, 2008 at 6:20 pm. Reason: add code tags
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
okay here's the code after ive sorted the thing about chosing a queue..
stuck with the time logic
stuck with the time logic

C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<time.h> struct node { node *next; int time_start,expire; }; class queue { private: node *head; node *tail; int t1; node n; public: int count; queue(); void addnode(); void print(); void dequeue(); friend int choose_counter(queue&,queue&,queue&,queue&); friend int choose_counter1(queue&,queue&,queue&); }; queue::queue() { head=NULL; tail=NULL; count=0; t1=5; } void queue::addnode() { int T; node *loc=new node; clock_t t=clock(); n.time_start=(t/CLK_TCK); loc->next=NULL; if(head==NULL) {head=loc;} else {tail->next=loc; tail=loc;} n.expire=n.time_start+t1; t=clock(); T=(t/CLK_TCK); if (T==n.expire) { dequeue(); print();} count++; } void queue::print() { cout<<"STATUS OF QUEUES:"<<endl; } void queue::dequeue() { node *temp; temp=head; while(temp->next->next!=NULL) temp=temp->next; delete temp->next; count--; temp->next=NULL; tail=temp; } int main() { queue q1,q2,q3,q4; char gender; cout<<"ENTER YOUR GENDER: (M/F)"<<endl; cin>>gender; switch(gender) { case 'M': {int c=choose_counter(q1,q2,q3,q4); if(c==0) {q1.addnode();} else if(c==1) {q2.addnode();} else if(c==2) {q3.addnode();} else if(c==3) {q4.addnode();} else { int d=choose_counter1(q1,q2,q3); if (d=1) {q1.addnode();} else if(d=2) {q2.addnode();} else {q3.addnode();} } break;} case 'F': { int c=choose_counter(q4,q2,q3,q1); if(c==0) {q4.addnode();} else if(c==1) {q2.addnode();} else if(c==2) {q3.addnode();} else if(c==3) {q1.addnode();} else { q4.addnode();} break; } } return 0; } int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4) { if(obj1.head==NULL) return 0; else if(obj2.head==NULL) return 1; else if(obj3.head==NULL) return 2; else if(obj4.head==NULL) return 3; else return 5; } int choose_counter1(queue& obj1,queue& obj2,queue& obj3) {if(obj1.count<obj2.count && obj2.count<obj3.count) return 1; else if(obj2.count<obj1.count && obj2.count<obj3.count) return 2; else return 3;}
Last edited by Ancient Dragon; Jun 4th, 2008 at 6:20 pm. Reason: add code tags
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Segmentation error
- Next Thread: Class Hierarchy Question
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop developer directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







what is expire? a data member of the structure node?