Data Structures, Algorithms, & Applications in C++
Chapter 1, Exercise 11

The new code is given below and in the file countWithException.cpp.


template <class T>
int count(T a[], int n, const T& value)
{// Return number of occurrences of value in a[0:n-1].
   if (n < 1)
      throw "n must be >= 1";

   int theCount = 0;
   for (int i = 0; i < n; i++)
      if (a[i] == value)
         theCount++;
   return theCount;
}