Queues. Help required in project

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

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

 
1
  #11
Jun 4th, 2008
It's about time you read the intro threads and figured out how to use code tags.
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
  #12
Jun 5th, 2008
oh..i'm sorry..i didnt know that would take care in future
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
  #13
Jun 7th, 2008
so any sugesstions about the logic please?
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
  #14
Jun 7th, 2008
Well apart from the formatting being a mess, what else would you like us to suggest?

A couple of things:

1. Your 'counter' functions should not be friends. Instead, provide a member function like isEmpty(), so you can do things like if ( q1.isEmpty() )

2. Using clock() means you're measuring processor time, not time according to the clock on the wall. Because this is only a simulation, you don't need to synchronise with the real world time.
So you could just provide a member function like
  1. queue::tick ( void ) {
  2. myInternalClock++;
  3. // anything else time related, like dropping an expired entry off the queue
  4. }
You'd call this method from main(), just like any other member function.
If later, you do need to sync with the real world, then main() can do that and all your queue logic remains unaffected.

3. An array of queues (not q1, q2, q3) may be easier to manage.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Queues. Help required in project

 
0
  #15
Jun 7th, 2008
Just one tip: you don't need clock(). You counting virtual time, so you can represent this as integer. It's easier to add and calculate average queue time that way.

Opps sorry Salem already wrote that.
Last edited by Cybulski; Jun 7th, 2008 at 7:35 am.
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
  #16
Jun 13th, 2008
i cannot use an integer..then i wont be able to automatically queue out a person after a particular time...maybe i shud try out the
  1. time();
function..and yes the teacher also hinted about using the
  1. rand();
to generate random time for each person to be queued out..i dunno how to work that.
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
  #17
Jun 13th, 2008
  1. #include<iostream>
  2. #include<time>
  3. #include<stdlib>
  4. #include<stdio>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct node
  10. {
  11. node *next;
  12. long time_start,time_total,time_compare;
  13. };
  14.  
  15. class queue
  16. { private:
  17. node *head;
  18. node *tail;
  19. node n;
  20. long time_global;
  21.  
  22.  
  23. public:
  24. int count;
  25. queue();
  26. void addnode();
  27. void print();
  28. void dequeue();
  29. friend int choose_counter(queue&,queue&,queue&,queue&);
  30. friend int choose_counter1(queue&,queue&,queue&);
  31. };
  32.  
  33.  
  34. queue::queue()
  35. { head=NULL;
  36. tail=NULL;
  37. count=0;
  38. time_global=time(NULL);
  39.  
  40.  
  41.  
  42. }
  43.  
  44. void queue::addnode()
  45. { if(head==NULL && tail==NULL)
  46. {
  47. node *person=new node;
  48. tail->next=NULL;
  49. time_compare=time(NULL)+rand()%10;
  50. count++;
  51. }
  52.  
  53. else
  54. {
  55. node *person=new node;
  56. tail->next=person;
  57. tail=tail->next;
  58. tail->next=NULL:
  59. count++;
  60. }
  61.  
  62. if (head->time_compare==time_global)
  63. {dequeue();}
  64. }
  65.  
  66.  
  67. void queue::print()
  68. { cout<<"STATUS OF QUEUES:"<<endl;
  69.  
  70.  
  71. }
  72.  
  73.  
  74. void queue::dequeue()
  75. { node *temp;
  76. temp=head;
  77. head=head->next;
  78. count--;
  79.  
  80. if(count==0)
  81. {head=tail=NULL; }
  82.  
  83. else
  84. {
  85. srand(time(NULL));
  86. head->time_compare=time(NULL)+rand()%10;
  87. head->time_total=time_compare-time_start;
  88. }
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
  95. int main()
  96. { queue q1,q2,q3,q4;
  97.  
  98.  
  99. char choice;
  100.  
  101. cout<<"PLEASE CHOSE FROM THE FOLLOWING MENU:"<<endl<<endl<<"1. To enter a male
  102.  
  103. customer, press 'M'."<<endl<<"2. To enter a female customer, press 'F'."<<endl<<"3. To exit
  104.  
  105. press 'X'."<<endl;
  106.  
  107. cin>>choice;
  108.  
  109. switch(choice)
  110. {
  111. case 'M':
  112. {int c=choose_counter(q1,q2,q3,q4);
  113. if(c==0)
  114. {q1.addnode();}
  115. else if(c==1)
  116. {q2.addnode();}
  117. else if(c==2)
  118. {q3.addnode();}
  119. else if(c==3)
  120. {q4.addnode();}
  121. else
  122. { int d=choose_counter1(q1,q2,q3);
  123. if (d=1)
  124. {q1.addnode();}
  125. else if(d=2)
  126. {q2.addnode();}
  127. else
  128. {q3.addnode();}
  129. }
  130.  
  131.  
  132. break;}
  133.  
  134. case 'F':
  135. {
  136. int c=choose_counter(q4,q2,q3,q1);
  137. if(c==0)
  138. {q4.addnode();}
  139. else if(c==1)
  140. {q2.addnode();}
  141. else if(c==2)
  142. {q3.addnode();}
  143. else if(c==3)
  144. {q1.addnode();}
  145. else
  146. { q4.addnode();}
  147. break;
  148. }
  149.  
  150. case 'X':
  151. { exit(0);}
  152.  
  153. }
  154.  
  155.  
  156. return 0;
  157.  
  158. }
  159.  
  160.  
  161. int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4)
  162. { if(obj1.head==NULL)
  163. return 0;
  164. else if(obj2.head==NULL)
  165. return 1;
  166. else if(obj3.head==NULL)
  167. return 2;
  168. else if(obj4.head==NULL)
  169. return 3;
  170. else
  171. return 5;
  172. }
  173.  
  174.  
  175. int choose_counter1(queue& obj1,queue& obj2,queue& obj3)
  176. {if(obj1.count<obj2.count && obj2.count<obj3.count)
  177. return 1;
  178. else if(obj2.count<obj1.count && obj2.count<obj3.count)
  179. return 2;
  180. else
  181. return 3;}


what i want to do is take the current time using time() as the person enters. and if that person is first in the queue, i-e head==NULL, a standard set time should be added to that person's time and when it becomes equal to global time, the person dequeues. for any other person entering, i randomly generate time and then dequeue that person after comparing..i tried to put in the random function but i doubt the correctness..secondly i cnt code up this logic..stuck at it

another problem is that when the control is gone to the create node function, how do another person enter at that time? should i use the
  1. while(!kbhit())
statement? when a key is pressed, the control will go bak to main..i dunno how to implement that in my class please help..im running almost outa tym
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: Jun 2008
Posts: 1
Reputation: Niiitian is an unknown quantity at this point 
Solved Threads: 0
Niiitian Niiitian is offline Offline
Newbie Poster

Re: Queues. Help required in project

 
0
  #18
Jun 13th, 2008
i Suggest that u visit one of my collegue,he can definetly help u is this regard ,infact he s PHD,,,Dr hammad,,,his office is in the faculty office..
best of luck
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
  #19
Jun 13th, 2008
its the project he has given..he wont correct my code ofcoarse..and u r? please sumbody see this code n help me out
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: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 306
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Queues. Help required in project

 
0
  #20
Jun 13th, 2008
Here's your own code properly indented :
  1.  
  2. //indented code
  3.  
  4.  
  5. #include<iostream>
  6. #include<time>
  7. #include<stdlib>
  8. #include<stdio>
  9.  
  10. using namespace std;
  11.  
  12. struct node
  13. {
  14. node *next;
  15. long time_start,time_total,time_compare;
  16. };
  17.  
  18. class queue
  19. {
  20. private:
  21. node *head;
  22. node *tail;
  23. node n;
  24. long time_global;
  25.  
  26.  
  27. public:
  28. int count;
  29. queue();
  30. void addnode();
  31. void print();
  32. void dequeue();
  33. friend int choose_counter(queue&,queue&,queue&,queue&);
  34. friend int choose_counter1(queue&,queue&,queue&);
  35. };
  36.  
  37.  
  38. queue::queue()
  39. {
  40. head=NULL;
  41. tail=NULL;
  42. count=0;
  43. time_global=time(NULL);
  44. }
  45.  
  46. void queue::addnode()
  47. {
  48. if(head==NULL && tail==NULL)
  49. {
  50. node *person=new node;
  51. tail->next=NULL;
  52. time_compare=time(NULL)+rand()%10;
  53. count++;
  54. }
  55. else
  56. {
  57. node *person=new node;
  58. tail->next=person;
  59. tail=tail->next;
  60. tail->next=NULL:
  61. count++;
  62. }
  63.  
  64. if (head->time_compare==time_global)
  65. dequeue();
  66. }
  67.  
  68. void queue::print()
  69. {
  70. cout<<"STATUS OF QUEUES:"<<endl;
  71. }
  72.  
  73.  
  74. void queue::dequeue()
  75. {
  76. node *temp;
  77. temp=head;
  78. head=head->next;
  79. count--;
  80.  
  81. if(count==0)
  82. head=tail=NULL;
  83.  
  84.  
  85. else
  86. {
  87. srand(time(NULL));
  88. head->time_compare=time(NULL)+rand()%10;
  89. head->time_total=time_compare-time_start;
  90. }
  91.  
  92.  
  93. }
  94.  
  95. int main()
  96. {
  97. queue q1,q2,q3,q4;
  98. char choice;
  99. cout<<"PLEASE CHOSE FROM THE FOLLOWING MENU:"<<endl
  100. <<endl<<"1. To enter a male customer, press 'M'."
  101. <<endl<<"2. To enter a female customer, press 'F'."
  102. <<endl<<"3. To exit press 'X'."<<endl;
  103.  
  104. cin>>choice;
  105.  
  106. switch(choice)
  107. {
  108. case 'M':
  109. {
  110. int c=choose_counter(q1,q2,q3,q4);
  111. if(c==0)
  112. q1.addnode();
  113. else if(c==1)
  114. q2.addnode();
  115. else if(c==2)
  116. q3.addnode();
  117. else if(c==3)
  118. q4.addnode();
  119. else
  120. int d=choose_counter1(q1,q2,q3);
  121. if (d=1)
  122. q1.addnode();
  123. else if(d=2)
  124. q2.addnode();
  125. else
  126. q3.addnode();
  127. }
  128. break;
  129. }
  130.  
  131. case 'F':
  132. {
  133. int c=choose_counter(q4,q2,q3,q1);
  134. if(c==0)
  135. q4.addnode();
  136. else if(c==1)
  137. q2.addnode();
  138. else if(c==2)
  139. q3.addnode();
  140. else if(c==3)
  141. q1.addnode();
  142. else
  143. q4.addnode();
  144. break;
  145. }
  146.  
  147. case 'X':
  148. {
  149. exit(0);
  150. }
  151.  
  152. }
  153.  
  154.  
  155. return 0;
  156.  
  157. }
  158.  
  159.  
  160. int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4)
  161. {
  162. if(obj1.head==NULL)
  163. return 0;
  164. else if(obj2.head==NULL)
  165. return 1;
  166. else if(obj3.head==NULL)
  167. return 2;
  168. else if(obj4.head==NULL)
  169. return 3;
  170. else
  171. return 5;
  172. }
  173.  
  174. int choose_counter1(queue& obj1,queue& obj2,queue& obj3)
  175. {
  176. if(obj1.count<obj2.count && obj2.count<obj3.count)
  177. return 1;
  178. else if(obj2.count<obj1.count && obj2.count<obj3.count)
  179. return 2;
  180. else
  181. return 3;
  182. }

There are quite a lot of things wrong with this code:

It's starts with your include files:
  1. #include<iostream>
  2. #include<time>
  3. #include<stdlib>
  4. #include<stdio>
Change it to:
  1. #include<iostream>
  2. #include<ctime>
  3. #include<stdlib.h>
  4. #include<stdio.h>


line 125-129, you have an extra bracket
            else
                q3.addnode();
        } <--- delete this one
        break;
    }


line 121-123
  1. if (d=1)
  2. q1.addnode();
  3. else if(d=2)
I guess you meant == instead of =? Remember: = != ==.
And where did the 'd' come from? It's never delcared?


Line 52:
time_compare=time(NULL)+rand()%10;
time_compare is never declared


line 60:
tail->next=NULL: <-- change this : to a ;

line 86-90:
  1. {
  2. srand(time(NULL));
  3. head->time_compare=time(NULL)+rand()%10;
  4. head->time_total=time_compare-time_start;
  5. }
time_compare and time_start are never declared



etc. You should first get this to compile. Look careful at the compiler-errors and warning you get and try to fix it one problem at a time.

Good luck!
Last edited by niek_e; Jun 13th, 2008 at 9:09 am.
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