Topical Information

The purpose of this quiz is to give you a chance to practice the topics involved with nD vector structures in C++.

Quiz Information

Questions

  1.  
    TRUE   FALSE   A vector that consists of strings is actually 3-dimensional.
    TRUE   FALSE   A vector structure used to represent the consistency inside a block of cheese will be 2D.
    TRUE   FALSE   Heat flow around the surface of a box would be most compactly stored as a 3D vector.
  2. Describe the best way to store heat flow information for the surface of a hollow box.

    
        This could be done in a couple of ways.  The first is to just make a 3D
        vector structure of the dimensions of the box and only fill in and
        calculate the heat data on the outside elements.  This wastes all the
        memory for the elements in the middle, though.  Depending on the size of
        the box, this could be huge!
    
        Another way to do it is to take the sides of the box apart.  There would
        be two of each shape:  top & bottom, left & right, front & back.
        The hard part would be calculating the heat flow at the edges as they would
        have to map from one separate piece to another.  But to save memory, it
        could be done.  Would this save memory?  Well, we'd still want to store
        these as a single 3D vector, so it would waste space at the corners.  We
        could draw this out and calculate how much memory is wasted in the corner
        regions vs how much memory was wasted with the interior of the box and set
        up/solve the inequality.
    
        Then the only question is, is overcoming the edge heat flow difficulty
        worth it to save any memory the sides storage technique can?  This has to
        be answered by the individual/team implementing the solution.
    
    

  3. Given a multi-dimensional vector structure declared as 'vector< vector<double> > mat', answer each of the following:

    1. at any time, the structure contains mat.size()*mat[0].size() elements (how many? this may be a formula...)
    2.  
      TRUE   FALSE   mat[2][4] represents the 5th element in the 3rd row
    3.  
      TRUE   FALSE   mat[2] represents the entire 3rd row
    4.  
      TRUE   FALSE   mat represents the entire vector structure
    5.  
      TRUE   FALSE   mat[][4] represents the entire 5th column
    6. the data type of mat[2][4] is double
    7. the data type of mat[2] is vector<double>
    8. the data type of mat is vector<vector<double>>

  4. For each dimension an nD vector structure has, we need (at least) one loop(s) to process its data. 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 is entered and we are processing the elements, the loop used is typically a definite/finite-style loop.

  5. If you plan to use a 2D vector structure whose base element type is long integer, show a set of typedefs or using aliases which would prove useful to simplify the program's declarations and argument types.

    
        Either:
    
            typedef vector<long> Lvec1D;
            typedef Lvec1D::size_type Lvec1D_sz;
            typedef vector<Lvec1D> Lvec2D;
            typedef Lvec2D::size_type Lvec2D_sz;
    
        Or:
    
            using Lvec1D = vector<long>;
            using Lvec1D_sz = Lvec1D::size_type;
            using Lvec2D = vector<Lvec1D>;
            using Lvec2D_sz = Lvec2D::size_type;
    
    

  6. Use the above typedefs/using aliases to code a function that will search an entire 2D vector structure of longs to find the smallest value amongst the data. You should take the vector structure as input to the function and return both the smallest value and the position (row and column) of the smallest value.

    
        long smallest(const Lvec2D & vec, Lvec2D_sz & row, Lvec1D_sz & col)
        {
            if (!vec.empty())
            {
                row = 0;
                col = 0;
                for (Lvec2D_sz r = 0; r < vec.size(); ++r)
                {
                    for (Lvec1D_sz c = 0; c < vec[r].size(); ++c)
                    {
                        if (vec[r][c] < vec[row][col])
                        {
                            row = r;
                            col = c;
                        }
                    }
                }
            }
            return vec.empty() ? 0 : vec[row][col];
        }
    
    

    Overload this function with an inlined companion that will only return the smallest value and not its coordinates within the structure.

    
        inline long smallest(const Lvec_2D & vec)
        {
            Lvec2D_sz row;
            Lvec1D_sz col;
            return smallest(vec, row, col);
        }
    
    

  7. Write a function that will accept a 2D vector structure containing short integers and return a vector containing the average of the elements along each row of the input 2D structure.

    
        vector<double> row_avgs(const vector<vector<short>> & vec)
        {
            vector<double> avgs;
            for (const auto & row : vec)
            {
                double sum = 0.0;
                for (auto elem : row)
                {
                    sum += elem;
                }
                avgs.push_back(sum/row.size());
            }
            return avgs;
        }
    
    

    Write a similar function to return a vector containing the averages of each column of a 2D structure of short integers given by the caller.

    
        vector<double> col_avgs(const vector<vector<short>> & vec)
        {
            vector<double> avgs;
            for (vector<short>::size_type col = 0; col < vec[0].size(); ++col)
            {
                double sum = 0.0;
                for (vector<vector<short>>::size_type row = 0; row < vec.size(); ++row)
                {
                    sum += vec[row][col];
                }
                avgs.push_back(sum/vec[0].size());
            }
            return avgs;
        }
    
    

    Can these two functions be overloads of one another? Why/Why not?

    
        No.  They have the same argument lists, so the name must distinguish them
        one from the another.
    
    

  8. Show how to create a set of parallel vector (structures) that can contain the following: grades for each student on all course assignments throughout the semester, names for each student, titles for each assignment, and weighting factors for each assignment.

    Give both declarations for the variables and a drawing of how the vectors align in parallel.

    
        vector<vector<double>> grades;
        vector<string> names, titles;
        vector<double> weights;
    
              +----+----+----+----+----+----+----+----+----+----+
       titles |    |    |    |    |    |    |    |    |    |    |
              +----+----+----+----+----+----+----+----+----+----+
     names       0    1    2    3    4    5    6    7    8    9
     +----+   +----+----+----+----+----+----+----+----+----+----+
     |    | 0 |    |    |    |    |    |    |    |    |    |    | g
     +----+   +----+----+----+----+----+----+----+----+----+----+ r
     |    | 1 |    |    |    |    |    |    |    |    |    |    | a
     +----+   +----+----+----+----+----+----+----+----+----+----+ d
     |    | 2 |    |    |    |    |    |    |    |    |    |    | e
     +----+   +----+----+----+----+----+----+----+----+----+----+ s
     |    | 3 |    |    |    |    |    |    |    |    |    |    |
     +----+   +----+----+----+----+----+----+----+----+----+----+
                 0    1    2    3    4    5    6    7    8    9
              +----+----+----+----+----+----+----+----+----+----+
      weights |    |    |    |    |    |    |    |    |    |    |
              +----+----+----+----+----+----+----+----+----+----+
    
    

  9.  
    TRUE   FALSE   1D can represent a row or column of data.
    TRUE   FALSE   In C++, 2D represents rows-by-columns — not the other way around.
    TRUE   FALSE   3D can represent planes of rows of elements (columns).
    TRUE   FALSE   The dimensions represented in a multi-dimensional vector structure can be physical, temporal, or even just societal/by definition.
  10. Describe an nD vector structure to represent all the books in a particular row at the library — content and all.

    
        Let's imagine that a vector of strings is a page of text.  A
        collection of pages will form a chapter.  A collection of chapters would
        form a book.
       
        Of course, books are arranged along a shelf and shelves are stacked 4-6
        high on a rack.  Racks are placed side-by-side along a row.
    
    

    How many dimensions is your structure? How can you tell? (Hint: How many dimensions is just a single page from a book?)

    
        The pages are 2D themselves.  This makes the chapters 3D and the books 4D.
        Then we add to that the shelf, rack, and row dimensions to make the whole
        thing 7D.
    
    

    Show typedefs for your structure. (You can skip the position/size types — just the typedefs for the data.) (Or you can utilize using aliases if you prefer.)

    
        Either:
    
            typedef string Line;
            typedef vector<Line> Page;
            typedef vector<Page> Chapter;
            typedef vector<Chapter> Book;
            typedef vector<Book> Shelf;
            typedef vector<Shelf> Rack;
            typedef vector<Rack> Row;
    
        Or:
    
            using Line = string;
            using Page = vector<Line>;
            using Chapter = vector<Page>;
            using Book = vector<Chapter>;
            using Shelf = vector<Book>;
            using Rack = vector<Shelf>;
            using Row = vector<Rack>;