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

Questions

  1.  
    TRUE   FALSE   A vector declaration always 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).

    
        double average(const vector<double> & v,
                       vector<double>::size_type start,
                       vector<double>::size_type end_with)
        {
            double sum = 0.0;
            for (vector<double>::size_type p = start; p <= end_with; ++p)
            {
                sum += v[p];
            }
            return sum / (end_with - start + 1);
        }
    
    

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

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

    Also, during entry, elements are added using the vector function push_back. During processing, however, we could use the []/subscript mechanism to step from one element's position to the next. Such a position is not actually related to the element itself except as a measure of its position relative to the first item in the vector. Because of this, such a value  ( must / need not )  be sent to a function along with the vector to whose elements we desire access.

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

        #include <vector>
    
        inline 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;
        }
    
        ...some_function(...)
        {
            vector<long> heights = { 15, 14, 17, 18, 17, 13, 15, 16 };
            vector<long>::size_type where;
    
    
            where = locate(heights, 17);
    
            return ...;
        }
    
    
    
        where would hold the value 2.  This is because the function starts
        looking at the first position (0) and moves up from there toward the
        end.  It stops when it finds the first occurrance of the find_me value.
    
    
  5. Do you need to use an ampersand (&) on the formal argument to pass vector arguments by reference?

    
        Yes.  This is always the syntax that signifies to the compiler that a
        formal argument is to refer to the actual argument's memory location.
    
    

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

    
        This will make a copy of the entire vector's contents.  This would be
        tremendously wasteful on memory and take a good deal of time as well.
    
    

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

    
        This makes sure the vector's contents don't change during the function
        call even though we have a reference to its original memory location.
        This acts as a sort of fast pass-by-value causing the argument to act
        just like a value argument except without the copying and extra space.
    
    
  6. Rewrite the locate function above to search a vector of char values instead of an vector of long values. You need show only the code that must change.

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

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

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

    
        1) Their data types are different.
        2) The vector has no *find* functions.
        3) The vector has no erase, replace, or insert functions.
        4) The vector has no operators to help with input or output.
        5) The vector has no operators to help with concatenation.
        6) The vector's elements would be treated separately rather than as
           a group.
    
    
  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 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 vec[4] is short
    8. the data type of the 4 above is vector<short>::size_type
    9. the data type of vec is vector<short>
  9. What are we saying when we pass start and end (before) size_types to a vector processing function?

    
        We are saying that the function should start processing the vector
        elements at start and end before it gets to end — by one
        position.
    
    

    What is different about passing only the vector itself into the function (and not the size_types, too)?

    
        This implies that the entire vector's contents should be processed
        by the function.
    
    
  10. A vector in math is a sequence of numbers. (This is (pretty much) directly analogous to a vector in C++ whose [base] data type is numeric.)

    To add two mathematical 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 = { 6 5 5 8 6 }
    
    

    You should be able to code 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 5 5 8 6 }
    
    

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

    
        void vector_sum(vector<short> & v3,
                        const vector<short> & v1,
                        const vector<short> & v2)
        {
            v3.clear();
            if (v1.size() == v2.size())
            {
                for (vector<short>::size_type p = 0; p != v1.size(); ++p)
                {
                    v3.push_back(v1[p] + v2[p]);
                }
            }
            return;
        }