Answer to Question #126969 in C++ for hussain

Question #126969
Develop a class of 2 x 2 matrices of double precision floating point variables that has the features listed below.
• A method (function) that returns the determinant of the matrix.
• A method that returns the inverse of the matrix, if it exists.
• Overloading of the assignment operator, allowing us to write code such as A =B; for instances of the class A and B.
1
Expert's answer
2020-07-21T13:34:25-0400
#pragma once
#include <cmath>
class Matrix {
public:
    Matrix() : x11(0.0), x12(0.0), x21(0.0), x22(0.0) {}
    Matrix(double x11, double x12, double x21, double x22)
        : x11(x11), x12(x12), x21(x21), x22(x22) {}
public:
    double determinant() {
        return x11 * x22 - x12 * x21;
    }
    Matrix inverse() {
        double determinant = this->determinant();
        if (determinant == 0) {
            return Matrix();
        }
        double x11 = (pow(-1, 2) * this->x22) / determinant;
        double x12 = (pow(-1, 3) * this->x12) / determinant;
        double x21 = (pow(-1, 3) * this->x21) / determinant;
        double x22 = (pow(-1, 4) * this->x11) / determinant;
        return Matrix(x11, x12, x21, x22);
    }
public:
    Matrix operator=(Matrix mat) {
        return Matrix(mat);
    }
private:
    double x11, x12, x21, x22;
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS