In: Computer Science
How to add and multiply two matrices in the following code?
#include "My_matrix.h"
#include <stdexcept>
My_matrix::My_matrix()
{
// add your code here
n = 0;
m = 0;
ptr = nullptr;
}
void My_matrix::allocate_memory()
{
// add your code here
ptr = new int* [n];
for (int i = 0; i < n; ++i) {
ptr[n] = new int[m];
}
}
My_matrix::My_matrix(int n1, int m1)
{
// add your code here
n = n1;
m = m1;
ptr = new int*[n];
for(int i = 0; i < n; i++) {
ptr[i] = new int[m];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ptr[i][j] = 0;
}
}
}
My_matrix::My_matrix(const My_matrix& mat)
{
// add your code here
this->n = mat.n;
this->m = mat.m;
ptr = new int*[n];
for(int i = 0; i < n; i++) {
ptr[i] = new int[m];
}
for (int i = 0; i < mat.n; i++) {
for (int j = 0; j < mat.m; j++) {
this->ptr[n][m] = mat.ptr[n][m];
}
}
}
My_matrix::~My_matrix()
{
// add your code here
for (int i = 0; i < n; ++i) {
delete [] ptr[i];
}
delete [] ptr;
}
My_matrix& My_matrix::operator=(const My_matrix& mat)
{
// add your code here
this->n = mat.n;
this->m = mat.m;
ptr = new int*[n];
for(int i = 0; i < n; i++) {
ptr[i] = new int[m];
}
for (int i = 0; i < mat.n; i++) {
for (int j = 0; j < mat.m; j++) {
this->ptr[n][m] = mat.ptr[n][m];
}
}
}
int My_matrix::number_of_rows() const
{
// add your code here
return n;
}
int My_matrix::number_of_columns() const
{
// add your code here
return m;
}
int* My_matrix::operator()(int i) const
{
// add your code here
return ptr[i];
}
int My_matrix::operator()(int i, int j) const
{
// add your code here
return ptr[n][m];
}
int& My_matrix::operator()(int i, int j)
{
// add your code here
return ptr[n][m];
}
int My_matrix::elem(int i, int j) const
{
if (i < 0 || i >= n) throw out_of_range("Out of range");
if (j < 0 || j >= m) throw out_of_range("Out of range");
// add your code here
return ptr[n][m];
}
int& My_matrix::elem(int i, int j)
{
// add your code here
if (i < 0 || i >= n) throw out_of_range("Out of range");
if (j < 0 || j >= m) throw out_of_range("Out of range");
return ptr[n][m];
}
ostream& operator<<(ostream& out, const My_matrix& mat)
{
// add your code here
for (int i = 0; i < mat.number_of_rows(); i++) {
for (int j = 0; j < mat.number_of_columns(); j++) {
out << mat(i, j) << " ";
}
out << endl;
}
return out;
}
istream& operator>>(istream& in, My_matrix& mat)
{
// add your code here
for(int i = 0; i < mat.number_of_rows(); i++ ){
for(int j = 0; j < mat.number_of_columns(); j++){
in >> mat(i, j);
}
}
return in;
}
My_matrix operator+(const My_matrix& mat1, const My_matrix& mat2)
{ // add your code here
}
My_matrix operator*(const My_matrix& mat1, const My_matrix& mat2)
{
// add your code here
}
// Add two matrices
// for adding 2 matrices we should have same number of columns and rows in both matrices
My_matrix operator+(const My_matrix& mat1, const My_matrix& mat2)
{ // add your code here
int r1 = mat1.number_of_rows();
int c1 = mat1.number_of_columns();
int r2 = mat2.number_of_rows();
int c2 = mat2.number_of_columns();
if (r1 !=r2 || c1!=c2){
out<<"can not add these matrices";
} else {
int sum[r1][c1], i, j;
// Add matrices
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
out << endl << "resultant matrix is: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j) {
cout << sum[i][j] << " ";
if(j == c - 1)
out << endl;
}
}
}
/// lMultiply 2 matrices
// we can multiply 2 matrices only if column of 1st matrix is same as rows of second matrix. c1=r2;
My_matrix operator*(const My_matrix& mat1, const My_matrix& mat2)
{
// add your code here
if (c1 !=r2){
out<<"can not multiply these matrices";
} else {
int r1 = mat1.number_of_rows();
int c1 = mat1.number_of_columns();
int r2 = mat2.number_of_rows();
int c2 = mat2.number_of_columns();
int row = r1;
int col = c2;
mul[r1][c2];
///////
// Initializing mat mul 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mul[i][j]=0;
}
// store the multiplication result.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mul[i][j] += mat1[i][k] * mat2[k][j];
}
// Displaying the multiplication of two matrix.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
out << " " << mul[i][j];
if(j == c2-1)
cout << endl;
}
//////
}
}