Question

In: Computer Science

create a class matrix.Let U and V be the two matrices of type Int and number...

create a class matrix.Let U and V be the two matrices of type Int and number of rows and columns are user defined.
Write the following member functions of the class
a) Add
Add the two matrices U and V. For this, add the corresponding entries, and place their sum in the
corresponding index of the result matrix.
b) Subtract
Subtract the two matrices U and V. For this, subtract the corresponding entries, and place this answer in
the corresponding index of the result matrix.
c) Multiply
Multiply the two matrices U and V. In matrix multiplication, one row element of the first matrix is
multiplied by all column elements of second matrix and place result in matrix

Solutions

Expert Solution

#include<iostream>

using namespace std;

class Matrix
{
  private:
    int U[10][10], V[10][10], Sum[10][10], Diff[10][10], Pdt[10][10];
    int r1, c1, r2, c2;

 public:
 // Read matrix U and V
  void Read()
  {
      cout << "Enter number of rows of matrix U : ";
      cin >> r1;
      cout << "Enter number of columns of matrix U : ";
      cin >> c1;

      cout << "Enter elements of matrix U : " << endl;
      for(int i=0; i<r1; i++) {
        cout << "Enter elements of row " << i+1  << endl;
        for(int j=0; j<c1; j++) {
            cin >> U[i][j];
        }
      }

      cout << "Enter number of rows of matrix V : ";
      cin >> r2;
      cout << "Enter number of columns of matrix V : ";
      cin >> c2;

      cout << "Enter elements of matrix V : " << endl;
      for(int i=0; i<r2; i++) {
        cout << "Enter elements of row " << i+1  << endl;
        for(int j=0; j<c2; j++) {
            cin >> V[i][j];
        }
      }
  }

// ADD matrix U and V
  void Add()
  {
     if(r1 == r2 && c1== c2) {

          for(int i=0; i<r1; i++) {
            for(int j=0; j<c1; j++) {
                Sum[i][j] = U[i][j] + V[i][j];
            }
          }

          cout << "\n SUM OF U & V is " << endl;
          for(int i=0; i<r1; i++) {
                cout << endl;
            for(int j=0; j<c1; j++) {
                cout << "\t" << Sum[i][j] ;
            }
          }

     } else {
         cout << "\n Can't add Matrices U and V" << endl;
     }

  }

// SUBTRACT matrix U and V
  void Subtract()
  {
    if(r1 == r2 && c1== c2) {

          for(int i=0; i<r1; i++) {
            for(int j=0; j<c1; j++) {
                Diff[i][j] = U[i][j] - V[i][j];
            }
          }

          cout << "\n DIFFERENCE OF U & V is " << endl;
          for(int i=0; i<r1; i++) {
                cout << endl;
            for(int j=0; j<c1; j++) {
                cout << "\t" << Diff[i][j];
            }
          }

     } else {
         cout << "\n Can't subtract Matrices U and V" << endl;
     }
  }

// MULTIPLY matrix U and V
  void Multiply()
  {
    if(r2 == c1) {

          for(int i=0; i<r1; i++) {
            for(int j=0; j<c2; j++) {
               Pdt[i][j] = 0;
                for(int k=0; k<r1; k++) {
                  Pdt[i][j] = Pdt[i][j] + (U[i][k] * V[k][j]);
                }
            }
          }

          cout << "\n PRODUCT OF U & V is " << endl;
          for(int i=0; i<r1; i++) {
                cout << endl;
            for(int j=0; j<c2; j++) {
                cout << "\t" << Pdt[i][j];
            }
          }

     } else {
         cout << "\n Can't multiply Matrices U and V" << endl;
     }

  }

};

// main function
int main()
{
  Matrix m;
  m.Read();
  m.Add();
  m.Subtract();
  m.Multiply();

  return 0;
}

SAMPLE OUTPUT


Related Solutions

Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int...
Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int size - of type String color - of type String price - of type double Create set methods for the order number, size, and color and get methods for all four fields. The price is determined by the size: $22.99 for XXL or XXXL, and $19.99 for all other sizes. Create a subclass named CustomTee that descends from TeeShirt and includes a field named...
show that for any two vectors u and v in an inner product space ||u+v||^2+||u-v||^2=2(||u||^2+||v||^2) give...
show that for any two vectors u and v in an inner product space ||u+v||^2+||u-v||^2=2(||u||^2+||v||^2) give a geometric interpretation of this result fot he vector space R^2
If V = U ⊕ U⟂ and V = W ⊕ W⟂, and if S1: U...
If V = U ⊕ U⟂ and V = W ⊕ W⟂, and if S1: U → W and S2: U⟂ → W⟂ are isometries, then the linear operator defined for u1 ∈ U and u2 ∈ U⟂ by the formula S(u1 + u2) = S1u1 + S2u2 is a well-defined linear isometry. Prove this.
Let u and v be two integers and let us assume u^2 + uv +v^2 is...
Let u and v be two integers and let us assume u^2 + uv +v^2 is divisible by 9. Show that then u and v are divisible by 3. (please do this by contrapositive).
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
Prove that if U, V and W are vector spaces such that U and V are...
Prove that if U, V and W are vector spaces such that U and V are isomorphic and V and W are isomorphic, then U and W are isomorphic.
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call computeFibRecurse(n). private int computeFibRecurse(int n), which should recurse (that is, call itself) unless n is 1 or 2. If n is 1 or 2, the method should return 1. A main method that prints “STARTING”, then constructs a FibGenerator generator and then calls nthFib(), passing in interesting values. To look into this problem, you’re going to use software to analyze software. Add an instance...
Let U be a subspace of V . Prove that dim U ⊥ = dim V...
Let U be a subspace of V . Prove that dim U ⊥ = dim V −dim U.
Suppose u, and v are vectors in R m, such that ∥u∥ = 1, ∥v∥ =...
Suppose u, and v are vectors in R m, such that ∥u∥ = 1, ∥v∥ = 4, ∥u + v∥ = 5. Find the inner product 〈u, v〉. Suppose {a1, · · · ak} are orthonormal vectors in R m. Show that {a1, · · · ak} is a linearly independent set.
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT