Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list.
Requeriments:



Headers:
DynamicArray.h
#ifndef DynamicArray_h
#define DynamicArray_h
#include
using namespace std;
template
class DynamicArray
{
V* values;
int cap;
V dummy;
public:
DynamicArray(int = 2);
DynamicArray(const DynamicArray&);
~DynamicArray() { delete[] values; }
int capacity() const { return cap; }
void capacity(int);
V operator[](int) const;
V& operator[](int);
DynamicArray& operator=(const DynamicArray&);
};
template
DynamicArray::DynamicArray(int cap)
{
this->cap = cap;
values = new V[cap];
for (int index = 0; index < cap; index++) {
values[index] = V();
}
}
template
V DynamicArray::operator[](int index) const
{
if (index < 0 || index >= cap)
return V(); // a copy
return values[index]; // a copy
}
template
V& DynamicArray::operator[](int index)
{
if (index < 0) {
return dummy; // a copy
}
else if (index >= cap) {
capacity(2 * index);
}
return values[index]; // a copy
}
template
void DynamicArray::capacity(int newCap) {
V* temp = new V[newCap];
// get the lesser of the new and old capacities
int limit = min(newCap, cap);
// copy the contents
for (int i = 0; i < limit; i++) {
temp[i] = values[i];
}
// set added values to their defaults
for (int i = limit; i < cap; i++) {
temp[i] = V();
}
// deallocate original array
delete[] values;
// switch newly allocated array into the object
values = temp;
// update the capacity
cap = newCap;
}
template
DynamicArray::DynamicArray(const DynamicArray& original)
{
cap = original.cap; // still copy
values = new V[cap]; // not copy, is new
for (int i = 0; i < cap; i++) { // contents copy original to new
values[i] = original.values[i];
}
}
template
DynamicArray& DynamicArray::operator=(const DynamicArray& original)
{
if (this != &original) //check if copy or not, better not be tho
{
// same as destructor
delete[] values;
// same as copy constructor
cap = original.cap;
values = new V[cap]; // not copy, is new
for (int i = 0; i < cap; i++) { // contents copy original to new
values[i] = original.values[i];
}
}
return *this; // return self reference
}
#endif
Queue.h
#ifndef QUEUE_QUEUE_H //checks if 'QUEUE_QUEUE_H' has been defined
#define QUEUE_QUEUE_H //define 'QUEUE_QUEUE_H' to use in the program
template //use a template class called 'V'
class Queue //class Queue
{
struct Node
{
V value;
Node* next;
};
Node* firstNode; //head pointer to the first node
int siz = 0; //variable necessary to trak the number of nodes
Node* lastNode; //necssary to create a private data member
public:
Queue(); // may have a defaulted parameter, tested
void push(const V&);
V& front(); // return a mutable reference to the oldest node
V& back(); // return a mutable reference to the newest node
void pop(); // remove the oldest node
int size() const { return siz; } //return the values of the size
bool empty() const { return siz == 0; } //neccesary to empty the values
void clear(); //necessary to clear the values
~Queue() { clear(); }
Queue& operator=(const Queue&);
Queue(const Queue&);
};
template
Queue::Queue()
{
lastNode = 0;
siz = 0;
}
//function to push the values from the stack
template
void Queue::push(const V& value)
{
Node* temp = new Node{ value }; // C++11
if (lastNode) lastNode->next = temp;
else firstNode = temp;
lastNode = temp;
++siz;
}
//template to put the values to the front of the stack
template
V& Queue::front()
{
return firstNode->value;
}
//function necessary to put the values on the back of the stack
template
V& Queue::back() {
return lastNode->value;
}
//function necessary to pop the values fromn the stack
template
void Queue::pop()
{
if (firstNode)
{
Node* p = firstNode;
firstNode = firstNode->next;
delete p;
--siz;
}
if (siz == 0)
{
lastNode = 0;
}
}
//function necessary to clear the values from the stack
template
void Queue::clear()
{
while (firstNode)
{
Node* p = firstNode;
firstNode = firstNode->next;
delete p;
--siz;
}
lastNode = 0;
}
//function necessary to copy the values
template
Queue::Queue(const Queue& original)
{
firstNode = 0;
lastNode = 0; // temporary tail
siz = original.siz;
for (Node* p = original.firstNode; p; p = p->next)
{
Node* temp = new Node;
temp->value = p->value;
temp->next = 0;
if (lastNode) lastNode->next = temp;
else firstNode = temp;
lastNode = temp;
}
}
//function necessary to create values if they are not stored in the original queue
template
Queue& Queue::operator=(const Queue& original)
{
if (this != &original)
{
// deallocate existing list
while (firstNode)
{
Node* p = firstNode->next;
delete firstNode;
firstNode = p;
}
// build new queue
lastNode = 0; // temporary tail
for (Node* p = original.firstNode; p; p = p->next)
{
Node* temp = new Node;
temp->value = p->value;
temp->next = 0;
if (lastNode) lastNode->next = temp;
else firstNode = temp;
lastNode = temp;
}
siz = original.siz;
}
return *this;
}
#endif
Code:
#include
#include
#include
#include
using namespace std;
#include
#include
#include "MyDynamicArray.h"
#include "MyQueue.h"
struct Customer {
char ID;
int arrivalT;
int endT;
};
int getRandomNumberOfArrivals(double);
char genID(char&);
int randTimeAdd(int, int);
int main()
{
srand(time(0));
rand();
int numServers = 0, waitMaxLength = 0, minServTime = 0, maxServTime = 0, clockStopTime = 0;
double avgArrivalRate = 0.5;
char curAlpha = 'A';
ifstream fin;
fin.open("simulation.txt");
int switchCount = 0;
while (fin.good()) {
string input;
getline(fin, input);
switch (switchCount)
{
case 0:
numServers = atoi(input.c_str());
cout << "number of servers: " << numServers << endl;
break;
case 1:
avgArrivalRate = atof(input.c_str());
break;
case 2:
waitMaxLength = atoi(input.c_str());
cout << "maximum queue length: " << waitMaxLength << endl;
break;
case 3:
minServTime = atoi(input.c_str());
cout << "minimum service time: " << minServTime << " minutes" << endl;
break;
case 4:
maxServTime = atoi(input.c_str());
cout << "maximum service time: " << maxServTime << " minutes" << endl;
break;
case 5:
clockStopTime = atoi(input.c_str());
cout << "customer arrival rate: " << avgArrivalRate << " per minute, for " << clockStopTime << " minutes" << endl;
break;
default:
throw("UH OH - PROBLEM");
}
switchCount++;
}
Queue custQ;
DynamicArray nowServing;
DynamicArray serversStatus;
for (int time = 0; ; time++) {
// handle all services scheduled to complete at this clock time
for (int i = 0; i < numServers; i++) {
if (serversStatus[i]) { //means is busy
if (nowServing[i].endT == time) {
serversStatus[i] = false;
}
}
}
// handle new arrivals -- can be turned away if wait queue is at maximum length!
if (time < clockStopTime) {
int numArrive = getRandomNumberOfArrivals(avgArrivalRate);
for (int i = 0; i < numArrive; i++) {
if (custQ.size() < waitMaxLength) {
Customer c;
c.ID = genID(curAlpha);
c.arrivalT = time;
custQ.push(c);
}
}
}
// for idle servers, move customer from wait queue and begin
for (int i = 0; i < numServers; i++) {
if (!serversStatus[i] && !custQ.empty()) {
nowServing[i] = custQ.front();
custQ.pop();
nowServing[i].endT = time + randTimeAdd(minServTime, maxServTime);
serversStatus[i] = true;
}
}
//output the summary
//output the current time
//output a visual representation of the servers and the wait queue
cout << "\nTime: " << time << endl;
cout << "---------------------------" << endl;
cout << "server now serving wait queue" << endl;
cout << "------ ----------- ----------" << endl;
for (int i = 0; i < numServers; i++) {
string show = " ";
if (serversStatus[i]) {
show = nowServing[i].ID;
}
cout << setw(2) << i << setw(10) << show;
if (i == 0) {
Queue tempQ = custQ;
cout << setw(10);
while (!tempQ.empty()) {
cout << tempQ.front().ID;
tempQ.pop();
}
}
cout << endl;
}
int numIdle = 0;
for (int i = 0; i < numServers; i++) {
if (!serversStatus[i]) {
numIdle++;
}
}
if (numIdle == numServers && time >= clockStopTime) {
break;
}
do {
cout << '\n' << "Press ENTER to continue...";
} while (cin.get() != '\n');
}
system("pause");
}
int randTimeAdd(const int a, const int b) {
return a + (rand() % b);
}
char genID(char& curAlpha) {
if (curAlpha == 'Z') {
curAlpha = 'A';
return 'Z';
}
return curAlpha++;
}
int getRandomNumberOfArrivals(double averageArrivalRate) {
int arrivals = 0;
double probOfnArrivals = exp(-averageArrivalRate);
for (double randomValue = (double)rand() / RAND_MAX;
(randomValue -= probOfnArrivals) > 0.0;
probOfnArrivals *= averageArrivalRate / static_cast(++arrivals));
return arrivals;
}
txt file
4
2.5
8
3
10
50
DynamicArray.h
#ifndef DynamicArray_h
#define DynamicArray_h
// #include
using namespace std;
template <class V>
class DynamicArray
{
V *values;
int cap;
V dummy;
public:
DynamicArray(int = 2);
DynamicArray(const DynamicArray &);
~DynamicArray() { delete[] values; }
int capacity() const { return cap; }
void capacity(int);
V operator[](int) const;
V &operator[](int);
DynamicArray &operator=(const DynamicArray &);
};
template <class V>
DynamicArray<V>::DynamicArray(int cap)
{
this->cap = cap;
values = new V[cap];
for (int index = 0; index < cap; index++)
{
values[index] = V();
}
}
template <class V>
V DynamicArray<V>::operator[](int index) const
{
if (index < 0 || index >= cap)
return V(); // a copy
return values[index]; // a copy
}
template <class V>
V &DynamicArray<V>::operator[](int index)
{
if (index < 0)
{
return dummy; // a copy
}
else if (index >= cap)
{
capacity(2 * index);
}
return values[index]; // a copy
}
template <class V>
void DynamicArray<V>::capacity(int newCap)
{
V *temp = new V[newCap];
// get the lesser of the new and old capacities
int limit = min(newCap, cap);
// copy the contents
for (int i = 0; i < limit; i++)
{
temp[i] = values[i];
}
// set added values to their defaults
for (int i = limit; i < cap; i++)
{
temp[i] = V();
}
// deallocate original array
delete[] values;
// switch newly allocated array into the object
values = temp;
// update the capacity
cap = newCap;
}
template <class V>
DynamicArray<V>::DynamicArray(const DynamicArray &original)
{
cap = original.cap; // still copy
values = new V[cap]; // not copy, is new
for (int i = 0; i < cap; i++)
{ // contents copy original to new
values[i] = original.values[i];
}
}
template <class V>
DynamicArray<V> &
DynamicArray<V>::operator=(const DynamicArray &original)
{
if (this != &original) //check if copy or not, better not be tho
{
// same as destructor
delete[] values;
// same as copy constructor
cap = original.cap;
values = new V[cap]; // not copy, is new
for (int i = 0; i < cap; i++)
{ // contents copy original to new
values[i] = original.values[i];
}
}
return *this; // return self reference
};
#endif
Queue.h
#ifndef QUEUE_QUEUE_H //checks if 'QUEUE_QUEUE_H' has been defined
#define QUEUE_QUEUE_H //define 'QUEUE_QUEUE_H' to use in the program
template <class V> //use a template class called 'V'
class Queue //class Queue
{
struct Node
{
V value;
Node *next;
};
Node *firstNode; //head pointer to the first node
int siz; //variable necessary to trak the number of nodes
Node *lastNode; //necssary to create a private data member
public:
Queue(); // may have a defaulted parameter, tested
void push(const V &);
V &front(); // return a mutable reference to the oldest node
V &back(); // return a mutable reference to the newest node
void pop(); // remove the oldest node
int size() const { return siz; } //return the values of the size
bool empty() const { return siz == 0; } //neccesary to empty the values
void clear(); //necessary to clear the values
~Queue() { clear(); }
Queue &operator=(const Queue &);
Queue(const Queue &);
};
template <class V>
Queue<V>::Queue()
{
lastNode = 0;
siz = 0;
}
//function to push the values from the stack
template <class V>
void Queue<V>::push(const V &value)
{
Node *temp = new Node; // C++11
temp->value = value;
if (lastNode)
lastNode->next = temp;
else
firstNode = temp;
lastNode = temp;
++siz;
}
//template to put the values to the front of the stack
template <class V>
V &Queue<V>::front()
{
return firstNode->value;
}
//function necessary to put the values on the back of the stack
template <class V>
V &Queue<V>::back()
{
return lastNode->value;
}
//function necessary to pop the values fromn the stack
template <class V>
void Queue<V>::pop()
{
if (firstNode)
{
Node *p = firstNode;
firstNode = firstNode->next;
delete p;
--siz;
}
if (siz == 0)
{
lastNode = 0;
}
}
//function necessary to clear the values from the stack
template <class V>
void Queue<V>::clear()
{
while (firstNode)
{
Node *p = firstNode;
firstNode = firstNode->next;
delete p;
--siz;
}
lastNode = 0;
}
//function necessary to copy the values
template <class V>
Queue<V>::Queue(const Queue &original)
{
firstNode = 0;
lastNode = 0; // temporary tail
siz = original.siz;
for (Node *p = original.firstNode; p; p = p->next)
{
Node *temp = new Node;
temp->value = p->value;
temp->next = 0;
if (lastNode)
lastNode->next = temp;
else
firstNode = temp;
lastNode = temp;
}
}
//function necessary to create values if they are not stored in the original queue
template <class V>
Queue<V> & Queue<V>::operator=(const Queue &original)
{
if (this != &original)
{
// deallocate existing list
while (firstNode)
{
Node *p = firstNode->next;
delete firstNode;
firstNode = p;
}
// build new queue
lastNode = 0; // temporary tail
for (Node *p = original.firstNode; p; p = p->next)
{
Node *temp = new Node;
temp->value = p->value;
temp->next = 0;
if (lastNode)
lastNode->next = temp;
else
firstNode = temp;
lastNode = temp;
}
siz = original.siz;
}
return *this;
};
#endif
main.cpp
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;
// #include
// #include
#include "DynamicArray.h"
#include "Queue.h"
struct Customer
{
char ID;
int arrivalT;
int endT;
};
int getRandomNumberOfArrivals(double);
char genID(char &);
int randTimeAdd(int, int);
int main()
{
srand(time(0));
rand();
int numServers = 0, waitMaxLength = 0, minServTime = 0, maxServTime = 0, clockStopTime = 0;
double avgArrivalRate = 0.5;
char curAlpha = 'A';
ifstream fin;
fin.open("simulation.txt");
int switchCount = 0;
while (fin.good())
{
string input;
getline(fin, input);
switch (switchCount)
{
case 0:
numServers = atoi(input.c_str());
cout << "number of servers: " << numServers << endl;
break;
case 1:
avgArrivalRate = atof(input.c_str());
break;
case 2:
waitMaxLength = atoi(input.c_str());
cout << "maximum queue length: " << waitMaxLength << endl;
break;
case 3:
minServTime = atoi(input.c_str());
cout << "minimum service time: " << minServTime << " minutes" << endl;
break;
case 4:
maxServTime = atoi(input.c_str());
cout << "maximum service time: " << maxServTime << " minutes" << endl;
break;
case 5:
clockStopTime = atoi(input.c_str());
cout << "customer arrival rate: " << avgArrivalRate << " per minute, for " << clockStopTime << " minutes" << endl;
break;
default:
throw("UH OH - PROBLEM");
}
switchCount++;
}
Queue<Customer> custQ;
DynamicArray<Customer> nowServing;
DynamicArray<int> serversStatus;
for (int time = 0;; time++)
{
// handle all services scheduled to complete at this clock time
for (int i = 0; i < numServers; i++)
{
if (serversStatus[i])
{ //means is busy
if (nowServing[i].endT == time)
{
serversStatus[i] = false;
}
}
}
// handle new arrivals -- can be turned away if wait queue is at maximum length!
if (time < clockStopTime)
{
int numArrive = getRandomNumberOfArrivals(avgArrivalRate);
for (int i = 0; i < numArrive; i++)
{
if (custQ.size() < waitMaxLength)
{
Customer c;
c.ID = genID(curAlpha);
c.arrivalT = time;
custQ.push(c);
}
}
}
// for idle servers, move customer from wait queue and begin
for (int i = 0; i < numServers; i++)
{
if (!serversStatus[i] && !custQ.empty())
{
nowServing[i] = custQ.front();
custQ.pop();
nowServing[i].endT = time + randTimeAdd(minServTime, maxServTime);
serversStatus[i] = true;
}
}
//output the summary
//output the current time
//output a visual representation of the servers and the wait queue
cout << "\nTime: " << time << endl;
cout << "---------------------------" << endl;
cout << "server now serving wait queue" << endl;
cout << "------ ----------- ----------" << endl;
for (int i = 0; i < numServers; i++)
{
string show = " ";
if (serversStatus[i])
{
show = nowServing[i].ID;
}
cout << setw(2) << i << setw(10) << show;
if (i == 0)
{
Queue<Customer> tempQ = custQ;
cout << setw(10);
while (!tempQ.empty())
{
cout << tempQ.front().ID;
tempQ.pop();
}
}
cout << endl;
}
int numIdle = 0;
for (int i = 0; i < numServers; i++)
{
if (!serversStatus[i])
{
numIdle++;
}
}
if (numIdle == numServers && time >= clockStopTime)
{
break;
}
do
{
cout << '\n'
<< "Press ENTER to continue...";
} while (cin.get() != '\n');
}
system("pause");
}
int randTimeAdd(const int a, const int b)
{
return a + (rand() % b);
}
char genID(char &curAlpha)
{
if (curAlpha == 'Z')
{
curAlpha = 'A';
return 'Z';
}
return curAlpha++;
}
int getRandomNumberOfArrivals(double averageArrivalRate)
{
int arrivals = 0;
double probOfnArrivals = exp(-averageArrivalRate);
for (double randomValue = (double)rand() / RAND_MAX;
(randomValue -= probOfnArrivals) > 0.0;
probOfnArrivals *= averageArrivalRate / static_cast<int>(++arrivals))
;
return arrivals;
}![Crkv-sdcQlocalhost DynanicArray]$ g++ nain.cpp Crkv-sdcQlocalhost DynanicArray]$ ./a. out nunber of servers: 4 naxinun queue](http://img.homeworklib.com/questions/b790cd00-d284-11ea-a6d9-33d4ef8428b6.png?x-oss-process=image/resize,w_560)
Hi I need a fix in my program. The program needs to finish after serving the...
I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...
A library maintains a collection of books. Books can be added to
and deleted from and checked out and checked in to this
collection.
Title and author name identify a book. Each book object
maintains a count of the number of copies available and the number
of copies checked out. The number of copies must always be greater
than or equal to zero. If the number of copies for a book goes to
zero, it must be deleted from the...
Hello, I have some errors in my C++ code when I try to debug it.
I tried to follow the requirements stated below:
Code:
// Linked.h
#ifndef INTLINKEDQUEUE
#define INTLINKEDQUEUE
#include <iostream>
usingnamespace std;
class IntLinkedQueue
{
private: struct Node {
int data;
Node *next;
};
Node *front; // -> first item
Node *rear; // -> last item
Node *p; // traversal position
Node *pp ; // previous position
int size; // number of elements in the queue
public:
IntLinkedQueue();...
can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...
Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it. Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...
The program I wrote has a few memory leaks. My assignment is to
just fix the memory leaks. Here's the code:
bool RemoveTail()
{
if (tail == nullptr)
return false;
Node *ptr = tail;
if(tail->prev != nullptr)
{
tail = tail->prev;
tail->next = nullptr;
}
else
{
tail = nullptr;
head = nullptr;
}
delete ptr;
ptr = nullptr;
length--;
return true;
}
bool RemoveAt(const unsigned int index)
{
if(index >= length || index < 0)
return false;
unsigned int...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...
howthe output of the following 4 program segments (a) const int SIZE=8; int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8}; int index; index=0; res = values[index]; for (int j=1; j<SIZE; j++) { if (values[j] > res) { res = values[j]; index = j; cout << index << res << endl; } } cout <<...
Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node { int data; node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() { struct node*head=NULL; char commands; int value; ...