answersLogoWhite

0

What else can I help you with?

Continue Learning about Engineering

C program to perform addition of matrices using functions?

#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(); }


Perform addition multiplication subtraction of 2-D array using Operator Overloading in C plus plus?

#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; }


Write a c plus plus program to add two matrix using arrays?

Matrix Add/* Program MAT_ADD.C**** Illustrates how to add two 3X3 matrices.**** Peter H. Anderson, Feb 21, '97*/#include &ltstdio.h>void add_matrices(int a[][3], int b[][3], int result[][3]);void print_matrix(int a[][3]);void main(void){int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };int r[3][3];add_matrices(p, q, r);printf("\nMatrix 1:\n");print_matrix(p);printf("\nMatrix 2:\n");print_matrix(q);printf("\nResult:\n");print_matrix(r);}void add_matrices(int a[][3], int b[][3], int result[][3]){int i, j;for(i=0; i&lt3; i++){for(j=0; j&lt3; j++){result[i][j] = a[i][j] + b[i][j];}}}void print_matrix(int a[][3]){int i, j;for (i=0; i&lt3; i++){for (j=0; j&lt3; j++){printf("%d\t", a[i][j]);}printf("\n");}}


How to write a c plus plus program to multiply matrices using function template?

#include<iostream> #include<algorithm> #include<cassert> unsigned multiply_recursive (unsigned a, unsigned b) { if (!a !b) return 0; // Note: not(a) or not (b) if (a==1) return b; if (b==1) return a; return a + multiply_recursive (a, --b); } unsigned multiply_iterative (unsigned a, unsigned b) { int product = 0; int x = std::min (a, b); int y = std::max (b, a); while (x--) product += y; return product; } int main() { unsigned x, y; // Test all combinations with 0. x = multiply_recursive (0, 0); assert (x==0); x = multiply_iterative (0, 0); assert (x==0); x = multiply_recursive (0, 1); assert (x==0); x = multiply_iterative (0, 1); assert (x==0); x = multiply_recursive (1, 0); assert (x==0); x = multiply_iterative (1, 0); assert (x==0); // Test non-zero values with 1. x = multiply_recursive (1, 42); // inefficient: lowest value must be on right (fewer recursions). y = multiply_iterative (1, 42); assert (x==42 && y==42); x = multiply_recursive (42, 1); y = multiply_iterative (42, 1); assert (x==42 && y==42); // Test other non-zero values are commutative. x = multiply_recursive (24, 42); // inefficient: lowest value must be on right (fewer recursions). y = multiply_iterative (24, 42); assert (x==1008 && y==1008); x = multiply_recursive (42, 24); y = multiply_iterative (42, 24); assert (x==1008 && y==1008); }


Write a program using c plus plus to check whether the given square matrix is symmetric or not?

means whether the matrix is same or not program for symmetric matrix : include<stdio.h> #include<conio.h> main() { int a[10][10],at[10][10],k,i,j,m,n; clrscr(); printf("enter the order of matrix"); scanf("%d %d",&m,&n); printf("enter the matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } for(i=0;i<m;i++) { for(j=0;j<n;j++) at[i][j]=a[j][i]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(at[i][j]!=a[i][j]) k=1; } } if(k==1) printf("not symmetric"); else printf("symmetric"); getch(); }

Related Questions

What is a reduced matrix?

Reduced matrix is a matrix where the elements of the matrix is reduced by eliminating the elements in the row which its aim is to make an identity matrix.


What are the elementry matrices?

An elementary matrix is a matrix obtained from the identity matrix following one of the following row operations:Swap 2 rows;Multiply any row by a non-zero constant;Replace a row by the sum of itself and a non-zero multiple of another row.


How can I use MATLAB to view a specific value in a sparse matrix?

To view a specific value in a sparse matrix using MATLAB, you can use the command full(matrix(row, column)) where matrix is your sparse matrix and row and column are the indices of the value you want to view. This command converts the sparse matrix to a full matrix and allows you to access the specific value at the given row and column.


How to construct a matrix with 23 elements?

It will either be a 1*23 row matrix or a 23*1 column matrix.


C program to perform addition of matrices using functions?

#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(); }


Write a c plus plus program that calculate the matrix of three by three addition?

#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; }


Show that for a square matrix the linear dependence of the row vectors implies that of the column vectors and conversely.?

show that SQUARE MATRIX THE LINEAR DEPENDENCE OF THE ROW VECTOR?


What is a 1x2 matrix?

It is a matrix with 1 row and two columns: something like (x, y).


What is element a13 in this matrix?

To determine element a13 in a matrix, you need to identify its position based on the matrix's row and column indexing. In a typical matrix notation, a13 refers to the element located in the 1st row and 3rd column. If you provide the specific matrix, I can help you find the value of a13.


Does doing row operations on a matrix change its determinant?

No.


Does doing row operations of a matrix change its determinant?

No.


Perform addition multiplication subtraction of 2-D array using Operator Overloading in C plus plus?

#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; }