Topical Information

This lab will help you practice with vectors, function overloading, and libraries.

Program Information

Two useful functions that are not provided in the standard library are finding the index of an element in a vector and finding the index of the beginning of a sub-sequence in a vector.

For instance, the character 'e' appears at position 2 in the vector { 'T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x' } . However, it does not appear at all in the vector { 'c', 'a', 't', 's', ' ', 'b', 'a', 't', ' ', 'a', 'b', 'o', 'u', 't', ' ', 'y', 'a', 'r', 'n' } . So our function could return a valid index if the element is found (2 above) or the size of the vector (an invalid index; 19 above) when it can't find the element.

Sub-sequences are similar. The sequence { 'h', 'e' } appears at position 1 in { 'T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x' } . But, again, it doesn't appear at all in { 'c', 'a', 't', 's', ' ', 'b', 'a', 't', ' ', 'a', 'b', 'o', 'u', 't', ' ', 'y', 'a', 'r', 'n' } . And we can return the vector's size (19 here) or a valid index (such as 1 here) to indicate where the sub-sequence was found.

Write these two functions -- both named 'find'. One will take a vector of characters and a single character and return either an integral position or the size of the vector. The second will take two vectors of characters and return either an integral position or the size of the first vector. Place them in a library called vecextra (since the name vector is already taken).

Some further examples:

Examples of what your functions should do...
vector Contents In Which We Are Looking For returned Position Which Find Is Called
{ 'a', 'p', 'p', 'l', 'e' } 'z' 5 element
{ 'a', 'p', 'p', 'l', 'e' } 'l' 3 element
{ 'a', 'p', 'p', 'l', 'e' } 'e' 4 element
{ 'a', 'p', 'p', 'l', 'e' } { 'e' } 4 sub-sequence
{ 'a', 'p', 'p', 'l', 'e' } { 'p', 'p' } 1 sub-sequence
{ 'a', 'p', 'p', 'l', 'e' } { 'p', 'e' } 5 sub-sequence

Other examples which can catch common coding errors:

Watch for these kinds of results -- they're all wrong!
vector Contents In Which We Are Looking For [Invalid] returned Position Which Find Is Called
{ 'a', 'p', 'p', 'l', 'e' } { 'a', 'p', 'e' } 0 sub-sequence
{ 'a', 'p', 'p', 'l', 'e' } { 'a', 'p', 'l' } 0 sub-sequence
{ '1', '1', '1', '1', '2' } { '1', '1', '2' } 5 sub-sequence

Next write a test application for this library. A test application is basically a driver that tests all the functions in the library.

Thought Provoking Questions

  1. What arguments does each find function take? Are they changed? What special care should you take with them?
  2. What value is returned by your functions? What type is it and what does it represent?
  3. What care does a caller of your functions have to take with this return value? (Can they immediately assume it is a valid index?)
  4. How does the compiler distinguish which of your functions is being used for a particular call? (They have the same name, after all...)
  5. What is a test application? Oh...I see. So what's a driver, then?
  6. How do you protect your library from being circularly included?
  7. What changes are needed in your main application (the test app here) to get it to work with the library? What about the compiling process?
  8. How many files does your library consist of? What are they? Which one(s) do you #include?

This assignment is (Level 3).

Options