CSCI 230 Data Structures and Algorithms Fall 1998

Lab 1 (27-28 Aug)
SOLUTION

Solution Files:
buffer.cpp
buffer.h
main.cpp
sorted_buffer.cpp
sorted_buffer.h
test_sorted.cpp

  1. Compiler errors. There were a number of minor compiler errors you needed to fix.

  2. Test 1. The bug exposed by test 1 is that in the buffer::increment function, the index to be incremented is passed by value rather than by reference. To fix it, change the type of the argument.

  3. Test 2. The bug exposed by test 2 is that the buffer::isfull function does not work correctly when head = 0 and tail = last. For this case, "one more than the tail index" means that we must "wrap around" the array. You could write a decrement function, or just use the existing increment. You need to be careful not to change the head/tail index, but rather a temporary local variable.

  4. Test 3. This is another error in the use of pass by value where pass by reference should have been used in functoin FillTen.

  5. Test 4. The bug in test 4 is due to the default assignment operator, which does a "bitwise copy". You need to add a custom assignment operator to the buffer class. This is an important aspect of implementing data structures in C++, so study the example solution and read up on operator overloading in your favorite C++ book. Make sure you know how to do this; you will need to do it a lot in future labs and projects.

  6. Test 5. (If you got this far.) There were a couple of errors to fix to get it to compile: the private members of buffer need to be protected instead so that sorted_buffer can access them, and the file test_sorted.cpp needs to include iostream as well as sorted_buffer.h.

    There are two bugs in the sorted_buffer implementation code. The sorted_buffer::decrement function needs to be pass by reference (we are emphasizing this particular bug so much because it is a very common error in the projects), and the sorted_buffer::add function has a tricky bug in the following line:

           if (buf[i] < x)
    
    should be:
           if (buf[j] < x)
    


    Last updated: 28 Aug 1998 by valoisj@cs.rpi.edu