473,836 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Game with queue

Rooro
16 New Member
Hello everyone

i'm working on :


"

Familiar childhood games such as hide and Go Seek and Tag involve determining the player who is to be "It". One method has the players stand in a circle while one of the players recites some rhyme to count off players, eliminating every nth player, where n depends on the number of the syllables in the counting rhyme. This process stops when only one player, the one to be "It" remains.

This process can be simulated in a queue, even though we usually envision a queue as a line rather than a circle of items. (It is typical for simulations to produce a realistic effect without mirroring the exact details of the actual process).

Suppose there are five players, and the counting rhyme has 3 syllables (e.g. " You are out."). The following diagram depicts the elimination process as simulated by a queue:


Code a program that uses the Queue ADT to simulate the game described above. The program should take as input the list of players and the number of syllables in the rhyme. It should display the name of the person to be "It".


"


i have fished writting the heder file and the implementation file


Expand|Select|Wrap|Line Numbers
  1. //heder
  2.  
  3. #ifndef QUEUE
  4. #define QUEUE
  5.  
  6. const int QUEUE_CAPACITY = 128;
  7.  
  8. typedef int QueueElement;
  9.  
  10.  
  11. class Queue
  12.  
  13. {    
  14.    public:
  15.  
  16.        Queue();
  17.  
  18.        bool empty() const;
  19.  
  20.        void enqueue(const QueueElement &value);
  21.  
  22.        void display (ostream &out) const;
  23.  
  24.        QueueElement front()  const;
  25.  
  26.        void dequeue();
  27.  
  28.     private:
  29.  
  30.         int      myFront, 
  31.  
  32.             myBack;
  33.  
  34.         QueueElement   myArray  [QUEUE_CAPACITY];
  35.  
  36. };
  37. #endif
  38.  
  39.  




Expand|Select|Wrap|Line Numbers
  1.  
  2. //implementation file
  3.  
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. #include "Queo.h"
  9.  
  10. Queue::Queue()
  11.     { 
  12.         myFront=0;
  13.  
  14.         myBack=0;
  15.      }
  16.  
  17. bool Queue::empty() const
  18.  
  19. {
  20.  
  21. return(myFront==myBack);
  22.  
  23.  
  24.     }
  25.  
  26.  
  27.  
  28.  
  29. void Queue::enqueue(const QueueElement &value) 
  30. {
  31.  
  32.  
  33.         int newBack = (myBack + 1 ) % QUEUE_CAPACITY;
  34.  
  35.  
  36.         if (newBack !=myFront)
  37. {
  38.             myArray[myBack]=value;
  39.  
  40.         myBack = newBack;
  41.  
  42. }
  43.  
  44. else
  45.  
  46. {
  47.     cerr<<"***Queue is full***\n";
  48.  
  49.     exit(1);
  50.  
  51. }
  52.  
  53.  
  54.     }
  55.  
  56. void Queue::display (ostream &out) const
  57. {
  58.  
  59.  
  60. for(int i = myFront;i !=myBack;i=(i+1)% QUEUE_CAPACITY)
  61.  
  62. out<<myArray[i]<<"    ";
  63.  
  64. cout<<endl;
  65.  
  66.     } 
  67.  
  68.  
  69. QueueElement Queue::front()  const
  70.  
  71.  
  72.  
  73.  {
  74.  
  75.      if (!empty())
  76.  
  77.          return(myArray[myFront]);
  78.  
  79.      else {
  80.  
  81.          cerr<<"*** Queue is empty --return garbage -- ***\n";
  82.  
  83.              QueueElement garbage;
  84.  
  85.          return garbage;
  86.      }
  87.  
  88.  
  89.  
  90.  }
  91.  
  92.        void Queue::dequeue()
  93.  
  94.        {
  95.  
  96. if (!empty())
  97.  
  98. myFront = (myFront + 1 ) % QUEUE_CAPACITY;
  99.  
  100. else 
  101.  
  102. cerr <<"*** Queue is empty - can not remove a value ***\n ";
  103.  
  104.        }
  105.  


