answersLogoWhite

0


Best Answer

Matrix addition is simply the process of summing one array to another array of the same size. The result is another array of the same size where each element is the sum of the corresponding elements in the first two arrays, like so:

{1, 2, 3} + {4, 5, 6} = {1+4, 2+5, 3+6} = {5, 7, 9}

This can be extended to multi-dimensional matrices (an array of arrays):

{1, 2, 3}

{4, 5, 6}

+

{7, 8, 9}

{10, 11, 12}

=

{7+1, 8+2, 9+3}

{10+4, 11+5, 12+6}

=

{8, 10, 12}

{14, 16, 18}

To achieve this in C++, C-style static arrays are the easiest method:

int main()

{

int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

int b[2][3] = {{7, 8, 9}, {10, 11, 12}};

int c[2][3] = {{0, 0, 0}, {0, 0, 0}};

for (size_t row=0; row<2; ++row)

for (size_t col=0; col<3; ++col)

c[row][col] = a[row][col] + b[row][col];

}

C-style dynamic arrays as well as std::vector and std::array can also be used to represent the matrices, but it is important that all dimensions are the same. Matrices of different sizes cannot be added together (the result is undefined).

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is Matrix addition in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the product of matrix A and C?

It's matrix C.


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


What is matrix programming in C programming?

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


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

#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;random&gt; template&lt;const size_t R, const size_t C&gt; class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix&amp; source); Matrix (Matrix&amp;&amp; source); Matrix&amp; operator= (const Matrix&lt;R,C&gt;&amp; source); Matrix&amp; operator= (Matrix&lt;R,C&gt;&amp;&amp; source); ~Matrix () {} public: // accessors row_type&amp; row (const size_t index) { return m_data[index]; } const row_type&amp; row (const size_t index) const { return m_data[index]; } row_type&amp; operator[] (const size_t index) { return m_data[index]; } const row_type&amp; 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&lt;R,C&gt;&amp; operator+= (const Matrix&lt;R,C&gt;&amp;); Matrix&lt;R,C&gt;&amp; operator-= (const Matrix&lt;R,C&gt;&amp;); }; template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix() { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = 0; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix(const Matrix&lt;R,C&gt;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = source.m_data[row][col]; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix(Matrix&lt;R,C&gt;&amp;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator= (const Matrix&lt;R,C&gt;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator= (Matrix&lt;R,C&gt;&amp;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator+= (const Matrix&lt;R,C&gt;&amp; rhs) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator-= (const Matrix&lt;R,C&gt;&amp; rhs) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt; operator+ (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R,C&gt;&amp; rhs) { Matrix&lt;R,C&gt; sum (lhs); return sum += rhs; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt; operator- (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R,C&gt;&amp; rhs) { Matrix&lt;R,C&gt; sub (lhs); return sub -= rhs; } template&lt;const size_t R, const size_t C, const size_t R1, const size_t C1&gt; Matrix&lt;R,C1&gt; operator* (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R1,C1&gt;&amp; rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix&lt;R,C1&gt; 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&lt;const size_t R, const size_t C&gt; std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const Matrix&lt;R,C&gt;&amp; m) { for (size_t row=0; row&lt;R; ++row) { for (size_t col=0; col&lt;C; ++col) { std::cout &lt;&lt; m[row][col] &lt;&lt; '\t'; } std::cout &lt;&lt; std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution&lt;int&gt; distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix&lt;rows, cols&gt; a, b; for (size_t row=0; row&lt;rows; ++row) { for (size_t col=0; col&lt;cols; ++col) { a[row][col] = distribution (generator); b[row][col] = distribution (generator); } } std::cout &lt;&lt; "Matrix a:\n\n" &lt;&lt; a &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix b:\n\n" &lt;&lt; b &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a + b:\n\n" &lt;&lt; a + b &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a - b:\n\n" &lt;&lt; a - b &lt;&lt; '\n' &lt;&lt; std::endl; Matrix&lt;cols, rows&gt; c; for (size_t row=0; row&lt;rows; ++row) { for (size_t col=0; col&lt;cols; ++col) { c[col][row] = distribution (generator); } } std::cout &lt;&lt; "Matrix c:\n\n" &lt;&lt; c &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a * c:\n\n" &lt;&lt; a * c &lt;&lt; '\n' &lt;&lt; std::endl; }


How do you implement Hadamard matrix in c?

A Hadamard Matrix is a square matrix composed of 1 or -1. Using a square matrix system the hadamard matrix could be created

Related questions

If properties of matrix addition let A B C D the m n matrix then 1 A plus B B plus A?

Yes. Matrix addition is commutative.


C Matrix addition program?

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,


If properties of matrix addition let A B C D the m n matrix then 1A plus B B plus A?

We have to proof that A+B=B+A we know from defn of matrix addition that (ij )th element of B+A is bij+aij Since aij &amp;bij are real no's aij +bij =bij+aij (1


What would be the result if you add matrices B plus A instead A plus B?

It would be no different. Matrix addition is Abelian or commutative. Matrix mutiplication is not.


Write a c program for matrix addition using function?

#include&lt;


How can you download c c plus plus language in Hindi addition?

You cannot download programming languages!


Algorithm of the addition of the two matrix?

Matrix-Addition(a,b)1 for i =1 to rows [a]2 for j =1 to columns[a]3 Input a[i,j];4 Input b[i,j];5 C[i, j] = A[i, j] + B[i, j];6 Display C[i,j];


Are matrix addition and matrix multiplication commutative?

Matrix addition is commutative if the elements in the matrices are themselves commutative.Matrix multiplication is not commutative.


How do you Write a C program for matrix addition?

for (i=0; i&lt;IMAX; i++) for (j=0; j&lt;JMAX; j++) c[i,j] = a[i,j] + b[i,j];


What is the product of matrix A and C?

It's matrix C.


C plus plus program -matrix multiplication using class?

for(int i=0;i


Why do you include count plus plus in addition of matrix?

C++ doesn't contains all operations you may need on your program so in some cases you need to include a specific class that contains the function you need the is here you need to include count class cuse C++ can't add matrices so you have to use the include ;)