Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// LAB52.cpp : Defines the entry point for the console application. // #include stdafx.h #include #include #include math.h using namespace std; typedef double mattype; #define

// LAB52.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include

#include

#include "math.h"

using namespace std;

typedef double mattype;

#define myfabs(x) fabs(x)

class Matrix

{

private: // all data is private

mattype **m;

int NR, NC;

public:

Matrix() { NR = 0; NC = 0; m = 0; } //Constructor

Matrix(int rows, int cols) { allocate(rows, cols); } //Constructor

Matrix(const Matrix &a); // Constructor

~Matrix() { unallocate(); } //Destructor

void allocate(int rows, int cols);

void unallocate();

int getRows() { return NR; }

int getCols() { return NC; }

void zero();

void set(mattype val, int rows, int cols) { m[rows][cols] = val; }

mattype get(int rows, int cols) { return m[rows][cols]; }

int gaussjordan();

int invert();

void addcolumns(int cols); // Add columns for I-mat

void removecolumns(int cols); // Remove columns of I-mat

Matrix &operator=(const Matrix &a);

Matrix operator+(const Matrix &a);

Matrix operator-(const Matrix &a);

Matrix operator*(const Matrix &a);

Matrix operator*(const mattype &a); // Scalar multiply by "a"

Matrix operator/(const mattype &a); // Scalar divide by "a"

Matrix operator!(void); // Unary operation to "invert"

Matrix operator~(void); // Unary operation to "sove by gaussjordan"

};