as start with the main function :



Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. #include "Queo.h"
  7.  
  8.  
  9. int main ()
  10.  
  11.  
  12. {
  13.  
  14.     int num;
  15.  
  16.     Queue c;
  17.  
  18.     string players;
  19.  
  20. cout<<"Please enter the number of players : ";
  21.  
  22. cin>>num;
  23.  
  24. cout<<"Enter the name of the playeres: ";
  25.  
  26. for (int i=1;i<num;i++)
  27.  
  28. {
  29.  
  30. cin>>players;
  31. c.enqueue(i);
  32.  
  33.  
  34. }
  35.  
  36.  
  37.  
  38. do 
  39.  
  40. c.dequeue();
  41.  
  42. while(num!=1);
  43.  
  44. ///>>>while ... this is wrong i know but it is as logic 
  45.  
  46.  
  47.  
  48.  
  49.  
  50. return(0);
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
my probleme is on the main function , i can not get the idea clearly to do it ..

i want to unduestand how can i do it ?
Nov 30 '06 #1
5 3116
DeMan
1,806 Top Contributor
I suspect you probably want to do something like:
Expand|Select|Wrap|Line Numbers
  1. while(eenie_meanie_has_more_syllables) //obviously edit conditional
  2. {
  3.   c.enQueue(c.front()); //put the front element in the back of the list, so he can come around again
  4.   c.deQueue();  //remove the front element, since he is now at the back
  5. }
  6.  
At the end of the loop the element at c.front() is your element (well actually the one atr the back is, since when we selected 'out' we still moved him around to the back of the queue, but if this is an issue we fix this by making our loop one smaller ie make sure the condition in the while loops for syllables -1.

Hope this helps
Nov 30 '06 #2
Rooro
16 New Member
DeMan , that is fine ..

but i changed my idea to :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<iostream>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. #include "Queue.h"
  9.  
  10.  
  11. int main ()
  12.  
  13.  
  14. {
  15.  
  16.     int num,num_rhyme;
  17.  
  18.     Queue c;
  19.  
  20.     string players,newfront;
  21.  
  22. cout<<"Please enter the number of players : ";
  23.  
  24. cin>>num;
  25.  
  26. cout<<"Enter the name of the playeres: ";
  27.  
  28. for (int i=1;i<=num;i++)
  29.  
  30. {
  31.  
  32. cin>>players;
  33.  
  34.  
  35. }
  36.  
  37. cout<<"Please enter how many times for the ' rhyme '";
  38.  
  39.  
  40. cin>>num_rhyme;
  41.  
  42. for(int j=num;j>1;j--)
  43.  
  44. {
  45.  
  46.     for (int r=1;r<num_rhyme;r++)
  47.  
  48.  
  49.     {
  50.  
  51. newfront=c.front();
  52.  
  53.         c.dequeue();
  54.  
  55.         c.enqueue(newfront);
  56.  
  57.                 }
  58.  
  59. cout<<c.front()<<endl;
  60.  
  61. c.dequeue();
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69. return(0);
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  

but there is a problem with the implemtation file :(
Dec 1 '06 #3
DeMan
1,806 Top Contributor
Did you want help with the problem? If so pls add a little more detail....
Dec 1 '06 #4
Rooro
16 New Member
DeMan , I did it :)

Thanks ....
Dec 3 '06 #5
DeMan
1,806 Top Contributor
Well Done!!!!
Dec 3 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2791
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
15
4500
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and server have 17 fps, while when playing in server window, server has 44 fps while client ...
9
2504
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over ride just add aditional code ontop of the current class or completely over ride it in vb? I am use to C++ this is the first inherited thing I've done in VB.NET... I'm a little unsure of diffrences, could someone explain this to me some? thanks!
3
5163
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main': G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to `Queue<char>::Queue()' G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In function `Z10do_commandcR5QueueIcE':
2
2969
by: lavender | last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10. Where is the wrong in my code? Why it cannot store up to 10 items? Output from my code: Enter you choice: 1 Enter ID document to print : 21 Enter you choice: 1 Enter ID document to print : 22
3
2043
by: jrpfinch | last post by:
I have a script which is based on the following code. Unfortunately, it only works on Python 2.3 and not 2.5 because there is no esema or fsema attribute in the 2.5 Queue. I am hunting through the Queue.py code now to try to figure out how to make it work in 2.5, but as I am a beginner, I am having difficulty and would appreciate your help. Many thanks Jon
4
4602
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the length of my queue. I started getting compilation errors when I included a length function. <code> template<class ItemType> void Queue<ItemType>::MakeEmpty() {
2
2937
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include <cassert // for assert #include <new // for bad_alloc using namespace std; //private:{Queue::Queue(const Queue& Q)}
0
2742
by: ecestd | last post by:
I did implement the copy constructor but still have a problem with it. It is not working. What could be wrong? #include "QueueP.h" #include <cassert // for assert #include <new // for bad_alloc #include <iostream> //typedef std::queue<QueueItemTypeQueue; using namespace std; //private:{Queue::Queue(const Queue& Q)}
0
9825
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
9673
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
10859
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
10260
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
9388
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
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4463
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
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.