answersLogoWhite

0


Best Answer

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

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program that calculate the matrix of three by three addition?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program for matrix addition using function?

#include&lt;


Write a program to multiply 33 matrix.?

write a program to multily 3*3 matrix.


Write a program using iostreams to take as input two multi-dimensional arrays and print their sum as output Matrix Addition in Matrix format?

http://www.assignmentsclub.com/


2x2 matrix multiplication program in 8085 processor?

how to write a program for matrix multiplication in microprocesspr


How do you Write A program in c language for checking a diagonal 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.


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];


A c program to square matrix?

A C program to square matrix is a math problem. In the math problem you write down all the outer boundary values of matrix in a circle, then write down the inner value.


Write a c program to find eigenvalue of a matrix?

Yes, do write. That's what you always have to do when you have got a homework-program.


How do you write a java program to find the transpose of the matrix for the given elements?

You basically write a nested for loop (one for within another one), to copy the elements of the matrix to a new matrix.


How do you write a java program to find the ad joint of a matrix?

bgfygfrhjyuyhh


Write a program to print the sum of a sparse matrix?

This is a directive, not a question.


Write a C program to find sum of 3 matrices?

matrix