Add code to the header and main file to meet standards Files

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 the stack.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

Solution

1. Class Should Integers rather than Characters.

so simply change the private variables of char datatype to int datatype.

  

2.pop should return -1 if the stack if empty

3. Adding a member function called print as

int stack::print()

{

for(int i=0;i<stack_size;i++)

cout<<data[i]<<endl;

}

4.Adding Element member function

int stack::elements()

{

return top;

}

4.Adding peek member function

int stack::peek()

{

return data[top];

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site