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

* Create a Matrix class. Note that the first letter is in upper case. * This Mat

ID: 3621391 • Letter: #

Question

* Create a Matrix class. Note that the first letter is in upper case.
* This Matrix class is to have three private data members that store the number of rows, number of columns, and matrix data. You may name these data members anything that you like. To keep things simple, these data members will store double values.
* This Matrix class is to have the following public methods:
o setMatrix(string filename)

This method is to take a string parameter that is a file that holds a matrix, as defined in previous assignments. Note this is just a single matrix, so numrows, numcols and then the data. This method is not to return any value.
o getNumrows()

This method is to have no parameters and it should return the number of rows if the matrix.
o getNumcols()

This method is to have no parameters and it to return the number of columns in the matrix.
o printMatrix()

This method is to have no parameters and it is not to return any value. This method is to print to cout the matrix.
o getIsSquare()

This method is to have no parameters and is to return a bool value. If the matrix is square, the function should return true, if not, the function shoudl return false.
o det()

This method is to take no parameters and to return a floating point number. The return value is to be determinant of the matrix
* In addition you should overload + and << so that you can do the following with two instances of the matrix class

cout << matrix1 + matrix2;

Explanation / Answer

please rate - thanks

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class Matrix
{friend ostream &operator <<(ostream &,const Matrix &);
private:
     double **ptr;
     int rsize;
     int csize;
public:
      Matrix();
      Matrix(int,int);
      int getNumrows();
      int getNumcols();
      void printMatrix();
      double det();
      void setMatrix(string);
      bool getIsSquare();
      Matrix operator + (Matrix a);
};
int main()
{Matrix a,b,c;
a.setMatrix("input1.txt");
b.setMatrix("input2.txt");
c.setMatrix("input3.txt");
cout<<"matrix a ";
a.printMatrix();
cout<<"matrix b ";
b.printMatrix();
cout<<"matrix c ";
c.printMatrix();
cout<<"The determinant of c is "<<c.det()<<endl;
if(a.getIsSquare())
    cout<<"a is square ";
else
    cout<<"a is not square ";
if(b.getIsSquare())
    cout<<"b is square ";
else
    cout<<"b is not square ";
Matrix d(a.getNumrows(),a.getNumcols());
cout<<"a+c ";
cout<<a+c;
  

system("pause");
return 0;
}
void Matrix::setMatrix(string filename)
{ ifstream input;
input.open(filename.c_str());
input>>rsize>>csize;
int i,j;

for(i=0;i<rsize;i++)
     for(j=0;j<csize;j++)
        input>>ptr[i][j];
input.close();
}
Matrix::Matrix()
         {rsize=10;
          csize=10;
          ptr = new double *[csize];
          for (int k = 0; k < rsize; k++)
              ptr [k] = new double [rsize];
          int i,j;
          for(i=0;i<rsize;i++)
             for(j=0;j<csize;j++)
                 ptr[i][j]=0.0;
          }
      Matrix::Matrix(int n,int m)
           {rsize=n;
           csize=m;
           ptr = new double *[csize];

           for (int k = 0; k < rsize; k++)
               ptr [k] = new double [rsize];
           int i,j;
           for(i=0;i<rsize;i++)
             for(j=0;j<csize;j++)
                 ptr[i][j]=0.0;
          }
      int Matrix::getNumrows()
        {return rsize;
        }
      int Matrix::getNumcols()
        {return csize;
        }
      void Matrix::printMatrix()
      {int i,j;
            for(i=0;i<rsize;i++)
              {for(j=0;j<csize;j++)
                 cout<<setw(5)<<ptr[i][j]<<" ";
               cout<<endl;
              }
       cout<<endl;
       }   
     
bool Matrix::getIsSquare()
{if(csize==rsize)
      return true;
else
      return false;
}
Matrix Matrix::operator + (Matrix a)
{int i,j;
Matrix t(rsize,csize);
   for(i = 0; i < rsize; i++)
      for(j = 0; j < csize; j++)
         t.ptr[i][j] = ptr[i][j]+a.ptr[i][j];
return t;

}
      

double Matrix::det()   //FUNCTION TO CALCULATE DETERMINANT
{ int i,j,k;
double mult,d=1;
for(i=0;i<rsize;i++)
{    for(j=0;j<rsize;j++)
    {mult=ptr[j][i]/ptr[i][i];
    for(k=0;k<rsize;k++)
    {if(i==j) break;
        ptr[j][k]=ptr[j][k]-ptr[i][k]*mult;
    }
    }
}
for(i=0;i<rsize;i++)
    d=d*ptr[i][i];
return d;
}

ostream &operator <<(ostream &strm, const Matrix &a)
      {int i,j;
            for(i=0;i<a.rsize;i++)
              {for(j=0;j<a.csize;j++)
                 cout<<setw(5)<<a.ptr[i][j]<<" ";
               cout<<endl;
              }
       cout<<endl;
       }