CPTR 124 Fundamentals of Programming


In this lab you will write some functions that work with the C++ vector data type.


  1. Teams

    You may work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.

  2. Create a new project

    Create a new project and add the header file vectorstuff.h. vectorstuff.h contains the prototypes of functions that you are to implement.

  3. Implement the functions

    Create a new C++ source file named vectorstuff.cpp that implements all the functions declared in the vectorstuff.h header file. The specifications for the functions are listed here:

    • void print(const vector<int>& a) { ... }

      Prints the contents of vector a. This can be the exact code we developed in class.

      The contents of vector a are not affected by this function.

    • int max(const vector<int>& a) { ... }

      Returns the maximum value in vector a. The behavior of this function is undefined if the vector is empty.

      The contents of vector a are not affected by this function.

    • int min(const vector<int>& a) { ... }

      Returns the minimum value in vector a The behavior of this function is undefined the vector is empty.

      The contents of vector a are not affected by this function.

    • int evens(const vector<int>& a) { ... }

      Returns the number of even elements found in vector a. For example, if list contains the elements 1, 2, 3, 4, 5, and 6, the call evens(list) would return 3, since 2, 4, and 6 are the even numbers in the vector.

      The contents of vector a are not affected by this function.

    • int find(const vector<int>& a, int seek) { ... }

      Returns the location (zero is the first position) of the first occurrence of seek within a. Said another way, it returns the lowest index in a that contains the value seek. If seek cannot be found within a, -1 is returned, since -1 is not a legal index within any C++ vector.

      The contents of vector a are not affected by this function.

    • int count(const vector<int>& a, int seek) { ... }

      Returns the number of times element seek appears in a.

      The contents of vector a are not affected by this function.

    • bool equals(const vector<int>& a, const vector<int>& b) { ... }

      Returns true if vectors a and b contain exactly the same elements in the same order; otherwise, the function returns false.

      The contents of neither vector a nor vector b are affected by this function.

    • void sort(vector<int>& a) { ... }

      Physically rearranges the elements of a so they are in ascending order.

      For example, if list contains the elements 2, 1, 3, 1, 5, and 2, the call sort(list) reorders list to contain 1, 1, 2, 2, 3, 5.

      The function will necessarily affect the contents of the vector.

    • bool remove_first(vector<int>& a, int del) { ... }

      Removes the first occurrence of the value del from the vector a. (The first occurence is the one with the lowest index.)

      The function returns true if del was located and removed; otherwise, it returns false.

      The function can affect the contents of the vector.

      For example, if the vector originally contains

                      23, 45, 14, 45, 19, 11
                   
      after removing 45 it would contain
                      23, 14, 45, 19, 11
                   
      Notice that only the first occurrence of 45 was removed.

      (Hint: Locate the element to remove, and then shift forward by one position all the elements that follow it. Be sure to decrease the vector's size by one.)

    • int remove_all(vector<int>& a, int del) { ... }

      Removes all occurrences of the value del from the vector a.

      The function returns the number of elements removed; if it removes nothing because the element to remove is not found in the vector, the function returns zero.

      The function can affect the contents of the vector.

      For example, if the vector originally contains

                      23, 45, 14, 45, 19, 11
                   
      after removing 45 it would contain
                      23, 14, 19, 11
                   
      Notice that all occurrences of 45 were removed.

    • int subsequence(const vector<int>& a, const vector<int>& b) { ... }

      Vectors a and b both represent sequences of integers. For the purposes of this assignment, a subsequence is a continuous sequence of numbers that are part of a potentially larger continuous sequence of numbers. The concept is best explained by some examples:

      • If a is the sequence
                            23, 4, 19, -4, 0, 3
                            
        and b is the sequence
                            19, -4, 0
                            
        b is a subsequence of a:
                            23, 4, 19, -4, 0, 3
                            
        and the function would return 2, since 2 is the starting index of b's sequence within a. The complete b sequence is identical to a continuous sequence within a.

      • If a is the sequence
                            23, 4, 19, -4, 0, 3
                            
        and b is the sequence
                            19, 0
                            
        b is not a subsequence of a, since b is missing -4, and so the function would return -1 to indicate b is not a subsequence of a.

      • If a is the sequence
                            23, 4, 19, -4, 0, 3
                            
        and b is the sequence
                            19, -4, 0, 3, 5
                            
        b is not a subsequence of a,
                            23, 4, 19, -4, 0, 3, 5
                            
        since even though b overlaps a, b has an extra element that is not part of a. The function would return -1 to indicate b is not a subsequence of a.

      • If a is the sequence
                            23, 4, 19, -4, 0, 3
                            
        and b is the sequence
                            19, 0, -4
                            
        b is not a subsequence of a even though it contains the same elements as a segment of a, since b's sequence is in a different order. The function would return -1 to indicate b is not a subsequence of a.

      • If a is the sequence
                            23, 4, 19, -4, 0, 3
                            
        and b is the sequence
                            23, 4, 19, -4, 0, 3
                            
        b is a subsequence of a since b overlaps a perfectly
                            23, 4, 19, -4, 0, 3
                            
        and the function would return 0, b's starting index within a. Every sequence, therefore, is a subsequence of itself.

      • The empty sequence is a subsequence of every sequence. The empty sequence begins at position 0 in any other sequence.

      This function returns the position of the sequence represented by b within a. If vector b appears more than once in a, return the lowest index (that is, the index of the first occurrence).

      Neither vector a nor vector b is affected by this function.

    The specification that a function "does not affect the vector" means that the contents of the vector are unchanged by the function; the function may not reassign, rearrange, or otherwise change the elements of the vector.

    None of the functions specified above should do any input or output. This means that neither cout nor cin should appear in any of the functions. You are welcome to add printing statements during development for debugging purposes, but you should remove or comment out these printing statements when you are ready to submit your program for testing.

    You may not modify the contents of the vectorstuff.h header file.

  4. Check out

    Your finished vectorstuff.cpp file will be evaluated for correctness and compliance. Before showing me your code, be sure that it complies with the following requirements:

    1. Your name and your partner's name should appear in the in a comment at the top of the source code. Failure to provide such a comment results in an immediate failed test.
    2. Ensure that your vectorstuff.cpp compiles correctly with the following very simple test file. If your vectorstuff.cpp file does not compile with this very simple test program, it will not compile with my test program either. If your code will not compile, it counts as a failed test. This simple test file is very minimal and is designed merely to verify that your code satisfies the compiler; it does not begin to demonstrate the correctness of your code's logic. You should write your own test code to verify your functions' correctness.

    You may provide to me your vectorstuff.cpp file for testing up to three times before its due date. The final test determines the score based on the number of functions that pass the tests. 10 points means all 10 functions passed all the tests, 9 points means one of the functions failed one or more tests, 8 points means two of the functions failed one or more tests, etc. After the final check you should submit your C++ source file to eclass.