Header Ads

You have to write a program in C++ to find ONLY the Left Hand Side of this property means `(A + B)^T`.

The following property is true for any two matrices of the same order:

`(A + B)^ T = A^T + B^T `

Here...

  • A & B are two matrices of the same order.
  • T stands for the transposition of a matrix.
  • AT and BT are transposed of matrices A and B respectively.
  • The left side of the above equation means:
    • Two matrices A and B are added first and then
    • The transpose of the resultant is computed.
  • The right side means:
    • Transpose of both matrices A and B are computed first and then
    • Their resultants (transposes) are added.

You have to write a program in C++ to find ONLY the Left Hand Side of this property means `(A + B)^T`.








For this purpose:

Write a class MatrixProperty: 

This class should have:
  • A two dimensional array named matrix of integers of dimension 2 x 2. 
  • A setter method setMatrix () which should assign the passed array to the array matrix.
  • It should have two constructors:
    • A default constructor which should initialize the array matrix to zero.
    • A parametrized constructor which should take an array as a parameter and initialize the array matrix with this passed array.
      • Here you should call the setMatrix() to do the task.
  • An overloaded + operator which should take references to two objects of MatrixProperty and
    • Add the two matrices(arrays) of these two objects.
    • Return a reference to the resultant matrix.
  • A friend function Transpose() which should take one argument:
    • a reference to an object of MatrixProperty
    • Find transpose of this passed array matrix and
    • Return a reference to this resultant matrix.
  • A function display() which could either take a reference to an object of  MatrixProperty or use this pointer 
    • To display the passed matrix.

In the main() function, you should:

  • Make TWO instances of this class MatrixProperty using the following matrices(arrays).

  • Add the above TWO created matrices using the overloaded + operator.
  • Call Transpose() function to find transpose of resultant(sum) matrix (done in the above step).
  • Call Display() function to:
    • display both matrices A and B.
    • display the sum(matrix).
    • display the transpose(matrix).



Solution:

Program:

#include <iostream>

using namespace std;

class MatrixProperty {

    int matrix[2][2];

    public:

    // Default constructor

    MatrixProperty() {

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

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

                matrix[i][j] = 0;

            }

        }

    }

    // Parametrized constructor

    MatrixProperty(int temp[2][2]) {

        setMatrix(temp);

    }


    // Setter method

    void setMatrix(int temp[2][2]) {

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

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

                matrix[i][j] = temp[i][j];

            }

        }

    }

   










// Overloaded + operator

    MatrixProperty operator+(MatrixProperty &B) {

        MatrixProperty temp;

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

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

                temp.matrix[i][j] = matrix[i][j] + B.matrix[i][j];

            }

        }

        return temp;

    }

    // Friend function for transpose

    friend MatrixProperty Transpose(MatrixProperty &A) {

        MatrixProperty temp;

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

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

                temp.matrix[i][j] = A.matrix[j][i];

            }

        }

        return temp;

    }

    // Display function

    void display() {

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

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

                cout << matrix[i][j] << " ";

}

            cout << endl;

        }

    }

};

int main() {

    int a[2][2] = {{1, 2}, {3, 4}};

    int b[2][2] = {{5, 7}, {6, 2}};

    // Create two instances of class MatrixProperty

    MatrixProperty A(a);

    MatrixProperty B(b);

    // Add the matrices using overloaded + operator

    MatrixProperty C = A + B;

    // Find transpose of sum matrix

    MatrixProperty D = Transpose(C);

    // Display matrices A and B

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

    A.display();

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

    B.display();

    // Display sum matrix

    cout << "Sum of A and B:" << endl;

    C.display();

    // Display transpose of sum matrix

    cout << "Transpose of sum of A and B:" << endl;

    D.display();

    return 0;

}






Output:





No comments

Powered by Blogger.