answersLogoWhite

0


Best Answer

#include<iostream>

#include<array>

template<typename T, const size_t c>

class Matrix1D

{

public:

using data_type = std::array<T, c>;

using iterator = typename data_type::iterator;

using const_iterator = typename data_type::const_iterator;

private:

data_type m_data;

public:

~Matrix1D () = default;

Matrix1D () = default;

Matrix1D (const Matrix1D&) = default;

Matrix1D& operator= (const Matrix1D&) = default;

Matrix1D (Matrix1D&& source): m_data (std::move (source.m_data)) {}

Matrix1D& operator= (Matrix1D&& source) { m_data = std::move (source.m_data); return *this; }

Matrix1D& operator+= (const Matrix1D&);

iterator begin() { return m_data.begin(); }

iterator end() { return m_data.end(); }

const_iterator begin() const { return m_data.begin(); }

const_iterator end() const { return m_data.end(); }

};

template<typename T, const size_t c>

Matrix1D<T, c>& Matrix1D<T, c>::operator+= (const Matrix1D<T, c>& rhs)

{

iterator ita;

const_iterator itb;

for (ita=begin(), itb=rhs.begin(); ita!=end(); ++ita, ++itb)

*ita += *itb;

return *this;

}

template<typename T, const size_t c>

std::ostream& operator<< (std::ostream& os, const Matrix1D<T, c>& m)

{

for (Matrix1D<T, c>::const_iterator it=m.begin(); it!=m.end(); ++it)

os << *it << '\t';

return os;

}

template<typename T, const size_t r, const size_t c>

class Matrix2D

{

public:

using data_type = std::array<Matrix1D<T, c>, r>;

using iterator = typename data_type::iterator;

using const_iterator = typename data_type::const_iterator;

private:

data_type m_data;

public:

~Matrix2D () = default;

Matrix2D () = default;

Matrix2D (const Matrix2D&) = default;

Matrix2D& operator= (const Matrix2D&) = default;

Matrix2D (Matrix2D&& source): m_data (std::move (source.m_data)) {}

Matrix2D& operator= (Matrix2D&& source) { m_data = std::move (source.m_data); return *this; }

Matrix2D& operator+= (const Matrix2D&);

Matrix2D operator+ (const Matrix2D&);

iterator begin() { return m_data.begin(); }

iterator end() { return m_data.end(); }

const_iterator begin() const { return m_data.begin(); }

const_iterator end() const { return m_data.end(); }

};

template<typename T, const size_t r, const size_t c>

Matrix2D<T, r, c>& Matrix2D<T, r, c>::operator+= (

const Matrix2D<T, r, c>& rhs)

{

iterator ita;

const_iterator itb;

for (ita=begin(), itb=rhs.begin(); ita!=end(); ++ita, ++itb)

*ita += *itb;

return *this;

}

template<typename T, const size_t r, const size_t c>

Matrix2D<T, r, c> Matrix2D<T, r, c>::operator+ (const Matrix2D<T, r, c>& rhs)

{

Matrix2D<T, r, c> result (*this);

result += rhs;

return result;

}

template<typename T, const size_t r, const size_t c>

std::ostream& operator<< (std::ostream& os, const Matrix2D<T, r, c>& m)

{

for (Matrix2D<T, r, c>::const_iterator it=m.begin(); it!=m.end(); ++it)

os << *it << '\n';

return os;

}

int main()

{

const size_t rows = 2, cols = 3;

Matrix2D<int, rows, cols> A, B;

Matrix2D<int, rows, cols>::iterator it;

Matrix1D<int, cols>::iterator in;

int value = 1;

for (it=A.begin(); it!=A.end(); ++it)

{

for (in=(*it).begin(); in!=(*it).end(); ++in)

(*in) = value++;

}

std::cout << "Matrix A:\n\n" << A << std::endl;

for (it=B.begin(); it!=B.end(); ++it)

{

for (in=(*it).begin(); in!=(*it).end(); ++in)

(*in) = ++value;

}

std::cout << "Matrix B:\n\n" << B << std::endl;

std::cout << "Matrix A + B:\n\n" << A + B << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can write a C plus plus programming on matrix addition only?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can a matrix with dimensions of 2 X 4 be added to another matrix with dimensions of 2 X 5?

No. Matrix addition (or subtraction) is defined only for matrices of the same dimensions.


Can a matrix with dimensions of 4 X 5 be added to another matrix with dimensions of 5 X 3?

No. Matrix addition (or subtraction) is defined only for matrices of the same dimensions.


In programming is there shorthand for Integer Divided by itself such as SlashIntegerEquals or EqualsSlashInteger?

No. You have such options only for addition and subtraction.


Is it more efficient to store the data in a matrix then write it to the file at the end of the program or to write each element to the file as the program runs?

It shouldn't make significant difference, only if the matrix is huge -- in that case you shouldn't store it in memory.


Write an expression that equals 468 that includes only addition?

467 + 1


If a matrix has only one element is it a square matrix?

Yes.


What is the difference between zero matrix and null matrix?

there is none you weasel. the only good matrix is revolutions. :)


Can a nonsquare matrix be a triangular matrix?

No. Only square matrices can be triangular.


Write an addition expression using the number 7 only it should be the same as 11x7?

70 + 7


Is the scalar matrix is always a identity matrix?

No. A scalar matrix is a diagonal matrix whose main diagonal elements are the same. Only if the diagonal elements are all 1 is it an identity matrix.


What is the determinant rank of the determinant of 123456 its a 2 x 3 matrix?

A determinant is defined only for square matrices, so a 2x3 matrix does not have a determinant.Determinants are defined only for square matrices, so a 2x3 matrix does not have a determinant.


Does every square matrix have an inverse?

No. A square matrix has an inverse if and only if its determinant is nonzero.