Quiz 2: 3 questions, 60 minutes

Question 1: array-based queues
For an array-based queue, emented using arrays, one index is used to track the current front position (for removes) and another to track the current back position (for inserts). After we remove we increment the front position, after we insert we increment the back position. However, when the queue is empty we should set front to -1 (so we realize there is no valid item in the queue) and we need to make front and back 'wrap around' to the start of the array when they run off the end. For the queue class shown below, implement the constructor and the insert method.
// default size of queue array
const int DefaultSize = 100;

// a queue of ints
class queue {
   protected:
      int *q;      // array of queue elements
      int size;    // number of elements queue can hold
      int front;   // current position of front element
      int back;    // current position of back element
   public:
      // dynamically allocate the array 
      //    and initialize front, back, size
      Queue(int sz = DefaultSize);
     ~Queue();             // deallocate the array
      bool insert(int i);  // insert item i at back        
      bool remove(int &i); // remove front item into i
      bool isfull();       // returns true iff the queue is full
      bool isempty();      // returns true iff the queue is empty
};   

Question 2: file I/O and command line arguments
Write a complete program that expects to receive two command line arguments (aside from the name of the executable of course).

If the correct number of arguments are supplied, it takes the first as a filename and the second as a block of text to search for, opens the file, and counts the number of times the text appears in the file. For example, in the line below the text "foo" appears three times:
blah blah foo blahfoo blah blahfooblah

Question 3: exceptions
In your own words, describe how exceptions (try, throw, and catch) work in C++, and provide a short code example.