Exercises
 

Exercises

You may compare your solutions with my solutions.

  1. Add method Top () that returns the value from the top of the stack without changing its state. Add method Count that returns the count of elements on the stack.
  2. Modify the main function so that one of the stack contracts is violated. See what happens when you run the program with the assertions turned on.
  3. Modify the stack so that it becomes CharStack--the stack of characters. Use it to invert strings by pushing all the characters of a string, and then popping and printing them one by one.
  4. Design the abstract data type Queue of doubles with methods Put and Get and the FIFO (First-In-First-Out) behavior. Implement it using an array and two indices: the put index, and the get index that trails the put index. The contract of this particular queue reads: The Put method shall never be called more than maxPuts times during the lifetime of the Queue. Thou shalt not Get when the Queue is empty.
  5. Design and implement the DblArray data type with the methods:
    void Set (int i, double val)
    and
    double Get (int i) const
    to set and get the values of the particular cells in the array. Add one more method
    bool IsSet (int i) const 
    that returns false if the cell has never been set before and true otherwise. The contract is that no Sets or Gets shall be done with the index greater than maxCells - 1; that one shall never try to get a value that hasn't been set before, and that one shouldn't try set a value that has already been set before (write once-read many times array). The array may store any values of the type double.

    Hint: if you want to negate a Boolean result, put an exclamation mark in front of it. For instance
    !IsSet (i)
    is true when the cell is not set and false when it is.