Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Suppose there are waiters for serving multiple customers in which 5 waiters have the following id: 101, 102, 103, 104, 105. Each waiter will be assigned with random tip between 0-10$ in each iteration. Waiter with least tip will be removed in each iteration. While considering the above scenario, do the following tasks.
(i)Code the above scheme in C++, insert 0 when initializing circular linked list.
(ii)Sort the Linked List according to tip after removing a waiter.
(iii)Removed waiter should be displayed on console after every iteration.
(iv)Display waiter id with highest tip.

What I have tried:

i am not able to do this task in C++. so please help me out with this code . i will be very gratefull. i have this question for quiz .i want this code urgently.
Posted
Updated 14-Mar-22 14:59pm

I'm in a good mood, so here's an outline to get you started.
C++
#include <cstdlib>   // rand() returns a random number
#include <iostream>  // std::cout writes to the console
#include <list>      // std::list provides a circular queue

class Waiter
{
public:
   Waiter(int id): id_(id), tips_(0) { }
   int GetId() const { return id_; }
   void AddTip(int amount) { tips_ += amount; }  // use float if tips can have cents
   int GetTips() const { return tips_; }
   void Display() const;
   bool operator<(const Waiter& that) const;
private:
   int id_;
   int tips_;
};

void Waiter::Display() const
{
   //  Display this Waiter's id and tips on the console.
}

bool Waiter::operator<(const Waiter& that) const
{
   if(this->tips_ < that.tips_) return true;
   if(this->tips_ > that.tips_) return false;

   //  What if two waiters have the same tips?  If this operator does not
   //  consistently order two items, std::list::sort throws an exception.
   //  So you need to do something to always choose the same waiter.
}

int GenTip() 
{
  return 0; // todo: random tip with range 0 - 10 
}

int main()
{
   std::list< Waiter > Waiters;  // all the waiters currently working

   for(int id = 101; id <= 105; ++id)
   {
      Waiters.push_back(Waiter(id));
   }

   while(Waiters.size() > 1)
   {
      for(auto w = Waiters.begin(); w != Waiters.end(); ++w)
      {
         w->AddTip(GenTip());  // tip with range 0-10 
      }

      //  Sort Waiters using list::sort, which will invoke your implementation
      //  of Waiter::operator<

      //  The Waiters have been sorted in descending order, so display the
      //  first one and remove it from the list.
   }

   //  Waiters only has one Waiter remaining.  Display that Waiter on the
   //  console.
}
 
Share this answer
 
v2
Comments
merano99 15-Mar-22 17:38pm    
very nice solution. (my 5)
To calculate a tip with range i would suggest
int GenTip()
{
std::random_device rd; // define random device
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(0, 10); // define the range
return distr(gen); // generate number
}

Note: the solution functionally covers the task well, but the reference to the circular linked list doesn't seem to be fulfilled?
"(i)Code the above scheme in C++, insert 0 when initializing circular linked list."
On the other hand, a circularly linked list would hardly be sortable.
It is unclear what the task giver was thinking.

You accept negative tips?
Greg Utas 15-Mar-22 18:16pm    
I wanted to leave the generation of the random number up to the poster, but you're quite right about the circular queue! I didn't read it carefully enough.
C++
// To implement a circular list class you need first a node.
class node {
public:
	node(int myid) :id(myid), next(NULL) {};
	virtual ~node() {};
    node* getNext() { return next; }
    int getID() { return id; }
	void setNext(node* newNext){ next = newNext; };
private:
	node* next;
	int id;
};

// Based on this, a list of waiters can now be created.
class waiters 
{
public:
	waiters() :last(NULL) { }; // constructor
	~waiters();                // destructor
	bool isempty(){ return last == NULL; };
	void insert(int id);
	int remove();
private:
	node *last;                // ptr to last node of list
};

waiters::~waiters() {               // destructor
	while (!isempty())
		remove();
};

void waiters::insert(int id)
{
  node *newNode = new node(id);        // new node
  if (last == NULL) {                  // if empty list
     last = newNode;                  //  create single node list
     newNode->setNext(newNode);
     return;
  }
  ...   //  append node to end of list
  last = newNode;
}

int waiters::remove()
{
   int x=0;
   ...
   return x;
}

int main()                              // test class waiter
{
	waiters w;
	w.insert(101);
	w.insert(102);
	w.insert(103);
	w.insert(104);
	w.insert(105);

	// ...

	while (!w.isempty())
		std::cout << w.remove() << std::endl;
	return 0;
}

Some places were deliberately not implemented here. If something is not clear, you can find many explanations on the net.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

And ... either this is the same question you pasted 2 days ago under a different account, or your classmates are ahead of you ... There are waiters for serving multiple customers in which 5 waiters have the following id: 101, 102, 103, 104, 105.[^] You didn't get the code then, either.
 
Share this answer
 
v2
Quote:
i am not able to do this task in C++. so please help me out with this code . i will be very gratefull. i have this question for quiz .i want this code urgently.

If you just go blank with this problem, you need to have an urgent talk with your teacher.
As is, your question is a do my homework for me, and this, we will not do.
Programming is learned by trial and error, you learn from your errors, and this, we can't do for you. It is like learning to read or write, the only way is to practice by yourself.

The requirement is not a monolith, it suggest to split in 4 parts that you will put together later.
I suggest a few readings that may help.
- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
Program Development by Stepwise Refinement[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900