137k views
5 votes
Implement Hot Potato gameA group of people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After X passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins.For example:if X = 0 and N = 5, players are eliminated in order, and player 5 winsIf X = 1 and N = 5, the order of elimination is 2, 4, 1, 5.Write a program for general values of X and N.Ask a user for number of people and number of passesTo speed up input, debugging, store names of the people in a file.Output number and the name of a person being eliminatedOutput number and the name of the winnerAllow user to play the game as many times as user wantsMake sure each function has a description, post and pre conditions

1 Answer

7 votes

Answer:

Check the explanation

Step-by-step explanation:

#include <iostream>

using namespace std;

class Circular{

public:

int key;

Circular *next;

Circular* prev;

};

int main()

{

int N,M;

cout<<"Enter the value of N: ";

cin>>N;

cout<<"Enter the value of M: ";

cin>>M;

Circular *c;

c=new Circular[N];

for(int i=0;i<N;i++)

{

(c+i)->key=i+1;

if(i==N-1) (c+i)->next=c;

else (c+i)->next=(c+i+1);

if(i==0) (c+i)->prev=(c+N-1);

else (c+i)->prev=(c+i-1);

}

Circular *current=c;

cout<<"Order of elimination are: ";

for(int i=1;i<N;i++)

{

for(int j=1;j<=M;j++)

{

current=current->next;

}

cout<<current->key<<" ";

current=current->next;

current->prev=((current->prev)->prev);

(current->prev)->next=current;

}

cout<<endl<<"Winner is: "<<current->key<<endl;

delete(c);

}

User Santiagozky
by
4.0k points