V
V
Vilmof202020-11-04 18:47:49
C++ / C#
Vilmof20, 2020-11-04 18:47:49

How to create a class that checks if the matrix is ​​normal?

It is necessary to create a class that checks if the matrix is ​​normal (the dimension of the matrix and its elements are entered from the keyboard by the user). But it's not clear to me how to implement this in C++.

When I create two objects, each has a matrix and I need to get two matrices one input and the other transposed with respect to input so that I can multiply them element by element and get the identity matrix.

The problem is that I want to transfer the elements of matrix A to object B in the original and keep them static there, and transpose only object A and get two matrices on which you can perform the corresponding mathematical operations, when I transpose matrix A, then the elements are transposed into B matrices, but I don't need this.

My work:

#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
class Myclass{
    public:
    int rows;
    int cols;
    int **arr;
    
    Myclass(int rows,int cols)
    {
     this->rows=rows;
     this->cols=cols;
     this->arr=new int*[rows];
      for(int i=0;i<rows;i++)
     {
        arr[i]=new int[cols];
        
     }  
    
    }
    
   void input(int rows,int cols)
   {
    for(int i=0;i<rows;i++)
     {
       for(int j=0;j<cols;j++)
       {
        cin>>arr[i][j];
       }
     }  
   }
   void inputb(int rows,int cols)
   {
    for(int i=0;i<rows;i++)
     {
       for(int j=0;j<cols;j++)
       {
        arr[i][j]=0;
       }
     }  
   }
   void print()
   {
    for(int i=0;i<rows;i++)
     {
       for(int j=0;j<cols;j++)
       {
        cout<<arr[i][j]<<"\t";
       }
       cout<<endl;
     }  
   }
   void transpont(int rows,int cols)
   {
    int t;
    for(int i = 0; i < rows; ++i)
    {
        for(int j = i; j < cols; ++j)
        {
            t = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = t;
        }
    }
   }
    Myclass & operator=(const Myclass &other)
        {
         this->rows=other.rows;
         this->arr=new int*[other.rows];
         for(int i=0; i<other.rows; i++)
         {
            this->arr[i]=other.arr[i];
         }
         
         return *this;
           
        }
};
int main()
{
    int r,c;
    cout<<"Enter rows:";
    cin>>r;
    cout<<"Enter cols:";
    cin>>c;
    
    Myclass a(r,c);
    Myclass b(r,c);
   
        a.input(r,c);
        b.inputb(r,c);
 
    a.print();
    a.transpont(r,c);
    cout<<"_______B____"<<endl;
    b.print();
    b=a;
    cout<<"_______B_ after b=a___"<<endl;
    b.print();
        cout<<"_______A____"<<endl;
        a.print();
 
 
    _getch();
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NIKITF, 2021-08-04
@NIKITF

Hello.
By multiplying the original and transposed matrix, you will not get the identity matrix in any way. This is possible in the case of finding the inverse matrix.
Why do you need to statically store large data (matrix)? It's inefficient.
I offer my solution:

#include <iostream>
#include<iomanip>
#include<string>
using namespace std;
bool inp(short& a)
{
  string t; cin >> t; short b;
  try
  {
    b = stoi(t);
  }
  catch (exception)
  {
    cout << "invalid input"; return 1;
  }
  a = b; return 0;
}
class TMatrix
{
private:   
  short n, m;
public:
  short** arr = NULL;
  bool set_size()
  {
    cout << "Enter number of columns: ";
    if (inp(n) || n < 2)
    {
      return 1;
    }
    cout << "Enter number of rows: ";
    if (inp(m) || m < 2)
    {
      return 1;
    }
    arr = new short* [n];
    for (int i = 0; i < n; i++)
    {
      arr[i] = new short[m];
    }
    return 0;
  };
  void transpose()
  {
    for (auto i = 0; i < n; i++)
    {
      for (auto j = i + 1; j < n; j++)
      {
        swap(arr[i][j], arr[j][i]);
      }
    }
  }
  TMatrix(const TMatrix& obj)
  {
    n = obj.n; m = obj.m;
    arr = new short* [n];
    for (int i = 0; i < n; i++)
    {
      arr[i] = new short[m];
    }
    for (auto c = 0; c < n; c++)
    {
      for (auto v = 0; v < m; v++)
      {
        arr[c][v] = obj.arr[c][v];
      }
    }
  }
  void multiply(const TMatrix& obj)
  {
    cout << "matrix C:" << endl;
    short reptiloid{ 0 };
    for (auto i = 0; i < n; i++)
    {
      for (auto j = 0; j < obj.m; j++)
      {
        for (auto i1 = 0; i1 < m; i1++)
        {
           reptiloid += arr[i][i1] * obj.arr[i1][j];
        }
        cout << setw(4) << reptiloid; reptiloid = 0;
      }
      cout << endl;
    }
  }
  TMatrix() {    }
  ~TMatrix()
  {
    for (auto i = 0; i < n; i++)
    {
      delete arr[i];
    }
  };
  void show()
  {
    cout << endl << endl;
    for (auto c = 0; c < n; c++)
    {
      for (auto v = 0; v < m; v++)
      {
        cout << setw(7) << arr[c][v];
      }
      cout << endl;
    }
    cout << endl << endl;
  }
  bool set_elem()
  {
    for (auto i = 0; i < n; i++)
    {
      for (auto j = 0; j < m; j++)
      {
        cout << "Enter [" << i << "][" << j << "]: ";
        if (inp(arr[i][j]))
        {
          cout << "invalid input";
          return 1;
        }
      }
      cout << endl;
    }
    return 0;
  };
};
int main()
{
  TMatrix Amas;
  if (!Amas.set_size())
  {
    if (Amas.set_elem())
    {
      return 0;
    }
  }
  Amas.show();
  TMatrix Bmas = Amas;
  Bmas.show();
  Bmas.transpose();
  Bmas.show();
  cout << endl;
  Amas.multiply(Bmas);
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question