CIS 330 Home Page Last updated 2008/03/07 14:10:19

CIS 330 Assignment 6

Exceptions and Generic Programming with Templates

Use the web service e-turnin to submit your work electronically. You may turn in revisions of your homework up to the time it is due.


  1. Write a generic mybsearch function that implements a binary search of a sorted array-like object. The form of the mybsearch function is that it will take three parameters. The first is an object of some type and represents the value to search for. The second parameter is an array-like object in which to search for the given value. The third parameter is an integer that is the number of items in the array. Being "array-like" means that the second parameter supports the array operator for values from zero to one less than the number of items passed as the third parameter.

    In addition to the second parameter being array-like, the first parameter type must support comparison to the elements in the array by the less than operator.

    The driver code bsearchTest.c can be used to test your solution. Your solution must implement just one template function in the file bsearch.h. It must work with the driver code and should also work with any other parameter values meeting the constraints described above. Note that a generic sort function is provided in sort.h.

    Running the driver program should produce the following:

    ivals is: -13 -9 0 8 8 10 18 22 39 44 100 
    mybsearch(-9, ivals, ilen) returns 1
    mybsearch(8, ivals, ilen) returns 4
    mybsearch(88, ivals, ilen) returns -1
    svals is: Apple goodbye hello peaches pumpkin 
    mybsearch("peaches", svals, slen) returns 3
    
    v is: 0 31 47 52 63 87 94 94 94 95 96 
    mybsearch(0, v, v.size()) returns 0
    mybsearch(94, v, v.size()) returns 8
    
    Note that when there are duplicate values in the list, this implementation finds the last of the duplicates. You may choose to have your implementation find the first of the duplicates.

    [The name mybsearch is used to avoid possible conflicts with a template function named bsearch in the Standard Template Library.]


  2. Write a generic Array class that will behave like a simple array for any data type. The only constraints on the data type are that it has the ability to be output. You may base your class Array on the integer array class used as a base class in the previous assignment.

    The driver code arrayTest.c can be used to test your implementation. Note that your entire template class and implementation should be in the file Array.h.

    Running the driver program should produce the following:

    a1 is [3 5 7 9 11 13 15 17 19 21]
    a2 is [3 5 7 9 11 13 15 17 19 21]
    a3 is [2 5 8 11 14]
    a2 is [2 5 8 11 14]
    s1 is [Tom Dick Harry Moe Curly]
    s2 is [Tom Dick Harry Moe Curly]
    s3 is [one two three]
    s2 is [one Hello,world three]
    

  3. Modify the class Rational of Assignment 3 to throw an exception when a denominator of zero is set in a Rational object. Start with the published Rational.h and Rational.c solution versions.

    Use the driver code rationalTest.c to test your solution.

    Running the driver program should produce the following:

    % rationalTest 3 9
    q is: 4/5
    r is: 1/3
    reciprocal of r is: 3/1
    difference (r - q) is: -7/15
    quotient (r + q) / (r - q) is: -17/7
    
    
    % rationalTest 17 0
    ****RationalError: divide by zero, numerator=17, denominator=0
    
    % rationalTest 8 10
    q is: 4/5
    r is: 4/5
    reciprocal of r is: 5/4
    difference (r - q) is: 0/1
    quotient (r + q) / (r - q) is: 
    ****RationalError: divide by zero, numerator=1, denominator=0
    
    

  4. Modify the class MyString of Assignment 3 to throw exceptions for the following conditions: Use an inheritance hierarchy of exceptions and virtual methods for printing out the exception message. Note that the driver code only has to handle a base exception type.

    To test the memory allocation exception handling, the driver sets a very small process memory size. This will probably only work in the Unix environment. Also, you will find that a memory allocation error causes a standard library exception to be thrown (something of type exception). You will need to catch this and throw your own MyString exception.

    Start with the published MyString.h and MyString.c solution versions.

    Use the driver code stringTest.c to test your solution.

    Running this program should produce the following:

    s1 is:Hello, world, and s1.length() is 12
    s2 is:This is a slightly longer string, but not too long., and s2.length() is 51
    s1[-5] is 
       ***** Caught string error: MyString out of bounds for index -5 and size 12
    s1[3] is l
    s1[s1.length()] is 
       ***** Caught string error: MyString out of bounds for index 12 and size 12
    s1[s1.length() + 5] is 
       ***** Caught string error: MyString out of bounds for index 17 and size 12
    s1(2, s1.length()/2) is llo, w
    s1(s1.length()-2, s1.length()/2) is 
       ***** Caught string error: MyString substring out of bounds for start 10, count 6 and size 12
    s2.length() is 102
    s2.length() is 204
    s2.length() is 408
    s2.length() is 816
    s2.length() is 1632
    s2.length() is 3264
    s2.length() is 6528
    s2.length() is 13056
    s2.length() is 26112
    
       ***** Caught string error: MyString out of space allocating size 52225