943,652 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1460
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 4th, 2008
1

Re: Queues. Help required in project

It's about time you read the intro threads and figured out how to use code tags.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 5th, 2008
0

Re: Queues. Help required in project

oh..i'm sorry..i didnt know that would take care in future
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niitian is offline Offline
13 posts
since Mar 2008
Jun 7th, 2008
0

Re: Queues. Help required in project

so any sugesstions about the logic please?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niitian is offline Offline
13 posts
since Mar 2008
Jun 7th, 2008
0

Re: Queues. Help required in project

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
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 7th, 2008
0

Re: Queues. Help required in project

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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 13th, 2008
0

Re: Queues. Help required in project

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
C++ Syntax (Toggle Plain Text)
  1. time();
function..and yes the teacher also hinted about using the
C++ Syntax (Toggle Plain Text)
  1. rand();
to generate random time for each person to be queued out..i dunno how to work that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niitian is offline Offline
13 posts
since Mar 2008
Jun 13th, 2008
0

Re: Queues. Help required in project

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niitian is offline Offline
13 posts
since Mar 2008
Jun 13th, 2008
0

Re: Queues. Help required in project

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Niiitian is offline Offline
1 posts
since Jun 2008
Jun 13th, 2008
0

Re: Queues. Help required in project

its the project he has given..he wont correct my code ofcoarse..and u r? please sumbody see this code n help me out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niitian is offline Offline
13 posts
since Mar 2008
Jun 13th, 2008
0

Re: Queues. Help required in project

Here's your own code properly indented :
cpp Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<time>
  3. #include<stdlib>
  4. #include<stdio>
Change it to:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Jun 13th, 2008 at 9:09 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is online now Online
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation error
Next Thread in C++ Forum Timeline: Class Hierarchy Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC