C Program You may be interested in this problem if you have
C++ Program
You may be interested in this problem if you have taken or are taking MATH 2090. Create a class Matrix with floating-point fields int **A, int n, and int m. Create the constructor: Matrix::Matrix(int **A, int n, int m) as well as the methods Matrix Matrix::add(Matrix B) Matrix Matrix::mul(Matrix B) which can perform addition and multiplication of matrices. Call the header matrix.h and the source matrix.cpp, and the demonstration file matrix-ops.cpp. Transposition, reflection. Additional points if you define functions: Matrix Matrix::transpose(Matrix B) Matrix Matrix::reflect(Matrix B) which obtain the transpose and reflection of a matrix. Make it generic and overload operators. As with the vector problem above, additional points if you make the class generic. Also, instead of defining add, overload +; and instead of mul, overload *. Demonstrate these in matrix-ops .cpp. If you did reflect, overload -, and for transpose, overload !.Solution
Code:
Matrix.h
#ifndef MATRIX
#define MATRIX
class Matrix
{
int **A;
int n;
int m;
public:
Matrix(int n, int m);
Matrix add(Matrix B);
Matrix Matrix :: mul(Matrix B);
void input();
void display();
};
#endif
Matrix.cpp
#include<iostream>
using namespace std;
#include\"Matrix.h\"
Matrix:: Matrix(int nn, int mn)
{
m = mn;
n = nn;
A = new int*[m];
for (int i = 0; i < m; i++)
A[i] = new int[n];
}
Matrix Matrix :: add(Matrix B)
{
Matrix C(n,m);
for (int i = 0; i < n; ++i)
{
for(int j=0;j<m;j++)
C.A[i][j]=A[i][j]+B.A[i][j];
}
return C;
}
Matrix Matrix :: mul(Matrix B)
{
Matrix T(n,m);
T.n=n;
T.m=B.m;
for(int i=0; i<T.n; i++)
for(int j=0; j<T.m; j++)
{
T.A[i][j]=0;
{
for (int k=0; k<T.m; k++)
{
T.A[i][j] += A[i][k]*B.A[k][j];
}
}
}
return T;
}
void Matrix :: input()
{
cout<<\"Enter matrix elements:\ \";
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> A[i][j];
}
}
}
void Matrix :: display()
{
cout <<\"The matrix:\ \";
for(int i = 0; i < m; i++)
{
cout <<endl;
for(int j = 0; j < n; j++)
{
cout << A[i][j] <<\" \";
}
}
}
main.cpp
#include<iostream>
using namespace std;
#include\"Matrix.h\"
int main()
{
Matrix D(3,3),E(3,3);
D.input();
E.input();
D.display();
E.display();
D=D.add(E);
D.display();
D=D.mul(E);
D.display();
system(\"pause\");
}
Output:
Enter matrix elements:
1
1
1
1
1
1
1
1
1
Enter matrix elements:
1
1
1
1
1
1
1
1
1
The matrix:
1 1 1
1 1 1
1 1 1 The matrix:
1 1 1
1 1 1
1 1 1 The matrix:
2 2 2
2 2 2
2 2 2 The matrix:
6 6 6
6 6 6
6 6 6 Press any key to continue . . .


