Write a program in c++ that take input in a integer matrix of size 4*4 and find out if the entered matrix is diagonal or not.
It's matrix C.
A Hadamard Matrix is a square matrix composed of 1 or -1. Using a square matrix system the hadamard matrix could be created
#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; }
C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion
int matrix[][]; // the matrix to find the max in int max = matrix[0][0]; int r,c; for(r = 0; r < 3; ++r) { for(c = 0; c < 3; ++c) { if(matrix[r][c] > max) { max = matrix[r][c]; } } } // max is now the maximum number in matrix
Write a program in c++ that take input in a integer matrix of size 4*4 and find out if the entered matrix is diagonal or not.
It's matrix C.
matrix
Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.
How we can addition Two Matrix plz send coding in C language mahesh dhanotiya astah_mahesh@rediff.com how i can built a square matrix in c,
Yes, do write. That's what you always have to do when you have got a homework-program.
Find directed graph that has the adjacency matrix Find directed graph that has the adjacency matrix
A Hadamard Matrix is a square matrix composed of 1 or -1. Using a square matrix system the hadamard matrix could be created
To find the original matrix of an inverted matrix, simply invert it again. Consider A^-1^-1 = A^1 = A
#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; }
#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; }