Design a class to perform various matrix operations A matrix

Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of 5 rows and 6 columns, we say that matrix A is of the size 5 X 6 and sometimes denote it as A5X6. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two matrices can be added and subtracted if they have the same size. Suppose that A = [aij] and B =[bij] are two matrices of the size m n, in which aij denotes the element of A in the ith row and the jth column, and so on. The sum and difference of A and B is given by: A + B = [aij + bij] A - B = [aij -bij]

The multiplication of A and B (A * B) is defined only if the number of columns of A are the same as the number of rows of B. If A is of the size m n and B is of the size n t, then A * B = [cik] is of the size m x t and the element cik is given by the formula: cik = (a)i1( b)1k +( a)i2(b)2k + . . . + (a)in(b)nk

Design and implement a class matrixType that can store a matrix of any size. Overload the operators +, -, and * to perform the addition, subtraction, and multiplication operations, respectively, and overload the operator << to output a matrix. Also, write a test program to test various operations on matrices.

Remember, classes with pointer member variables must:
1. Explicitly overload the assignment operator
2.Include the copy constructor
3.Include the destructor

Note: Please do not post the code here. I would really apreciate it if you could email me the code at: jemo_nemo4@yahoo.com

Thank You.

Solution

#include <iostream>
using namespace std;

class matrixOperations
{
int **ptr, mm, nn;
public:
matrixOperations(int row, int col)
{
mm = row;
nn = col;
ptr = new int*[mm];
for (int i = 0; i < mm; ++i)
{
   ptr[i] = new int[nn];
   for (int j = 0; j < nn; ++j)
   ptr[i][j] = 0;
}
}

void accept()
{
cout<<\"Enter matrix data: \";
for(int i = 0; i < mm; i++)
{
   for(int j = 0; j < nn; j++)
   {
       cin >> ptr[i][j];
   }
}
}

void display()
{
cout <<\"Current matrix is:\ \";
for(int i = 0; i < mm; i++)
{
       for(int j = 0; j < nn; j++)
       {
           cout << ptr[i][j] <<\" \";
       }
       cout <<endl;
}
}

matrixOperations& operator+ (matrixOperations& temp, const matrixOperations& m1)
{
return (*this += m1);
}

matrixOperations& operator* (const matrixOperations& m1)
{
return (*this *= m1);
}

matrixOperations& operator+= (const matrixOperations& temp)
{
for(int i = 0; i < mm; i++)
{
   for(int j = 0; j < nn; j++)
   {
   ptr[i][j] += temp.ptr[i][j];
   }
}
return *this;
}

matrixOperations& operator*= (const matrixOperations& mat)
{
if(nn == mat.mm)
{
   for(int i = 0; i < mat.mm; ++i)
   {
   for(int k = 0; k < nn; ++k)
   {
       ptr[i][k] *= mat.ptr[k][i];
   }
   }
}
  
return *this;
}

matrixOperations& operator= (const matrixOperations& mat)
{
ptr = mat.ptr;
nn = mat.nn;
mm = mat.mm;
  
return *this;
}

};


int main()
{
matrixOperations first(4,4), second(4,4);
first.accept();
cout << \"\ First matrix: \ \";
first.display();

second.accept();
cout << \"\ Second Matrix: \ \";
second.display();

cout << \"\ Res = second + first\ \";
second = second + first;
second.display();

cout << \"\ Result = (first * second)\ \";
second = (first * second);
second.display();

cout << \"\ Result += second\ \";
first += second;
first.display();
}

Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row p
Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row p
Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site