CS Logo
  • Homework 6

1. A

2. B

3. B

4. A

5. B or C    foo == &(foo[0]) and foo == &foo (Either for full credit)

 

Problem 6


// Stack program. Pop and push are implemented as well as

// a driver program to test pop and push.

// This program does not handle the case where a pop occurs on

// an empty stack or a push on a full one.

 

#include <iostream> 

using namespace std;

 

void push( int array[], int item, int& top );

 

int pop( int array[], int& top );

 

int main()

 

{

      int max_size = 0;

      int top = 0;

      int selection = 0;

 

      cout << "Enter the maximum stack size" << endl;

      cin >> max_size;

 

      int* array = new int[ max_size ];

 

      do

      {

      cout << "1. Push" << endl;

      cout << "2. Pop" << endl;

      cout << "3. Quit" << endl;

      cout << "Select an operation: " << endl;

      cin >> selection;

     

      if ( selection == 1 )

      {

         int item = 0;

         cout << "Enter the number to push onto the stack: "

   << endl;

         cin >> item;

         push( array, item, top);

      }

      else

      {

         cout << "Popped item " << pop( array, top )

         << " from the stack" << endl;

      }

      }while (selection != 3);

 

      return 0;

}

 

// Push function. Adds "item" to the top of the stack

// and increments "top"

void push( int array[], int item, int& top )

{

      array[top++] = item;

}

 

// Pop function. Decrements "top" and returns

// the item there

int pop( int array[], int& top )

{

      return array[--top];

}

     

 

Problem 7

 

// Queue program. Pop and push are implemented as well as

// a driver program to test pop and push. The program is identical

// to the stack program in problem 6 except that we have a top counter

// and a bottom counter - the queue adds to the top and takes from the

// bottom.

#include <iostream>

 

using namespace std;

 

void push( int array[], const int array_size, int item, int& top );

 

int pop( int array[], const int array_size, int& bottom );

 

int main()

{

 

      int max_size = 0;

      int top = 0, bottom = 0;

      int selection = 0;

 

      cout << "Enter the maximum stack size" << endl;

      cin >> max_size;

 

      int* array = new int[ max_size ];

 

      do

      {

            cout << "1. Push" << endl;

            cout << "2. Pop" << endl;

            cout << "3. Quit" << endl;

            cout << "Select an operation: " << endl;

            cin >> selection;

 

 

            if ( selection == 1 )

            {

                  int item = 0;

cout << "Enter the number to push onto the stack: " << endl;

 

                  cin >> item;

                  push( array, max_size, item, top);

            }

            else

            {

                  cout << "Popped item "

<< pop( array, max_size, bottom )

<< " from the stack" << endl;

            }

      }while (selection != 3);

 

      return 0;

}

 

//Pop and push use top or bottom modulus array_size to prevent

//the top and bottom counters from running off the end of the array.

// I did not expect you to do this in your solutions.

 

 

// Push function. Adds "item" to the top of the stack

// and increments "top".

void push( int array[], const int array_size, int item, int& top )

{

      array[top++ % array_size] = item;

}

 

// Pop function. Decrements the top and returns

// the item there

int pop( int array[], const int array_size, int& bottom )

{

      return array[bottom++ % array_size];

}