Pages

CS201 ASSIGNMENT NO. 2 FALL 2022 || 100% RIGHT SOLUTION || INTRODUCTION TO PROGRAMMING || BY VuTech

CS201 ASSIGNMENT NO. 2 FALL 2022 || 100% RIGHT SOLUTION || INTRODUCTION TO PROGRAMMING || BY VuTech

CS201 ASSIGNMENT NO. 2 FALL 2022

 KINDLY, DON’T COPY PASTE

CS201 ASSIGNMENT NO. 2 FALL 2022 || 100% RIGHT SOLUTION || INTRODUCTION TO PROGRAMMING || BY VuTech

Visit Website For More Solutions

 www.vutechofficial.blogspot.com

SEND WHATSAPP OR E-MAIL FOR ANY QUERY

0325-6644800

kamranhameedvu@gmail.com

Instructions

Please read the following instructions carefully before submitting assignment:

It should be clear that your assignment will not get any credit if:

o        Assignment is submitted after due date.

o        Submitted assignment does not open or file is corrupt.

o        Assignment is copied (From internet/students).

Software allowed to develop Assignment

-          Dev C++

Objectives:

To enable students to understand and practice the concepts of:

  • Classes and its structure.
  • Data Members and Member functions of a class.
  • Constructor of a class.
  • Friend function
  • Operator overloading 

Assignment Submission Instructions

You are required to submit only .cpp file on the assignments interface of CS201 at VU-LMS. 
Assignment submitted in any other format will not be accepted and will be graded zero marks.   
So, check your solution file format before submission.

For any query related to assignment, please contact cs201@vu.edu.pk.

Problem Statement

Following property is true for any two matrices of same order:

(A + B) T = AT + BT 

Here...

  • A & B are two matrices of same order.
  • T stands for transpose of a matrix.
  • AT and BT are transpose of matrices A and respectively.
  • Left side of the above equation means:

  1. Two matrices A and B are added first and then
  2. The transpose of the resultant is computed.

  • Right side means:

  1. Transpose of both matrices A and B are computed first and then
  2. 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:

·         default constructor which should initialize the array matrix to zero.

·         parametrized constructor which should take an array as a parameter and initialize the array matrix with this passed array.

v  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.

Ø  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:

v  display both matrices A and B.

v  display the sum(matrix).

v  display the transpose(matrix).

 Note: You may use pointers to pass the array arguments to all functions and do the pointer arithmetic.

(Hint: Transpose of a matrix can be obtained by changing its rows into columns or columns into rows.)

(Hint: Two matrices can be added by adding their corresponding elements.)

 

For consistency of the assignment, please only use the above given matrices(arrays).

Note: Marks will be deducted if the given arrays are not used.

Sample Screenshot of Output:



Good Luck!

Lectures Covered:  This assignment covers Lecture # 23-31.

Deadline: The deadline to submit your assignment solution is 24th January 2023. Your assignment must be submitted within the due date through VU-LMS. No assignment will be accepted through email after the due date.

SOLUTION:



#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 Matrix A and B:" << endl;
    C.display();

    // Display transpose of sum matrix
    cout << "Transpose of Matrix (A+B):" << endl;
    D.display();

    return 0;
}



Visit Website For More Solutions

 www.vutechofficial.blogspot.com

KINDLY, DON’T COPY PASTE

SUBSCRIBE, SHARE, LIKE AND COMMENTS FOR MORE UPDATES

SEND WHATSAPP OR E-MAIL FOR ANY QUERY

0325-6644800

kamranhameedvu@gmail.com