Colspan and rowspan are computer terms related to creation of columns and or cells in a program for spreadsheets or things of that nature. The colspan meaning "how many across" and the rowspan meaning "how many down" indicate how many columns or rows a cell should take up.
A matrix is symmetric when it is a perfect square and the transpose of the matrix is equal to the original matrix. The implementation is reasonably straightforward and can be done without actually creating the transposed matrix. We simply imagine a dividing line through the main diagonal (top-left to bottom right) and compare the corresponding elements on each side of that diagonal.bool is_symmetric (int* a, int rows, int cols) {if (rows!=cols) return false;for (int row=0, row
I don't know the code, so I can't give it to you. But the algorithm is simple. Matrices are usually stored as 2 dimensional arrays. Say M and N. Then you make a loop, any loop, that goes through each row, during each loop, another loop will go over every single column (so row 1, col 1, then row 1, col 2, then etc.) Each time, the loop goes into row i and column j, add the entries of that row and column from M and N, (or M i,j + N i,j) and let it be the i,j's entry of the sum matrix. Do the code yourself.
col our.... TWO..
follower row
Connecting "col wait sion" doesn't spell anything grammatical.
#include<iostream> #include<array> #include<time.h> int main() { srand ((unsigned)time(nullptr)); // Matrix: 5 rows, 4 columns const size_t rows = 5; const size_t cols = 4; std::array<std::array<size_t, cols>, rows> matrix; // Initialise matrix with random values (range: 0 to 9) for (size_t row=0; row<rows; ++row) for (size_t col=0; col<cols; ++col) matrix[row][col] = rand() % 10; // Print matrix. std::cout << "Matrix:\n\n"; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) std::cout << matrix[row][col] << '\t'; std::cout << std::endl; } std::cout << std::endl; // Initialise array for row and column sums std::array<size_t, rows> row_sum {}; std::array<size_t, cols> col_sum {}; // Calculate sums: for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { row_sum[row] += matrix[row][col]; col_sum[col] += matrix[row][col]; } } // Print sums: for (size_t row=0; row<rows; ++row) std::cout << "Row " << row << " sum = " << row_sum[row] << std::endl; std::cout << std::endl; for (size_t col=0; col<cols; ++col) std::cout << "Col " << col << " sum = " << col_sum[col] << std::endl; std::cout << std::endl; }
#include<stdio.h> unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) { unsigned sum, col; sum = 0; for (col=0; col<width; ++col) sum += sq[row*width+col]; return sum; } unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) { unsigned sum, row; sum = 0; for (row=0; row<width; ++row) sum += sq[row*width+col]; return sum; } unsigned sum_diag (unsigned* sq, const unsigned width) { unsigned sum, row, col; sum = 0; for (row=0, col=0; row<width; ++row, ++col) sum += sq[row*width+col]; return sum; } unsigned sum_anti (unsigned* sq, const unsigned width) { unsigned sum, row, col; sum = 0; for (row=0, col=width-1; row<width; ++row, --col) sum += sq[row*width+col]; return sum; } bool is_magic (unsigned* sq, const unsigned width) { unsigned magic, row, col; magic = sum_row (sq, width, 0); for (row=1; row<width; ++row) if (magic!=sum_row(sq, width, row)) return false; for (col=0; col<width; ++col) if (magic!=sum_col(sq, width, col)) return false; if (magic!=sum_diag(sq, width)) return false; if (magic!=sum_anti(sq, width)) return false; return true; } int main () { const unsigned width = 3; unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}}; unsigned row, col; printf ("Square:\n\n"); for (row=0; row<width; ++row) { for (col=0; col<width; ++col) { printf ("%d ", a[row][col]); } printf ("\n"); } printf ("\n"); if (is_magic((unsigned*)&a, width)) printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0)); else printf ("The square is not magic\n"); return 0; }
#include<iostream> #include<iomanip> #include<vector> #include<string> #include<sstream> using namespace std; const unsigned width = 4; const unsigned height = 3; class matrix { private: vector< vector<unsigned> > m_data; string m_title; public: matrix(string title=""): m_data(height, vector<unsigned>(width)), m_title(title) {} matrix(const matrix& copy): m_data(copy.m_data), m_title(copy.m_title) {} matrix& operator=(matrix rhs) { // Note: assignment does not overwrite the matrix title. for(unsigned row=0; row<height; ++row) for(unsigned col=0; col<width; ++col) operator[](row)[col]=rhs[row][col]; return(*this); } vector<unsigned>& operator[](const unsigned index){return(m_data[index]);} void set_title(const string title){ m_title = title; } string& get_title(){return(m_title);} void show() { cout<<m_title<<'\n'<<endl; for(unsigned row=0; row<height; ++row) { for(unsigned col=0; col<width; ++col) cout<<setw(7)<<(*this)[row][col]; cout<<endl; } cout<<endl; } matrix& operator+=(matrix rhs) { for(unsigned row=0; row<height; ++row) for(unsigned col=0; col<width; ++col) (*this)[row][col]+=rhs[row][col]; return(*this); } matrix operator+(matrix rhs) { matrix result(m_title+" + "+rhs.m_title); for(unsigned row=0; row<height; ++row) for(unsigned col=0; col<width; ++col) result[row][col]=(*this)[row][col]+rhs[row][col]; return(result); } matrix& operator-=(matrix rhs) { for(unsigned row=0; row<height; ++row) for(unsigned col=0; col<width; ++col) (*this)[row][col]-=rhs[row][col]; return(*this); } matrix operator-(matrix rhs) { matrix result(m_title+" - "+rhs.m_title); for(unsigned row=0; row<height; ++row) for(unsigned col=0; col<width; ++col) result[row][col]=operator[](row)[col]-rhs[row][col]; return(result); } }; unsigned input_num (std::string prompt) { unsigned id = 0; while (1) { cout<<prompt<<": "; string input=""; getline (cin, input); stringstream ss (input); if (ss>>id) break; cout<<"Invalid input.\n"; } return (id); } void initialise(matrix& m) { for(unsigned row=0; row<height; ++row) { for(unsigned col=0; col<width; ++col) { stringstream ss; ss<<"Enter a value for "<<m.get_title()<<'['<<row<<"]["<<col<<']'; m[row][col]=input_num(ss.str()); } } cout<<endl; } int main() { matrix matrix_1("matrix_1"); initialise(matrix_1); matrix_1.show(); matrix matrix_2("matrix_2"); initialise(matrix_2); matrix_2.show(); matrix matrix_3 = matrix_1 + matrix_2; matrix_3.show(); matrix matrix_4 = matrix_3 - matrix_2; matrix_4.show(); }
for (int row=0; row<4; ++row) { int i = 1; for (int col=0; col<=row; ++col) { printf ("%d", i); i=(i==1?0:1); } }
Two-dimensional arrays are typically iterated through using nested for loops. If you had a 2-D array alpha with ints ROWS and COLS representing the number of rows and columns respectively, and ints row and col as iterators, the loop would look like this: for (row = 0; row < ROWS; row++){ for (col = 0; col < COLS; col++{ alpha[row][col] = 5; } }
#include<iostream> #include<vector> #include<cassert> class magic_square { friend std::ostream& operator<< (std::ostream&, const magic_square&); private: size_t dim; std::vector<std::vector<size_t>> elem; public: magic_square (const size_t); magic_square (const magic_square& m): dim {m.dim}, elem {m.elem} {} magic_square (magic_square&& m): dim {m.dim}, elem {std::move (m.elem)} {} size_t size () const { return dim*dim; } size_t order () const { return dim; } size_t* operator[] (const size_t); #ifndef NDEBUG bool assert_valid(); #endif DEBUG }; magic_square::magic_square (const size_t order): dim {order}, elem {} { if (dim < 3) throw std::range_error ("Magic square's must have an order greater than 2"); elem.resize (dim); for (size_t row=0; row!=dim; ++row) elem[row].resize(dim); switch (dim % 4) { case (0): { size_t box = dim / 4; size_t lower = 1; size_t upper = dim * dim; for (size_t x=0; x!=dim; ++x) { size_t row = x+1; for (size_t y=0; y!=dim; ++y) { size_t col = y+1; if (((row<=box row>dim-box) && (col<=box col>dim-box)) ((row>box && row<=dim-box) && (col>box && col<=dim-box))) elem[x][y] = lower; else elem[x][y] = upper; ++lower; --upper; } } } break; case (1): case (3): { size_t num=0; size_t row=0; size_t col=(dim-1)/2; while (num<dim*dim) { elem[row][col] = ++num; size_t new_row = !row?dim:row-1; size_t new_col = col+1; if (new_row==dim) { if (new_col==dim) { new_col=col; --new_row; } else new_row=dim-1; } else if (new_col==dim) new_col=0; if (elem[new_row][new_col]!=0) { new_row=row+1; new_col=col; } row = new_row; col = new_col; } } break; case (2): { size_t num = 0; size_t quarter = dim / 2; magic_square temp (quarter); for (size_t loop=0; loop!=4; ++loop) { num = loop * quarter * quarter; switch (loop) { case (0): for (size_t row=0; row!=quarter; ++row) for (size_t col=0; col!=quarter; ++col) { elem[row][col] = temp.elem[row][col] + num; } break; case (1): for (size_t row=quarter; row!=dim; ++row) for (size_t col=quarter; col!=dim; ++col) { size_t x = row-quarter; size_t y = col-quarter; elem[row][col] = temp.elem[x][y] + num; } break; case (2): for (size_t row=0; row!=quarter; ++row) for (size_t col=quarter; col!=dim; ++col) { size_t x = row; size_t y = col-quarter; elem[row][col] = temp.elem[x][y] + num; } break; case (3): for (size_t row=quarter; row!=dim; ++row) for (size_t col=0; col!=quarter; ++col) { size_t x = row-quarter; size_t y = col; elem[row][col] = temp.elem[x][y] + num; } break; } } size_t div = (quarter-1) / 2; for (size_t row=0; row!=quarter; ++row) { size_t min_col=row==div?1:0; size_t max_col=row==div?div+1:div; for (size_t col=min_col; col!=max_col; ++col) std::swap (elem[row][col], elem[row+quarter][col]); } if (div=div-1) { for (size_t row=0; row!=quarter; ++row) { for (size_t col=dim-div; col!=dim; ++col) { std::swap (elem[row][col], elem[row+quarter][col]); } } } } break; } #ifndef NDEBUG assert_valid(); #endif NDEBUG } #ifndef NDEBUG bool magic_square::assert_valid() { size_t old_sum = 0; size_t sum = 0; for (size_t row=0; row!=dim; ++row) { sum = 0; for (size_t col=0; col!=dim; ++col) sum += elem[row][col]; if (!old_sum) old_sum = sum; assert (old_sum sum); return true; } #endif NDEBUG std::ostream& operator<< (std::ostream& os, const magic_square& s) { for (size_t x=0; x!=s.dim; ++x) { for (size_t y=0; y!=s.dim; ++y) { os << s.elem[x][y] << '\t'; } os << '\n'; } return os; } int main() { std::cout << "Magic square of order 3\n" << magic_square (3) << std::endl; std::cout << "Magic square of order 4\n" << magic_square (4) << std::endl; std::cout << "Magic square of order 6\n" << magic_square (6) << std::endl; }
#include<iostream> #include<vector> #include<time.h> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { srand ((unsigned)time(nullptr)); const size_t rows = 3; const size_t cols = 3; Matrix<rows, cols> a, b, c; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { a[row][col] = rand() % 10; b[row][col] = rand() % 10; } } std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; }
The code is below and i should also explain the algorithm. Well, What we are doing here is that we already defined the size to be 9x9 sudoku and are getting values using loops. All the empty spots are given UNASSIGNED value. Then we have functions to tell that if it is safe to put a value in the empty box by calculation and according to the rules of Sudoku it checks for is there any other some number horizontally and vertically and do the sum of the row and column is less than or equal to required or not. If the functions returns true then the program puts the value there.#include #define UNASSIGNED 0#define N 9bool FindUnassignedLocation(int grid[N][N], int &row, int &col);bool isSafe(int grid[N][N], int row, int col, int num);bool SolveSudoku(int grid[N][N]){int row, col;if (!FindUnassignedLocation(grid, row, col))return true; // success!for (int num = 1; num
{| ! scope="col" width="81" | idth ! scope="col" width="156" | Meters ! scope="col" width="142" | Zapal equivalent ! scope="row" | Top| 19.5213 ! scope="row" | Base| 55.337 ! scope="row" | Temple| 13.42 9 ! scope="row" | Stairs| 8.85 6 |}
#include<iostream> int main() { int matrix[3][3]; for (size_t row=0; row<3; ++row) { for (size_t col=0; col<3; ++col) { std::cout << "Enter a value for element [" << row << "][" << col << "]: "; std::cin >> matrix[row][col]; } } std::cout << std::endl; for (size_t row=0; row<3; ++row) { for (size_t col=0; col<3; ++col) { std::cout << matrix[row][col] << '\t'; } std::cout << std::endl; } std::cout << std::endl; }
#include<iostream> #include<vector> #include<random> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); Matrix<R,C>& operator-= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sub (lhs); return sub -= rhs; } template<const size_t R, const size_t C, const size_t R1, const size_t C1> Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix<R,C1> mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix<rows, cols> a, b; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { a[row][col] = distribution (generator); b[row][col] = distribution (generator); } } std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl; Matrix<cols, rows> c; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { c[col][row] = distribution (generator); } } std::cout << "Matrix c:\n\n" << c << '\n' << std::endl; std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl; }
#include<iostream> #include<vector> #include<random> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } void randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator); public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); Matrix<R,C>& operator-= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> void Matrix<R,C>::randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator) { for (size_t row=0; row!=R; ++row) { for (size_t col=0; col!=C; ++col) { m_data[row][col] = distribution (generator); } } } template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sub (lhs); return sub -= rhs; } template<const size_t R, const size_t C, const size_t R1, const size_t C1> Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix<R,C1> mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix<rows, cols> a, b; a.randomise (distribution, generator); b.randomise (distribution, generator); std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl; Matrix<cols, rows> c; c.randomise (distribution, generator); std::cout << "Matrix c:\n\n" << c << '\n' << std::endl; std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl; }