void Matrix::allocate(int rows, int cols) {

if (rows>0 && cols>0) {

NR = rows;

NC = cols;

m = new mattype *[NR];

for (int i = 0; i

}

else {

NR = 0; NC = 0; m = 0;

}

}

void Matrix::unallocate() {

if (m) {

for (int i = 0; i

delete[] m;

NR = 0; NC = 0; m = 0;

}

}

Matrix::Matrix(const Matrix &a) {

allocate(a.NR, a.NC);

for (int i = 0; i

for (int j = 0; j

m[i][j] = a.m[i][j];

}

void Matrix::zero() {

if (!m)

{

cout << "Matrix not sized yet." << endl;

}

else

{

for (int i = 0; i

for (int j = 0; j

m[i][j] = 0;

}

}

int Matrix::gaussjordan() {

int i, j, k;

mattype *t;

int BIGrow;

for (j = 0; j

{

// Find Maximum Pivot

BIGrow = j;

for (i = j + 1; i

{

if (myfabs(m[i][j])>myfabs(m[BIGrow][j]))

{

BIGrow = i;

}

}

if (myfabs(m[BIGrow][j])<1e-7) return 1;

t = m[j];

m[j] = m[BIGrow];

m[BIGrow] = t;

// Normalization

for (i = j + 1; i

m[j][j] = 1.0;

// Elimination

for (k = 0; k

{

if (k == j) continue; // Restart Loop

for (i = j + 1; i

m[k][j] = 0.0;

}

}

return 0;

}

void Matrix::addcolumns(int cols) {

mattype *t; int i, j;

if (!m) {

cout << "Matrix is not set." << endl;

return;

}

for (i = 0; i

{

t = new mattype[NC + cols];

for (j = 0; j

for (; j

delete[] m[i];

m[i] = t;

}

NC += cols;

}

void Matrix::removecolumns(int cols) {

mattype *t; int i, j;

if (!m) {

cout << "Matrix is not set." << endl;

return;

}

NC -= cols;

for (i = 0; i

{

t = new mattype[NC];

for (j = 0; j

delete[] m[i];

m[i] = t;

}

}

int Matrix::invert() {

int i, j;

if (!m) {

cout << "Matrix is not set." << endl;

return 1;

}

if (NC != NR) {

cout << "Matrix must be square." << endl;

return 1;

}

addcolumns(j = NC);

for (i = 0; i

i = gaussjordan();

removecolumns(j);

return i;

}

ostream& operator<<(ostream &os, Matrix a) {

for (int i = 0; i

for (int j = 0; j

os << endl;

}

return os;

}

Matrix &Matrix::operator=(const Matrix &rhs) {

if (this != &rhs)

{

if (NR != rhs.NR || NC != rhs.NC)

{

unallocate();

allocate(rhs.NR, rhs.NC);

}

for (int i = 0; i < NR; i++)

for (int j = 0; j < NC; j++)

m[i][j] = rhs.m[i][j];

}

return *this;

}

Matrix Matrix::operator+(const Matrix &rhs) {

Matrix t;

if (NR != rhs.NR || NC != rhs.NC)

{

cout << "Matrices not the same size." << endl;

}

else

{

t.allocate(NR, NC);

for (int i = 0; i

for (int j = 0; j

t.m[i][j] = m[i][j] + rhs.m[i][j];

}

return t;

}

Matrix Matrix::operator-(const Matrix &rhs) {

Matrix t;

if (NR != rhs.NR || NC != rhs.NC)

{

cout << "Matrices not the same size." << endl;

}

else

{

t.allocate(NR, NC);

for (int i = 0; i

for (int j = 0; j

t.m[i][j] = m[i][j] - rhs.m[i][j];

}

return t;

}

Matrix Matrix::operator*(const Matrix &rhs) {

Matrix t;

if (NC != rhs.NR)

{

cout << "Matrices not the right size." << endl;

}

else

{

t.allocate(NR, rhs.NC);

t.zero();

//Add Code Here for Matrix multiplication

}

return t;

}

Matrix Matrix::operator*(const mattype &a) {

Matrix t(NR, NC);

// Add your code here

return t;

}

Matrix Matrix::operator/(const mattype &a) {

Matrix t(NR, NC);

if (mattype(a) == mattype(0))

{

cout << "Cannot divide by zero scalar." << endl;

t.zero();

}

else

{

// Add your code here

}

return t;

}

Matrix Matrix::operator!(void) {

Matrix t(*this);

// Add your code here

return t;

}

Matrix Matrix::operator~(void) {

Matrix t(NC, NR);

// Add your code here

return t;

}

int main(void)

{

Matrix A(2, 3);

A.set(1, 0, 0); A.set(2, 0, 1); A.set(3, 0, 2);

A.set(4, 1, 0); A.set(5, 1, 1); A.set(6, 1, 2);

Matrix B(2, 3);

B.set(7, 0, 0); B.set(8, 0, 1); B.set(9, 0, 2);

B.set(10, 1, 0); B.set(11, 1, 1); B.set(12, 1, 2);

cout << "Matrix A:" << endl << A;

cout << "Matrix A*10:" << endl << A * 10;

cout << "Matrix B:" << endl << B;

cout << "Matrix B/10:" << endl << B / 10;

Matrix C; C = A * (~B);

cout << "Matrix C = A * (~B):" << endl << C;

Matrix D; D = (~A) * B;

cout << "Matrix D = (~A) * B:" << endl << D;

cout << "Inversion of Matrix C:" << endl << !C;

cout << "Inversion of Matrix D:" << endl << !D;

Matrix E(A); E.gaussjordan(); cout << "Solution of Gauss-Jordan on A:" << endl << E;

Matrix F(B); F.gaussjordan(); cout << "Solution of Gauss-Jordan on B:" << endl << F;

return 0;

}

// in c++The question is...For matrix multiplication, if we are given two matrices A (N by M), matrix B (M by L) which result in matrix C (N by L). Write the newly added code for the operator* function below given Matrix B as an argument; Matrix A is the current object. The matrix checks and configuration are already done for you. C is a zero N by L matrix.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5. Suggest a mechanism for this rearrangement. Me 'N Br2 'N NaOH Me

Answered: 1 week ago

Question

demonstrate the importance of induction training.

Answered: 1 week ago