CS 311 Spring 2008  >  Assignment 3

CS 311 Spring 2008
Assignment 3

Assignment 3 is due at 5 p.m. Thursday, February 21. It is worth 20 points.

Procedures

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA3”.

Exercises (20 pts total)

General

In each of the following four exercises, you are to write a function or function template. All four are to be in the files da3.h and da3.cpp. The templates should be implemented entirely in the header file. The non-templates should be prototyped in the header and implemented in the source, as usual.

Be sure to follow the coding standards. Standard 3A (“Requirements on template parameter types must be documented.”) and standard 3B (“Exceptions thrown by each function must be documented.”) now apply. You do not need to follow standard 3C.

In the files da3.h and da3.cpp, you may include any other functions or classes that you wish. These will not be tested; however, they must follow the coding standards. Also, generally, use of the C++ Standard Library is legal in this assignment; some exercises do have restrictions on how it may be used, however.

Test Program

I have written a single test program for all four exercises: da3_test.cpp. If you compile and run your package with this program (unmodified!), then it will test whether your package works properly.

Do not turn in da3_test.cpp.

Exercise A — Does It Throw?

Purpose

This exercise is all about calling code that may throw an exception, and catching the exception appropriately.

Instructions

Write a function template doesItThrow, prototyped as

template <typename Function>
bool doesItThrow(Function theFunc);

Function doesItThrow is given a pointer to a function, or something that acts like a function. It then calls this function with no parameters, catching any exceptions that result. Finally, it returns true if theFunc threw an exception, false otherwise.

Example usage:

void myFunc()
{ throw std::exception("Hi!"); }

bool result = doesItThrow(myFunc);

After the above call to doesItThrow, result should be true.

Other requirements:

Exercise B — Divide or Throw

Purpose

In this exercise you are to write code that may generate an error condition it cannot handle. Your code is to signal the client by throwing an exception. This exercise also requires you to deal with iterators that have few operations available.

Instructions

Write a function template divideAll, prototyped as

template <typename ForwardIterator, typename Number>
void divideAll(ForwardIterator first,
               ForwardIterator last,
               const Number & denom);

Function divideAll is given a range, specified as two iterators, in the usual way (as described in class), and a number (“denom”) to divide every item in the range by.

If the expression “(denom == 0)” is true, then divideAll should throw some appropriate exception and leave the range unchanged. Otherwise, it should divide every item in the range by denom, with the results being stored in the range.

Example usage:

double arr[3] = { 1.0, 3.0, 2.0 };
divideAll(arr, arr+3, 2.0);

The above should result in array arr holding values approximately equal to 0.5, 1.5, 1.0, in that order.

Other requirements:

Exercise C — Descending Sort

Purpose

This exercise requires you to deal with iterators and to do sorting, either by writing the code yourself, or by figuring out how to use the C++ STL to do a descending sort (hint: Google?).

Instructions

Write a function sortDescendingInt, prototyped as

template <typename RandomAccessIterator>
void sortDescendingInt(RandomAccessIterator first,
                       RandomAccessIterator last);

Function sortDescendingInt is given a range, which holds zero or more ints. The range is specified as two iterators, in the usual way (as described in class). It sorts the range in descending order, that is, by the operator “>”.

Example usage:

std::vector<int> v;
v.push_back(1);
v.push_back(5);
v.push_back(2);
v.push_back(0);
v.push_back(5);
sortDescendingInt(v.begin(), v.end());

The above should result in v holding the values 5, 5, 2, 1, 0, in that order.

Other requirements:

Exercise D — Print Reverse

Purpose

In this exercise, you are to implement a simple recursive algorithm.

Instructions

Write a recursive function (NOT a template) printRev, prototyped as

void printReverse(const std::string & printMe,
                  std::ostream & theStream);

Function printReverse is given a string and an ostream, or an object of a derived class. It prints the given string backwards, character by character, to the given stream.

Example usage:

printReverse("abc def", std::cout);

The above should print “fed cba” to cout.

Other requirements:


CS 311 Spring 2008: Assignment 3 / Updated: 18 Feb 2008 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!