Add code to the header and main file to meet standards Files outline are at end
ID: 3580104 • Letter: A
Question
Add code to the header and main file to meet standards Files outline are at end of post make sure it runs as shown
1. Make the following changes to the stack class:
Make all necessary changes to have the class store integers on the stack instead of characters. pop should return -1 if called when the stack is empty.
Add a print member function that prints the values in the stack from top to bottom, one per line.printshould not change the stack.
Write an elements member function that returns the number of elements on thestack.elementsshouldnotprint anything. elements should not change the stack.
Write a peek member function that returns the top element on the stack. peek returns -1 if the stack isempty. peek should not change the stack.
2. Write a simulation of dishwashing at a restaurant in the file stack.cpp. There will be a stack of dishes to be washed. The following variables are used in the program. A number of them have set values. Your program should still work properly if the values are changed.
// total number of minutes in the simulation
int total_minutes = 100,
// print results every print_minutes minutes
print_minutes = 10,
// largest size dish (dish values randomly set
// from 1 to max_dish)
max_dish = 5,
// number of dishes added each minute
add_dishes = 5,
// total size of dishes a washer can wash in one minute
wash_size = 5,
// number of diswashers read from user
dishwashers;
The simulation will run for total_minutes simulated minutes. The current state of the stack will be printed every print_minutes minutes. Dishes on the stack will be represented by integers indicating how big they are (random values from 1 to max_dish). There will be add_dishes random dishes added each minute. Each dishwasher can wash wash_size dishes each minute taken from the top of the stack. So, ifwash_sizeis 5, and the current stack is:
3
1
3
4
the dishwasher could wash the top two plates. The third plate would take the amount of dishes washed over 5. There will be dishwashers (entered by the user) dishwashers working.
A general algorithm for the simulation would thus be:
read number of dishwashers
for (minutes from 1 to total_minutes)
{
add add_dishes random dishes to the stack
for (each dishwasher)
{
wash wash_size dishes from the stack
}
if (it is time to print based on print_minutes)
{
print the current time
print the stack of dishes
print the number of dishes in the stack
}
}
The following page has a sample run. To make the run shorter, total_minutes was set equal to 30). Feel free to change the variable values to help test/debug your program. The simulation should generate different sets of random dishes each run. That is, use srand appropriately :
srand((unsigned)time(NULL)); // Put at beginning of main to seed rand.
L = rand( ) % max_dish + 1; // generates a number between 1 and max_dish
stack.cpp file
stack.h file
sample run:
Enter the number of dishwashers: 3
Time = 10
Dishes in stack:
4
3
Elements in stack: 2
Time = 20
Dishes in stack:
4
4
4
3
1
3
3
Elements in stack: 7
Time = 30
Dishes in stack:
3
3
4
2
4
4
4
3
1
3
3
Elements in stack: 11
Explanation / Answer
Following is the required c++ file. Please note that because entered dishes are random, output will be different on different runs.
#include <cstdlib>
#include <iostream>
#include <ctime>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
int total_minutes = 30, // total number of minutes in the simulation
print_minutes = 10, // print results every print_minutes minutes
max_dish = 5, // largest size dish (dish values randomly set from 1 to max_dish)
add_dishes = 5, // number of dishes added each minute
wash_size = 5, // total size of dishes a washer can wash in one minute
dishwashers; // number of diswashers read from user
stack dishStack;
cout << "Enter the number of dishwashers: ";
cin >> dishwashers;
srand((unsigned)time(NULL));
for( int tmin = 1; tmin <= total_minutes; tmin++ ){
for(int dish =0; dish < add_dishes; dish++ ){
dishStack.push( (rand()%max_dish + 1) );
}
for(int dwasherNo = 0; dwasherNo < dishwashers; dwasherNo++ ){
//wash wash_size dishes from stack
int totalWashed = 0;
int top = dishStack.peek();
while( !dishStack.empty() && totalWashed + top <= wash_size ){
dishStack.pop();
totalWashed += top;
top = dishStack.peek();
}
}
if( tmin%print_minutes == 0 ){
cout << "Time = " << tmin << endl;
cout << "Dishes in stack:" << endl;
dishStack.print();
cout << "Elements in stack: " << dishStack.elements() << endl;
}
}
cout << " ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.