logo CodeStepByStep logo

flatten

Language/Type: C++ Grid Vector collections
Related Links:

Write a method named flatten that accepts a reference to a Grid of integers as a parameter and that returns a Vector with the contents of the original grid "flattened" into a one-dimensional vector. For example, if the given grid is declared:

Grid<int> matrix {
    {  3,   8,  12},
    {  2,   9,  17},
    { 43,  -8,  46},
    {203,  14,  97}
};

Then the call of flatten(matrix) should return the following vector:

{3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97}

Your code should work for a grid of any size, even one with 0 rows or columns. Your method should not modify the grid that is passed in.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.