Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

May I get this in c++ please. Display all PowS this is a context class, an objec

ID: 3741694 • Letter: M

Question

May I get this in c++ please.

Display all PowS this is a context class, an object that contains objects in order to perform various functions CTX: on them. You can think of it as a "chalkboard" if you will, a place where the matrices will be written and operators performed on them 3) The CTX contains three Mat *, M1, M2, M3, all should initially be set to NULL These pointers can be instantiated randomly with number of rows and cols provided by user or by loading a specific file name from the keyboard. M3 is intended to be a results matrix, but options to set M1 or M2 M3 are required. A menu should be provided as follows:

Explanation / Answer

#include<iostream>
#include<stdlib.h>
using namespace std;
class CTX
{
private:
  int *M1;
  int *M2;
  int *M3;
  int M1m,M1n,M2m,M2n,M3m,M3n;
public:
  int generate_flag;
  CTX()//constructor...................................initial stage.............
  {
   M1m=M1n=M2m=M2n=M3m=M3n=0;
   M1=M2=M3=NULL;
   generate_flag=0;
  }
  //.................................................
  void add()//for additionn of M1 and M2................................option 1.....................
  {
   int i=0,j=0;
   for(i=0;i<M1m;i++)
   {
    for(j=0;j<M1n;j++)
    {
     *(M3 + i*M3n + j)=(*(M1 + i*M1n + j))+(*(M2 + i*M2n + j));
    }
   }
  }
  //.................................................
  void subtract(int flag)//for M1-M2 Or M2-M1...............option 2 & 3
  {
   int i=0,j=0;
   if(flag==1)//for M1-M2
   {
    for(i=0;i<M1m;i++)
    {
     for(j=0;j<M1n;j++)
     {
      *(M3 + i*M3n + j)=(*(M1 + i*M1n + j))-(*(M2 + i*M2n + j));
     }
    }
   }
   else//for M2-M1
   {
    for(i=0;i<M2m;i++)
    {
     for(j=0;j<M2n;j++)
     {
      *(M3 + i*M3n + j)=(*(M2 + i*M2n + j))-(*(M1 + i*M1n + j));
     }
    }
   }
  }
  //.................................................
  void inverse()//for inverse of M1 into .............................option 5......................
  {
   int i,j;
   for(i=0;i<M1m;i++)
   {
    for(j=0;j<M1n;j++)
    {
     *(M3 + j*M3n + i)=(*(M1 + i*M1n + j));
    }
   }
  }
  //.................................................
  void generate_mtrx()//generate matrix by sizing them and getting data from user(initial stage)
  {
   int i,j;
   get_size();//to get size of matrices
   M1=(int *)malloc(M1m*M1n*sizeof(int));
   M2=(int *)malloc(M2m*M2n*sizeof(int));
   get_data();//to get data of matrices from user
   M3=(int *)malloc(M1m*M2n*sizeof(int));//allocating size of M1 to M3
   M3m=M1m;
   M3n=M2n;
   generate_flag=1;
  }
  //.................................................
  void regenerate_mtrx()//re-generate matrix by sizing them again and getting data from user....option 9.....
  {
   get_size();
   M1=(int *)realloc(M1,M1m*M1n*sizeof(int));
   M2=(int *)realloc(M2,M2m*M2n*sizeof(int));
   get_data();
   generate_flag=1;
  }
  //.................................................
  void get_size()//function to get size of matrices(M1 and M2) from user
  {
   int valid=0;
   cout<<" Enter the size Of M1 (2D matrix) ";
   while(!valid)//to check that user entered matrix size for atleast 2 element(atleast M1[1][2] or M1[2][1])
   {
    while(M1m<1)//to check that the size should not less then 1..it will ask the user to enter value of M of matrix M1[M][N] till the valid value(M>1)
    {
     cout<<" m:";
     cin >> M1m;
     if(M1m<1)//error message if size is less then 1
     {
      cout<<" Invalid Value..!!";
     }
     if(M1m>1)
     {
      valid=1;
     }
    }
    while(M1n<1)//to check that the size should not less then 1..it will ask the user to enter value of N of matrix M1[M][N] till the valid value(N>1)
    {
     cout<<" n:";
     cin>>M1n;
     if(M1n<1)//error message if size is less then 1
     {
      cout<<" Invalid Value..!!";
     }
     if(M1n>1)
     {
      valid=1;
     }
    }
    if(!valid)//error message if size is invalid eg. M1[1][1]
    {
     M1m=M1n=0;
     cout<<" Please Enter matrix of atleast 2 elements..!!";
    }
    
   }
   valid=0;
   //......................................................
   cout<<" Enter the size Of M2 (2D matrix) ";
   while(!valid)//to check that user entered matrix size for atleast 2 element(atleast M2[1][2] or M2[2][1])
   {
    while(M2m<1)//to check that the size should not less then 1..it will ask the user to enter value of M of matrix M2[M][N] till the valid value(M>1)
    {
     cout<<" m:";
     cin>>M2m;
     if(M2m<1)//error message if size is less then 1
     {
      cout<<" Invalid Value..!!";
     }
     if(M2m>1)
     {
      valid=1;
     }
    }
    while(M2n<1)
    {
     cout<<" n:";
     cin>>M2n;
     if(M2n<1)//error message if size is less then 1
     {
      cout<<" Invalid Value..!!";
     }
     if(M2n>1)
     {
      valid=1;
     }
    }
    if(!valid)//error message if size is invalid eg. M2[1][1]
    {
     M2m=M2n=0;
     cout<<" Please Enter matrix of atleast 2 elements..!!";
    }
    
   }
   
   
  }
  //.................................................
  void get_data()//to get data from user
  {
   int i=0,j=0,temp;
   cout<<" Enter "<<M1m*M1n<<" Elements for Metrix M1:"<<endl;
   for(i=0;i<M1m;i++)//for M1
   {
    for(j=0;j<M1n;j++)
    {
     cout<<" ";
     cin>>temp;
     *(M1 + i*M1n + j) =temp;
    }
   }
   //........................................................
   cout<<" Enter "<<M2m*M2n<<" Elements for Metrix M2:"<<endl;
   for(i=0;i<M2m;i++)//for M2
   {
    for(j=0;j<M2n;j++)
    {
     cout<<" ";
     cin>>temp;
     *(M2 + i*M2n + j) =temp;
    }
   }
  }
  //.................................................
  void display(int flag)//to display data
  {
   int i,j;
   if(flag==1)//for M1
   {
    cout<<" Matrix M1: ";
    for(i=0;i<M1m;i++)
    {
     for(j=0;j<M1n;j++)
     {
      cout<<" "<<*(M1 + i*M1n + j);
     }
     cout<<endl;
    }
   }
   //..............................................
   if(flag==2)//for M2
   {
    cout<<" Matrix M2: ";
    for(i=0;i<M2m;i++)
    {
     for(j=0;j<M2n;j++)
     {
      cout<<" "<<*(M2 + i*M2n + j);
     }
     cout<<endl;
    }
   }
   if(flag==3)//for M3
   {
    cout<<" Matrix M3: ";
    for(i=0;i<M3m;i++)
    {
     for(j=0;j<M3n;j++)
     {
      cout<<" "<<*(M3 + i*M3n + j);
     }
     cout<<endl;
    }
   }
  }
//.........................functions for resize M3 Metrix......................start...............................
  //.................................................
  void resize(int flag)//for
  {
   if(flag==1)//for sesize M3 matrix according to M1 Matrix
   {
    M3=(int *)realloc(M3,M1m*M1n*sizeof(int));
    M3m=M1m;
    M3n=M1n;
   }
   else//for sesize M3 matrix according to M2 Matrix
   {
    M3=(int *)realloc(M3,M2m*M2n*sizeof(int));
    M3m=M2m;
    M3n=M2n;
   }
   
  }
  //...............................................
  void resize_inverse()//for Resizing M3 To hold Inverse of M1[M][N]..............option
  {
   M3=(int *)realloc(M3,M1n*M1m*sizeof(int));//Size of Matric M3 should be "N X M" to hold Inverse of "M2[M][N]"
   M3m=M1n;
   M3n=M1m;
  }
  //.................................................
  void resize_multiply()//to resize the M3 matrix to hold M1*M2
  {
   M3=(int *)realloc(M3,M1m*M1n*sizeof(int));
  }
  //.................................................
//.........................functions for resize M3 Metrix..................end...................................
  void set(int flag)//to set matrix M3 to M1 or M2................option 6 & 7.............................
  {
   int i,j;
   if(flag==1)//to set M1 into M3
   {
    for(i=0;i<M1m;i++)
    {
     for(j=0;j<M1n;j++)
     {
      *(M3 + i*M3n + j)=*(M1 + i*M1n + j);
     }
    }
   }
   else//to set M2 into M3
   {
    for(i=0;i<M2m;i++)
    {
     for(j=0;j<M2n;j++)
     {
      *(M3 + i*M3n + j)=*(M2 + i*M2n + j);
     }
    }
   }
  }
  //.................................................
  int validate(int flag)//to validate the matrix for addition, substraction and Multiplication.
  {
   if(flag==1)//for addition and substraction
   {
    if(M1m!=M2m||M1n!=M2n)
    {
     return 0;
    }
   }
   else//for Multiplication
   {
    if(M1m!=M2n)
    {
     return 0;
    }
   }
   return 1;
  }
  //.................................................
  void multiply()//for multiplication of M1 and M2....................option 4................
  {
   int sum=0,c,d,k;
   for (c = 0; c < M1m; c++)
   {
    for (d = 0; d <M2n ; d++)
    {
     for (k = 0; k < M2m; k++)
     {
        sum = sum +(*(M1 + c*M1n + k))*(*(M2 + k*M2n + d));
     }
     *(M3 + c*M3n + d) = sum;
     sum = 0;
    }
   }
  }
};
int main()
{
CTX obj;
obj.generate_mtrx();
obj.display(1);
obj.display(2);

int ch=-1;
while(ch!=9)
{
  if(!obj.generate_flag)
  {
   obj.regenerate_mtrx();
   obj.display(1);
   obj.display(2);
  }
  cout<<" 1. ADD M1 + M2 -> M3";
  cout<<" 2. SUBTRACT M1 + M2 -> M3";
  cout<<" 3. SUBTRACT M2 + M1 -> M3";
  cout<<" 4. MULTIPLY M1 + M2 -> M3";
  cout<<" 5. COMPUTE INVERSE OF M1 or M2 -> M3";
  cout<<" 6. SET M1 -> M3";
  cout<<" 7. SET M2 -> M3";
  cout<<" 8. RETURN TO MATRIX GENERATOR";
  cout<<" 9. EXIT";
  cout<<" ......................................................";
  cout<<"    Enter Your Choice:";
  cin>>ch;
  cout<<" ......................................................";
  switch(ch)
  {
   case 1://ADD M1 + M2 -> M3
    if(!(obj.validate(1)))
    {
     cout<<" Invalid Matrices for Addition..";
    }
    else
    {
     obj.resize(1);
     obj.add();
     obj.display(1);
     obj.display(2);
     obj.display(3);
    }
    break;
   case 2://SUBTRACT M1 + M2 -> M3
    if(!(obj.validate(1)))
    {
     cout<<" Invalid Matrices for Subtraction..";
    }
    else
    {
     obj.resize(1);
     obj.subtract(1);
     obj.display(1);
     obj.display(2);
     obj.display(3);
    }
    break;
   case 3://SUBTRACT M2 + M1 -> M3
    if(!(obj.validate(1)))
    {
     cout<<" Invalid Matrices for Subtraction..";
    }
    else
    {
     obj.resize(1);
     obj.subtract(2);
     obj.display(2);
     obj.display(1);
     obj.display(3);
    }
    break;
   case 4://MULTIPLY M1 + M2 -> M3
    if(!(obj.validate(2)))
    {
     cout<<" Invalid Matrices for Multiplication..";
    }
    else
    {
     if(!obj.validate(2))
     {
      cout<<" Invalid Matrices for Multiplication..!!";
     }
     else
     {
      obj.resize_multiply();
      obj.multiply();
      obj.display(1);
      obj.display(2);
      obj.display(3);
     }
    }
    break;
   case 5://COMPUTE INVERSE OF M1 or M2 -> M3
    obj.resize_inverse();
    obj.inverse();
    obj.display(1);
    obj.display(3);
    break;
   case 6://SET M1 -> M3
    obj.resize(1);
    obj.set(1);
    obj.display(1);
    obj.display(3);
    break;
   case 7://SET M2 -> M3
    obj.resize(2);
    obj.set(2);
    obj.display(2);
    obj.display(3);
    break;
   case 8://RETURN TO MATRIX GENERATOR
    obj.generate_flag=0;
    break;
   default ://EXIT
    if(ch!=9)
    {
     cout<<" Invalid Choice...!!!";
    }
  }
}
return 0;
}