Topical Information

The purpose of this quiz is to give you a chance to review your knowledge of topics involving vectors in C++.

Quiz Information

Directions:

Choose all appropriate answers to multiple choice questions. On TRUE/FALSE questions, choose only TRUE or FALSE. Pick the most correct word/phrase you can think of for fill-in-the blank (it doesn't have to be exact). On all other questions, follow the instructions given.

Questions

  1.  
    TRUE  FALSE  A vector declaration tells the (base) type, name, and maximum size of the vector.
    TRUE  FALSE  Luckily, vectors don't have a maximum size (that will concern us).
    TRUE  FALSE  The vector's (base) type can be any data type — even void.
    TRUE  FALSE  To use vectors, we must include the library header <container>.
  2. Write a function which, when called as 'average(vec, 3, 15)' will return the average of the elements of the vector vec between positions 3 and 15 (inclusive). Overload this function with a version that takes just the vector — no positions.

    
    double average(const vector<double> & v,
                   vector<double>::size_type beg,
                   vector<double>::size_type end)
    {
        end = end >= v.size() ? (v.size() == 0 ? v.size() ? v.size()-1) : end;
        end = end < beg ? beg : end;
        double sum = 0.0;
        for (vector<double>::size_type p = beg; p <= end; ++p)
        {
            sum += v[p];
        }
        return sum / (end - beg + 1);
    }
    
    

    Is it normal for both positions passed to this function to be included during processing? How do we usually specify the ends of a range of vector positions that are to be processed?

    
    No.  Normally the beginning is inclusive but the ending is exclusive.
    
    
  3. We need   ( branch(es) / loop(s) )   to process a vector's elements. During the entry of data elements, this is often a(n) indefinite style loop (since we don't know yet how many items there are). Once data is entered and we are processing the elements, the loop used is typically a definite/for loop.

    Also, during entry, elements are added to the vector using the function push_back. During processing, however, we use the []/subscript mechanism to access each element in turn. This mechanism always results in a reference to the element it currently 'points' to. If we don't want to change the element by accident, we should use it on a constant vector — same mechanism, but the resulting reference is not change-able because of the vector's properties.

  4. What would be the result of the following code fragment? (i.e. What is stored in the variable where?)

        #include <vector>
    
        vector<long>::size_type locate(const vector<long> & data, long find_me);
    
        ...some_function(...)
        {
            vector<long> heights;
            vector<long>::size_type where;
    
            // code to fill in heights vector
            // assume here that the contents are:
            // { 15, 14, 17, 18, 17, 13, 15, 16 }
    
            where = locate(heights, 17);
    
            return ...;
        }
    
        vector<long>::size_type locate(const vector<long> & data, long find_me)
        {
            vector<long>::size_type i = 0;
            while ((i != data.size()) && (data[i] != find_me))
            {
                i++;
            }
            return i;
        }
    
    
    
    2
    
    
    
  5. Do you need to use an ampersand (&) on the formal argument to pass vector arguments by reference?

    
    Yes.  Nothing is automatically reference in C++.
    
    

    Why should you not pass a vector argument by value to a function?

    
    Anything larger than a simple, built-in type should be passed as some
    kind of reference rather than by value.  The copying of such larger
    memory structures can be exorbitant.
    
    

    What does a const mean on a formal vector argument to a function which is being passed by reference?

    
    It means not to change the values in the function during that function's
    execution.  This effectively cancels out the ability of the reference to
    change the values.  But, the reference does still avoid making expensive
    copies of the original values into local memory space.
    
    
  6. Rewrite the locate function above to search a vector of char values instead of a vector of long values. You need show only the code that must change.

    
    vector<char>::size_type locate(const vector<char> & data, char find_me)
    
    and
    
        vector<char>::size_type i = 0;
    
    
    Just change all occurances of long to char.
    
    
  7. Write a function to read in a vector of characters. (Hint: What character ends the input?)

    
    void get_line(vector<char> & v)
    {
        char ch;
        cin.get(ch);
        while (ch != '\n')
        {
            v.push_back(ch);
            cin.get(ch);
        }
        return;
    }
    
    

    How is a vector of char different from a string? (There are at least five differences.)

    
      i) no *find* functions
     ii) no erase, insert, assign, replace functions
    iii) no >> or << operations
     iv) no + operator (+= is kinda like push_back)
      v) it is a collection of individuals rather than a single whole
    
    
  8. Given a vector declared as 'vector<short> vec;', answer each of the following:

    1. the vector contains short elements [type]

    2. the vector always contains vec.size() elements [number]

    3. the vector's valid positions are always in the range [0, vec.size())

    4.  
      TRUE  FALSE  vec[3] refers to the 4th element
    5.  
      TRUE  FALSE  vec[1] refers to the 1st element
    6.  
      TRUE  FALSE  vec refers to the whole vector
    7. the data type of 4 (when used to access part of vec) is vector<short>::size_type

    8. the data type of vec[4] is short

    9. the data type of vec is vector<short>

  9. What are we saying when we pass start and end (before) positions to a vector processing function?

    
    To process the starting element and all elements up to but not including
    the ending element.
    
    

    What is different about passing just the vector into the function (not also the positions)?

    
    This implies that all elements of the vector are to be processed —
    not just a specific sub-range within the vector.
    
    
  10. A vector in math is a sequence of numbers. This is (pretty much) directly analogous to a vector in C++ whose data type is numeric. To add two vectors, simply add all of their elements in sequence. If the two vectors are of different lengths, the result is undefined (so an empty vector would be the result).

    For example:

    
        v1 = { 1 2 3 4 5 }
        v2 = { 5 3 2 4 1 }
    
        v3 = v1 + v2
           = { 1+5 2+3 3+2 4+4 4+1 }
           = { 6 5 5 8 6 }
    
    

    You should be able to write this in a C++ program (using a function for vector addition) as:

    
        vector<short> v1, v2, v3;
        for (short i = 1; i <= 5; i++)
        {
            v1.push_back(i);      // fills with { 1 2 3 4 5 }
            v2.push_back(5-i+1);  // fills with { 5 4 3 2 1 }
        }
        vector_sum(v3, v1, v2);
        // now v3 contains { 6 6 6 6 6 }
    
    

    Write such a function to add two vectors. (You can choose a different type besides short for the vector elements, if you like.)

    
    void vector_sum(vector<short> & ans,
                    const vector<short> & one,
                    const vector<short> & two)
    {
        ans.clear();
        if (one.size() == two.size())
        {
            for (vector<short>::size_type e = 0; e != one.size(); ++e)
            {
                ans.push_back(one[e] + two[e]);
            }
        }
        return;
    }