Question: Partner CSE 232, Lab Exercise 7 Choose a partner in the lab to work with on this exercise. Two people should work at one computer.

Partner
CSE 232, Lab Exercise 7
Choose a partner in the lab to work with on this exercise. Two people should work at one computer. Occasionally switch who is typing.
The Problem
We are going to work on two things:
Writing templated code
2D vectors
2D vector as a matrix
You remember matrices don't you? We are going to do some simple manipulation of a matrix, namely: adding two matrices and multiplying a matrix by a scalar.
Matrix
A matrix is a 2 dimensional (rows and columns) data structure. It has a shape indicated by the number of rows and the number of columns. Though I suppose a matrix could have uneven sized rows, this doesn't usually happen in practice so a matrix is always rectangular, potentially square (based on its shape).
Matrix operations
We will perform two operations on our matrices, yielding a new matrix as a result.
The first is scalar multiplication. Regardless of the size or shape, if the matrix is not empty we multiply the scalar value by every entry in the matrix, yielding a new matrix. We do this for every entry in the matrix.
The second is addition. The shape of the two matrices must be the same for addition to go forward. If the shapes are the same and they are both not empty, we add the same row/col element of each argument matrix into the same row/col element of a new matrix, yielding the new matrix. We do this for every element in the two matrices.
Requirements
Scalar Multiplication
Addition of two matrices
We provide a skeleton.cpp that you can modify for the requirements of this lab. You can insert the required functions into this main for the lab. We will use a vector
In main we provide two using definitions to make things a little easier, to wit:
templateusing matrix_row = vector ;
templateusing matrix = vector >;
Thus you can declare a new matrix with the matrix type, and if you need to you can declare an individual row as a matrix_row type. These types are used in the main program.
Function Declarations
The functions are clearly described in the skeleton.cpp file provided, read them there.
"""
#includeusing std::cout; using std::endl; #include using std::setw; #include using std::vector; #include using std::string; #include using std::ostringstream; template using matrix_row = vector ; template using matrix = vector >; /* nicely print a matrix. Should have row/column alignment converts it to a string (doesn't print to cout!!!) uses width to space elements (setw). Default is 3 */ template string matrix_to_str(const matrix &m1, size_t width=3){ } /* true if the two matrices have the same shape false otherwise */ template bool same_size(matrix & m1, matrix & m2){ } /* matrices must not be empty and must be the same shape: - if true, return a new matrix that adds m1+m2 - if false, return an empty matrix (no elements) */ template matrix add(matrix & m1, matrix & m2){ } /* matrix must not be empty: - if true, multiply T scalar value by m - if false, return empty matrix (no elements) */ template matrix scalar_multiply(matrix &m, T val){ } int main(){ matrix m1{ {0,1,2}, {3,4,5}, {6,7,8} }; matrix m2{ {0,1}, {2,3}, {4,5} }; matrix m3; matrix result; // case 1 result = add(m1,m1); if (! result.empty() ) cout """
Hints
Start out by solving Case 1 and commenting out the rest of the main code. When you've solved that then include back Case 2 (so now working on Case 1 and Case 2) and so on.
You can make a temporary row (of type matrix_row) and push_back values on to that. You can then push_back the row onto a matrix (or type matrix). You can reuse the row in the your loop, but remember to .clear() it first.
Make sure the printing of the matrix is nicely aligned (column and row) as shown in the output below
./a.out Case 1 0 1 2 3 4 5 6 7 8 Case 2 0 3 6 9 12 15 18 21 24 Case 3 could not multiply Case 4 0 2 4 6 8 10 12 14 16 Case 5 could not add
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
