The Problem We are going to work on 2D vectors. 2D vector as a matrix You rememb
ID: 3593956 • Letter: T
Question
The Problem We are going to work on 2D vectors. 2D vector as a matrix You remember matrices, don't you? We are going to do some simple manipulation of a heins mestor yidaesting hw arie andmgba sealar. You watched 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).Explanation / Answer
main.cpp
#include <iostream>
#include "lab07_functions.h"
using std::cout; using std::cin; using std::endl;
int main() {
matrix m1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
matrix m2 = {{3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
// cout << matrix_to_str(m1);
// cout << endl;
// cout << matrix_to_str(m2);
// cout << endl;
// cout << matrix_to_str(add(m1, m2));
cout << matrix_to_str(scalar_multiply(m1, 3));
return 0;
}
lab07_functions.cpp
#define N_LINE " "
#define SPACE " "
#include<sstream>
using std::ostringstream;
#include<iomanip>
using std::setw;
#include "lab07_functions.h"
string matrix_to_str(const matrix &m1, size_t width){
ostringstream s;
for (matrix_row r : m1){ // for each matrix row
for (long l : r){ // loop each long in the row
s << setw(width); // set width to value
s << l; // insert value to the stream
}
if (r != m1.at(m1.size() - 1)){ // if the row isn't the last row
s << N_LINE; // append a new line
}
}
return s.str(); // return string member of the stream
}
bool same_size(matrix &m1, matrix &m2){
// if the size of the vectors of the rows are equal and the
// size of the first vector of longs are equal then true
return (m1.size() == m2.size() && m1.at(0).size() == m2.at(0).size());
}
matrix add(matrix &m1, matrix &m2){
matrix m;
// if the vectors are the same size
if (same_size(m1, m2)){
for (int i = 0; i < m1.size(); ++i){ // for each row in the vector
matrix_row r; // create new row
for (int u = 0; u < m1.at(0).size(); ++u){ // for each value in vector
// add values at the corresponding locations and push it into the new
r.push_back(m1.at(i).at(u) + m2.at(i).at(u));
}
// add the row to the new matrix
m.push_back(r);
}
}
return m; // return the matrix
}
matrix scalar_multiply(matrix &m, long val){
for (matrix_row &r : m){ // for each row in the matrix
for (long &l : r){ // for each value in the row
l *= val; // multiply the value by the parameter
}
}
return m; // return the matrix
}
lab07_functions.h
#ifndef MATRIX_TYPE
#define MATRIX_TYPE
#include<string>
using std::string;
#include<vector>
using std::vector;
/*
very convenient. If you want to change the type
stored in the matrix, you only have to change the
single template type in matrix_row
*/
using matrix_row = vector<long>;
using matrix = vector<matrix_row>;
/*
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
*/
string matrix_to_str(const matrix &m1, size_t width=3);
/*
true if the two matrices have the same shape
false otherwise
*/
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)
*/
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)
*/
matrix scalar_multiply(matrix &m, long val);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.