Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by hoangquan ( 5 years ago )
#pragma once
#include<iostream>
using namespace std;
class MATRIX
{
private:
int m, n;
int** arr;
public:
MATRIX(int row, int col);
void defineMatrix();
void upper();
void lower();
MATRIX operator*(const MATRIX& rhs);
MATRIX operator+(const MATRIX& rhs);
MATRIX operator-(const MATRIX& rhs);
MATRIX A_lower();
MATRIX B_upper();
MATRIX determinematrixA();
friend ostream& operator <<(ostream& os, const MATRIX& rhs);
};
MATRIX::MATRIX(int row = 0, int col = 0) {
m = row;
n = col;
arr = new int* [m];
for (int i = 0; i < m; i++) {
arr[i] = new int[n];
}
}
void MATRIX::defineMatrix()
{
cout << "Enter matrix elements" << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << "Row " << i + 1 << " " << "Column " << j + 1 << ": ";
cin >> arr[i][j];
}
}
}
void MATRIX::lower()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i < j)
{
cout << "0" << " ";
}
else
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void MATRIX::upper()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i > j)
{
cout << "0" << " ";
}
else
cout << arr[i][j] << " ";
}
cout << endl;
}
}
MATRIX MATRIX::operator*(const MATRIX& rhs) {
MATRIX temp(m, rhs.n);
if (n == rhs.m) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < rhs.n; j++) {
temp.arr[i][j] = 0;
for (int k = 0; k < n; k++) {
temp.arr[i][j] = temp.arr[i][j] + (arr[i][k] * rhs.arr[k][j]);
}
}
}
}
else {
cout << "Invalid Multiplication" << endl;
for (int i = 0; i < temp.m; i++) {
for (int j = 0; j < temp.n; j++) {
temp.arr[i][j] = NULL;
}
}
}
return temp;
}
MATRIX MATRIX::operator+(const MATRIX& rhs) {
MATRIX temp(m, n);
if (m == rhs.m && n == rhs.n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
temp.arr[i][j] = arr[i][j] + rhs.arr[i][j];
}
}
}
else {
cout << "Invalid Addition" << endl;
for (int i = 0; i < temp.m; i++) {
for (int j = 0; j < temp.n; j++) {
temp.arr[i][j] = NULL;
}
}
}
return temp;
}
MATRIX MATRIX::operator-(const MATRIX& rhs) {
MATRIX temp(m, n);
if (m == rhs.m && n == rhs.n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
temp.arr[i][j] = arr[i][j] - rhs.arr[i][j];
}
}
}
else {
cout << "Invalid Addition" << endl;
for (int i = 0; i < temp.m; i++) {
for (int j = 0; j < temp.n; j++) {
temp.arr[i][j] = NULL;
}
}
}
return temp;
}
MATRIX MATRIX:: A_lower()
{
MATRIX res(m, n);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i < j)
{
res.arr[i][j] = 0;
}
else
res.arr[i][j] = arr[i][j];
}
}
return res;
}
MATRIX MATRIX::B_upper()
{
MATRIX re(m, n);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i > j)
{
re.arr[i][j] = 0;
}
else
re.arr[i][j] = arr[i][j];
}
}
return re;
}
MATRIX MATRIX::determinematrixA()
{
int s, sum = 0;
if (m == 1) return arr[0][0];
if (m == 2) return (arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0]);
for (int k = 0; k < m; k++)
{
MATRIX smal(m, n);
for (int i = 0; i < m; i++)
{
for (int j = 1; j < m; j++)
{
if (i < k) smal.arr[i][j - 1] = arr[i][j];
else if (i > k) smal.arr[i - 1][j - 1] = arr[i][j];
}
}
if (k % 2 == 0) s = 1;
else s = -1;
sum += arr[k][0] * s * this->determinematrixA();
}
return sum;
}
ostream& operator <<(ostream& os, const MATRIX& rhs) {
for (int i = 0; i < rhs.m; i++)
{
for (int j = 0; j < rhs.n; j++)
{
os << rhs.arr[i][j] << " ";
}
os << endl;
}
return os;
}
#include"lab53.h"
int main()
{
cout << "\t\t\t ***************" << endl;
cout << "\t\t\t Question No. 01" << endl;
cout << "\t\t\t Class of Matrix" << endl;
cout << "\t\t\t ***************" << endl;
int row, col;
cout << "enter row:";
cin >> row;
cout << "enter col:";
cin >> col;
MATRIX a(row, col), b(row, col);
cout << "lower triangular matrix";
a.defineMatrix();
a.lower();
cout << "upper triangular matrix";
b.defineMatrix();
b.upper();
MATRIX X = a.A_lower();
MATRIX Y = b.B_upper();
cout << "sum" << endl << X + Y;
cout << "MULTIPLICATION" <<endl<< X * Y;
cout << "subtract" << endl << X - Y;
}
Revise this Paste