Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. The C++ Stack Class [ Code at the end ] 2. The Position Class Create a class

ID: 640323 • Letter: 1

Question

1. The C++ Stack Class

[ Code at the end ]

2. The Position Class

Create a class called Position. A Position object represents a row and a column position on a two dimensional 10 x 10 grid. You should be able to set the position (row, column), get the row, get the column, print the position, and compare two positions (return true if the positions match, false otherwise). [Note: no 2D grid is needed, just the Position class where an object represents one position on the grid.]

3. Test the Position Class with the Stack Class

Modify your Stack from problem #1, so that you can push and pop Position objects onto and off your stack. If you used the typedef for ItemType in your implementation, you can simply switch from char to Position. Create a short program to test your Position Stack. Declare two Position objects and one pointer to a Position object in main(). For your third object allocate memory for the Position object in main(). Ask the user to give you six pairs of numbers (rows and columns) that will represent three Positions on a 10 x 10 grid (row, column). Assume that for the user the rows and columns are numbered from 1 to 10. Input validation is needed when the user enters the data (before you set the position). Then set the values for the three different Positions and push each Position object onto the Stack (note that a copy of each object is placed on the Stack by the push function). Then, while the stack is not empty, pop the Position objects off the stack, print the position and report if this position matches the second Position. You may include other declarations and functions as appropriate. Refer to the program outline for a look at what the program should look like.

In summation: You should declare three objects of the Position class one being a pointer, push each one onto the Stack, then as you pop each one of the stack check if the item matches the second item.

PROGRAM OUTLINE:
#include stuff
class Position
{
//stuff
};

class Stack
{
//stuff
};
int main()
{
//stuff
}

/*Function Definitions*/

__________________________________________________

STACK CLASS CODE:

#include
#include
using namespace std;

const int SIZE = 8;
typedef char ItemType;; // Current implementation uses a stack of char

// Beginning of Stack Class //
class Stack
{

private:
   ItemType stackItems[SIZE]; // Holds stack of items
   int top; // Index to top of the stack and next available position

public:
   Stack(); // Constructor
   ~Stack(); // Destructor
   bool IsEmpty(); // Checks if stack is empty
   bool IsFull(); // Checks if stack is full
   void Clear(); // Clears stack
   void Push(ItemType); // Pushes an item onto the stack unless it's full
   ItemType Pop(); // Pops an item off the stack unless its empty

};

/* Member Function Definitions */
Stack::Stack()
{
   Clear();
}

Stack::~Stack()
{

}

bool Stack::IsEmpty()
{
   if (top == 0)
       return true;
   else
       return false;
}

bool Stack::IsFull()
{
   if (top == SIZE)
       return true;
   else
       return false;
}

void Stack::Clear()
{
   for (int i = 0; i < SIZE; i++)
       stackItems[i] = 0;
   top = 0;
}

void Stack::Push(ItemType item)
{
   if (IsFull())
   {
       cout << "Stack is full. No change to stack. ";
       return;
   }
   stackItems[top] = item;
   top++;
}

ItemType Stack::Pop()
{
   if (IsEmpty())
   {
       cout << "Error in Pop(): Stack is empty. Exiting program. ";
       exit(1);
   }
   top--;
   return stackItems[top];
}
// End of Stack Class //

Explanation / Answer

using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('V'); st.Push('H'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values "); st.Pop(); st.Pop(); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } } } }