Question

In: Computer Science

How to add and multiply two matrices in the following code? #include "My_matrix.h" #include <stdexcept> My_matrix::My_matrix()...

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

}

Solutions

Expert Solution

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

//////

}

}


Related Solutions

In JAVA How can you multiply two matrices that are different lengths. the answer should be...
In JAVA How can you multiply two matrices that are different lengths. the answer should be in 3d array so [][][] how to multiply a 3d array with a 2d array and the result is in 3d array for example: double[][][] nums1 = { { {0.0, 0.1, 2.0}, {3.0,4.0,5.0} }, { {6.0,7.0,8.0}, {9.0,10.0,11.0} } } int [][] nums2 = { {1,2,3}, {4,5,6}, {7,8,9}} and the results should be in [][][] <-- in this dimension int[][][] answer = { {  {18, 21,24},...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
How to add, multiply, and divide numbers in flowgorithm
How to add, multiply, and divide numbers in flowgorithm
Write a C++ program to multiply two matrices a and b and print the result. Use...
Write a C++ program to multiply two matrices a and b and print the result. Use two-dimensional arrays to represent the matrices.
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple times Get input from key-board and display the result in the console window
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
Write a program to multiply two polynomials. Code needed in Java.
Write a program to multiply two polynomials. Code needed in Java.
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles...
Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles grows with increasing matrix sizes. 10 points pseudocode 10 points description of code computing cycle growth
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT