Find answers, ask questions, and connect with our <br>community around the world.

Home Forums CPP scientific computing discussion Assignment in class

  • Posted by Yagna Murthy on December 31, 2023 at 6:32 pm

    I am doing the matrix 2×2 task from Module 6; I am approaching this problem similarly to a complex number task, so I created a header file, function file, and main class. I created an inverse method with an argument that takes the reference of the matrix and returns the reference of the inverse matrix, but before sending the input, I tried to print the matrix. It shows a different matrix. Could you please help me out?

    Here I am sharing functions.cpp and main method.

    Nishant replied 3 months, 2 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • Nishant

    Member
    January 2, 2024 at 10:55 am

    Hi Yagna Murthy,

    Please refer to the code below.

    #include <iostream>

    class Matrix2x2 {

    private:

    double data[2][2];

    public:

    // Default constructor initializing all entries to zero

    Matrix2x2() {

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    data[i][j] = 0.0;

    }

    }

    }

    // Constructor specifying four entries of the matrix

    Matrix2x2(double a, double b, double c, double d) {

    data[0][0] = a;

    data[0][1] = b;

    data[1][0] = c;

    data[1][1] = d;

    }

    // Copy constructor

    Matrix2x2(const Matrix2x2& other) {

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    data[i][j] = other.data[i][j];

    }

    }

    }

    // Method to calculate determinant

    double determinant() const {

    return data[0][0] * data[1][1] – data[0][1] * data[1][0];

    }

    // Method to calculate inverse (if it exists)

    Matrix2x2 inverse() const {

    double det = determinant();

    if (det == 0.0) {

    std::cerr << “Inverse does not exist (Determinant is zero)” << std::endl;

    return Matrix2x2(); // Return default matrix

    }

    double invDet = 1.0 / det;

    Matrix2x2 inv(

    data[1][1] * invDet,

    -data[0][1] * invDet,

    -data[1][0] * invDet,

    data[0][0] * invDet

    );

    return inv;

    }

    // Overloading assignment operator

    Matrix2x2& operator=(const Matrix2x2& other) {

    if (this != &other) {

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    data[i][j] = other.data[i][j];

    }

    }

    }

    return *this;

    }

    // Overloading unary subtraction operator

    Matrix2x2 operator-() const {

    Matrix2x2 negated;

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    negated.data[i][j] = -data[i][j];

    }

    }

    return negated;

    }

    // Overloading binary addition operator

    Matrix2x2 operator+(const Matrix2x2& other) const {

    Matrix2x2 result;

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    result.data[i][j] = data[i][j] + other.data[i][j];

    }

    }

    return result;

    }

    // Overloading binary subtraction operator

    Matrix2x2 operator-(const Matrix2x2& other) const {

    Matrix2x2 result;

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    result.data[i][j] = data[i][j] – other.data[i][j];

    }

    }

    return result;

    }

    // Method to multiply matrix by a scalar

    Matrix2x2 multiplyByScalar(double scalar) const {

    Matrix2x2 result;

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    result.data[i][j] = data[i][j] * scalar;

    }

    }

    return result;

    }

    // Display the matrix

    void display() const {

    for (int i = 0; i < 2; ++i) {

    for (int j = 0; j < 2; ++j) {

    std::cout << data[i][j] << ” “;

    }

    std::cout << std::endl;

    }

    }

    };

    int main() {

    Matrix2x2 A; // Default constructor

    Matrix2x2 B(1, 2, 3, 4); // Constructor with specified entries

    Matrix2x2 C = B; // Copy constructor

    Matrix2x2 D;

    D = -C; // Unary subtraction

    Matrix2x2 E = B + C; // Binary addition

    Matrix2x2 F = B – C; // Binary subtraction

    double scalar = 2.0;

    Matrix2x2 G = B.multiplyByScalar(scalar); // Multiplication by scalar

    std::cout << “Matrix A:” << std::endl;

    A.display();

    std::cout << “\nMatrix B:” << std::endl;

    B.display();

    std::cout << “\nMatrix C (Copy of B):” << std::endl;

    C.display();

    std::cout << “\nMatrix D (Negative of C):” << std::endl;

    D.display();

    std::cout << “\nMatrix E (B + C):” << std::endl;

    E.display();

    std::cout << “\nMatrix F (B – C):” << std::endl;

    F.display();

    std::cout << “\nMatrix G (B * ” << scalar << “):” << std::endl;

    G.display();

    std::cout << “\nDeterminant of A: ” << A.determinant() << std::endl;

    Matrix2x2 A_inv = A.inverse();

    A_inv.display();

    std::cout << “\nDeterminant of B: ” << B.determinant() << std::endl;

    Matrix2x2 B_inv = B.inverse();

    B_inv.display();

    return 0;

    }

Log in to reply.

error: Content is protected !!