C++ Programming Scenario A supermarket has 9 cashiers. Customers arrive and leav
ID: 3714709 • Letter: C
Question
C++ Programming
Scenario
A supermarket has 9 cashiers. Customers arrive and leave at random time intervals. Use 0-5 seconds for this project. The goal of the program is to put the next arriving customer in the shortest queue out of the 9 cashiers. Your program will need to loop infinitely to simulate the passing of a certain amount of time. I suggest looping the program every 50 ms and clearing the screen and displaying all queues. Use a simple for loop as a delay.
Key Points
Code must be object oriented
Code must be thoroughly commented
The project shall use data structures and algorithms learned in the course.
Explanation / Answer
#include <iostream>
#include <limits.h>
#include<sys/time.h>
using namespace std;
// A structure to represent a queue
class Queue
{
int front, rear;
unsigned capacity;
int* array;
public:
int size;
Queue(unsigned capacity1)
{
capacity = capacity1;
front = size = 0;
rear = capacity - 1; // This is important, see the enqueue
array = new int[capacity];
}
// Queue is full when size becomes equal to the capacity
int isFull()
{
return (size == capacity);
}
// Queue is empty when size is 0
int isEmpty()
{ return (size == 0); }
// Function to add an item to the queue.
// It changes rear and size
void enqueue( int item)
{
if (isFull())
return;
rear = (rear + 1)%capacity;
array[rear] = item;
size = size + 1;
cout<<"enqueued to queue:"<< item;
}
// Function to remove an item from queue.
// It changes front and size
int dequeue()
{
if (isEmpty())
return INT_MIN;
int item = array[front];
front = (front + 1)%capacity;
size = size - 1;
return item;
}
// Function to get front of queue
// Function to get rear of queue
};
// function to create a queue of given capacity.
// It initializes size of queue as 0
// Driver program to test above functions./
int main()
{
int size,i,min;
typedef struct timeval time;
time start;
gettimeofday(&start,NULL);
cout<<"Enter the size of eatch queue:";
cin>>size;
Queue queue[9]={size,size,size,size,size,size,size,size,size};
while(start.tv_sec>50)
{
if((start.tv_sec%5==0)&&(start.tv_sec!=0))
{
for(i=0;i<9;i++)
cout<<"dequed"<<queue[i].dequeue();
}
min=0;
for(i=1;i<9;i++)
if(queue[i].size<queue[min].size)
min=i;
queue[min].enqueue(min);
cout<<min<<" "<<queue[min].size<<endl;
if((start.tv_usec%50==0)&&(start.tv_usec!=0))
{
for(i=0;i<9;i++)
cout<<"Size of Queue"<<i+1<<queue[i].size<<endl;
}
}
//printf("Front item is %d ", queue.front());
//printf("Rear item is %d ", queue.rear());
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.