Add code to the header and main file to meet standards thanks 1. Make the follow
ID: 3572860 • Letter: A
Question
Add code to the header and main file to meet standards thanks
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 the stack. elements shouldnot print 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, if wash_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
output
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
stack.cpp file
stack.h file
Explanation / Answer
// implementation file for the stack class
#include <iostream>
using namespace std;
const int stack_size = 100;
class stack
{
private:
int data [stack_size]; // elements in the stack
int top; // index of the top element of the stack
public:
stack (); // constructor creates an empty stack
void push (int item); // adds an element to the top of the stack
int pop (); // removes and returns the top element
bool empty (); // returns true if stack is empty
bool full (); // returns true if stack is full
void print();
int elements();
int peek();
};
// constructor creates an empty stack
stack::stack ()
{
top = -1;
}
// push adds an element, item, to the top of the stack
void stack::push (int item)
{
// if the stack is full, print an error message
if (full ())
{
cout << " Stack Class Error: Pushing on a full stack";
cout << " Element being pushed is " << item;
}
else // OK to push the new element
{
top++;
data [top] = item;
}
}
// pop removes and returns the top element from the stack
int stack::pop ()
{
// if the stack is empty, print an error message
if (empty ())
{
cout << " Stack Class Error: Popping an empty stack";
cout << " Returning a -1";
return -1;
}
else // OK to pop the stack
{
top--;
return data [top + 1];
}
}
// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
return top == -1;
}
// full returns true if the stack is full, else it returns false
bool stack::full ()
{
return top == stack_size - 1;
}
void stack::print(){
cout << "Stack contains: ";
for(int i = 0; i <= top; ++i){
cout << data[i];
if(i != top)
cout << ", ";
}
cout << endl;
}
int stack::elements(){
return top + 1;
}
int stack::peek(){
if(empty()){
return -1;
}
else{
return data[top];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.