This lab will help you practice with vectors, function overloading, and libraries.
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:
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:
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.
This assignment is (Level 3).