Queues. Help required in project

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Queues. Help required in project

 
0
  #1
May 14th, 2008
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
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Queues. Help required in project

 
0
  #2
May 14th, 2008
Perhaps begin with a simple program which reacts to single key presses.
Then write another program to implement the basic queue data type.

Then come back to us.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 89
Reputation: n1337 is on a distinguished road 
Solved Threads: 9
n1337 n1337 is offline Offline
Junior Poster in Training

Re: Queues. Help required in project

 
0
  #3
May 14th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #4
May 15th, 2008
thankyou..i'm just gonna try it out this weekend, lets see
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #5
May 17th, 2008
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!
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Queues. Help required in project

 
0
  #6
May 17th, 2008
When you add a node, I would suggest
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #7
May 17th, 2008
ermm what is expire? a data member of the structure node?
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Queues. Help required in project

 
0
  #8
May 17th, 2008
It's whatever you want to call the member of your struct/class which indicates when someone will leave the queue.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #9
Jun 4th, 2008
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

  1. #include<iostream.h>
  2. #include<time.h>
  3.  
  4.  
  5. struct node
  6. {
  7. node *next;
  8. int time_start,expire;
  9. };
  10.  
  11. class queue
  12. { private:
  13. node *head;
  14. node *tail;
  15. int t1;
  16. node n;
  17.  
  18.  
  19. public:
  20. int count;
  21. queue();
  22. void addnode();
  23. void print();
  24. void dequeue();
  25. };
  26.  
  27.  
  28. queue::queue()
  29. { head=NULL;
  30. tail=NULL;
  31. count=0;
  32. t1=5;
  33.  
  34. }
  35.  
  36. void queue::addnode()
  37. { int T;
  38.  
  39. node *loc=new node;
  40.  
  41. clock_t t=clock();
  42. n.time_start=(t/CLK_TCK);
  43.  
  44. loc->next=NULL;
  45.  
  46. if(head==NULL)
  47. {head=loc;}
  48.  
  49. else
  50. {tail->next=loc;
  51. tail=loc;}
  52.  
  53. n.expire=n.time_start+t1;
  54.  
  55. t=clock();
  56. T=(t/CLK_TCK);
  57.  
  58. if (T==n.expire)
  59. { dequeue();
  60. print();}
  61.  
  62. count++;
  63.  
  64. }
  65.  
  66.  
  67. void queue::print()
  68. { cout<<"STATUS OF QUEUES:"<<endl;
  69.  
  70.  
  71. }
  72. void queue::dequeue()
  73. { node *temp;
  74. temp=head;
  75. while(temp->next->next!=NULL)
  76. temp=temp->next;
  77. delete temp->next;
  78. count--;
  79. temp->next=NULL;
  80. tail=temp;
  81. }
  82.  
  83. int main()
  84. { queue q1,q2;
  85.  
  86. char gender;
  87.  
  88. cout<<"ENTER YOUR GENDER: (M/F)"<<endl;
  89. cin>>gender;
  90.  
  91. switch(gender)
  92. { case 'M':
  93.  
  94. {q1.addnode();
  95.  
  96. break;}
  97.  
  98. case 'F':
  99. {q2.addnode();
  100. break;
  101. }
  102.  
  103. }
  104.  
  105.  
  106. return 0;
  107.  
  108. }
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: niitian is an unknown quantity at this point 
Solved Threads: 0
niitian niitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #10
Jun 4th, 2008
okay here's the code after ive sorted the thing about chosing a queue..

stuck with the time logic

  1. #include<iostream.h>
  2. #include<time.h>
  3.  
  4.  
  5. struct node
  6. {
  7. node *next;
  8. int time_start,expire;
  9. };
  10.  
  11. class queue
  12. { private:
  13. node *head;
  14. node *tail;
  15. int t1;
  16. node n;
  17.  
  18.  
  19. public:
  20. int count;
  21. queue();
  22. void addnode();
  23. void print();
  24. void dequeue();
  25. friend int choose_counter(queue&,queue&,queue&,queue&);
  26. friend int choose_counter1(queue&,queue&,queue&);
  27. };
  28.  
  29.  
  30. queue::queue()
  31. { head=NULL;
  32. tail=NULL;
  33. count=0;
  34. t1=5;
  35.  
  36. }
  37.  
  38. void queue::addnode()
  39. { int T;
  40.  
  41. node *loc=new node;
  42.  
  43. clock_t t=clock();
  44. n.time_start=(t/CLK_TCK);
  45.  
  46. loc->next=NULL;
  47.  
  48. if(head==NULL)
  49. {head=loc;}
  50.  
  51. else
  52. {tail->next=loc;
  53. tail=loc;}
  54.  
  55. n.expire=n.time_start+t1;
  56.  
  57. t=clock();
  58. T=(t/CLK_TCK);
  59.  
  60. if (T==n.expire)
  61. { dequeue();
  62. print();}
  63.  
  64. count++;
  65.  
  66. }
  67.  
  68.  
  69. void queue::print()
  70. { cout<<"STATUS OF QUEUES:"<<endl;
  71.  
  72.  
  73. }
  74. void queue::dequeue()
  75. { node *temp;
  76. temp=head;
  77. while(temp->next->next!=NULL)
  78. temp=temp->next;
  79. delete temp->next;
  80. count--;
  81. temp->next=NULL;
  82. tail=temp;
  83. }
  84.  
  85.  
  86. int main()
  87. { queue q1,q2,q3,q4;
  88.  
  89. char gender;
  90.  
  91. cout<<"ENTER YOUR GENDER: (M/F)"<<endl;
  92. cin>>gender;
  93.  
  94. switch(gender)
  95. {
  96. case 'M':
  97. {int c=choose_counter(q1,q2,q3,q4);
  98. if(c==0)
  99. {q1.addnode();}
  100. else if(c==1)
  101. {q2.addnode();}
  102. else if(c==2)
  103. {q3.addnode();}
  104. else if(c==3)
  105. {q4.addnode();}
  106. else
  107. { int d=choose_counter1(q1,q2,q3);
  108. if (d=1)
  109. {q1.addnode();}
  110. else if(d=2)
  111. {q2.addnode();}
  112. else
  113. {q3.addnode();}
  114. }
  115.  
  116.  
  117. break;}
  118.  
  119. case 'F':
  120. {
  121. int c=choose_counter(q4,q2,q3,q1);
  122. if(c==0)
  123. {q4.addnode();}
  124. else if(c==1)
  125. {q2.addnode();}
  126. else if(c==2)
  127. {q3.addnode();}
  128. else if(c==3)
  129. {q1.addnode();}
  130. else
  131. { q4.addnode();}
  132. break;
  133. }
  134.  
  135. }
  136.  
  137.  
  138. return 0;
  139.  
  140. }
  141.  
  142.  
  143. int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4)
  144. { if(obj1.head==NULL)
  145. return 0;
  146. else if(obj2.head==NULL)
  147. return 1;
  148. else if(obj3.head==NULL)
  149. return 2;
  150. else if(obj4.head==NULL)
  151. return 3;
  152. else
  153. return 5;
  154. }
  155.  
  156.  
  157. int choose_counter1(queue& obj1,queue& obj2,queue& obj3)
  158. {if(obj1.count<obj2.count && obj2.count<obj3.count)
  159. return 1;
  160. else if(obj2.count<obj1.count && obj2.count<obj3.count)
  161. return 2;
  162. else
  163. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC