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

Please help me solve this c++ problem. Implement a class Matrix The Matrix.h hea

ID: 3739489 • Letter: P

Question

Please help me solve this c++ problem.

Implement a class Matrix

The Matrix.h header file is provide with member function and variable

Write the Matrix.cpp file where you'll defne each function clearly with comment next to the code

Everything in Matrix.cpp should be written according to informations inside Matrix.h, please do not attempt to change"void setData(int, int, double)" function, it's already written

Finally write a main function to test the above program

Notice: this is an obeject oriented on Matrix implementation in C++

-------------------------------------------------------------Matrix.h----------------------------------------------------------------------

#ifndef MATRIX_H

#define MATRIX_H

#include <iostream>

using namespace std;

class Matrix {

private:

double ** data; // A two-dimensional double matrix

int row; // save data row

int col; // save data col

void setUp(int, int); // utility function, Set row and col value

public:

// default constructor, No argument passed, set it to 2*2 double data matrix with all values been 0

Matrix();

// constructor, set matrix size

Matrix(int, int);

// constructor, Save the incoming one-dimensional array as a two-dimensional array into data

Matrix(int, int, double [], int);

// copy constructor

Matrix(const Matrix &);

// destructor, clean up data

~Matrix();

void setData(int, int, double); // set each position (r,c)its with a data value

int getRow() const; // return private member: row

int getCol() const; // return private member: col

double getData(int, int) const; // Get value inserted in position (r,c) of matrix

Matrix add(const Matrix &); // Add two matrices, return the sum

Matrix multiply(const Matrix &);//Multiply two matrices, return the product

Matrix transpose(const Matrix &);// Transpose a matrix and return its transpose

  

void displayData() {// Print matrix data (Don’t modify)

for (int i = 0;i<row; i++) {

for (int j = 0;j<col; j++) {

cout<<data[i][j]<<" ";

}

cout<<""<<endl;

}

}

};

#endif

Explanation / Answer

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <limits>

#include "matrix.h"

using std::cout; using std::endl;

//Constructors/destructors:

Matrix:: Matrix(){

int PA[2][2]={}; // constructor assigning 0 to values of 2*2 matrix

return PA;

}

Matrix:: Matrix(int nrows, int ncols){

int PA[nrows][ncols]={}; // constructor assigning 0 to values of nrows*ncols matrix

return PA;

}

  

//constructor from a pointer-to-pointer

Matrix:: Matrix(int nrows, int ncols, const double* const* pa){

nRows = nrows;

nCols = ncols;

pA[nrows][ncols]={};

  

for(int i = 0; i < nRows; i++)

for(int j = 0; j < nCols; j++)

pA[i][j] = pa[i][j];

}

//copy constructor

Matrix::Matrix(const Matrix& A){

nRows = A.nRows;

nCols = A.nCols;

pA = new(std::nothrow) double*[nRows];

pointerCheck();

for(int i = 0; i < nRows; i++){

pA[i] = new(std::nothrow) double[nCols];

pointerCheck(i);

}

for(int i = 0; i < nRows; i++)

for(int j = 0; j < nCols; j++)

pA[i][j] = A.pA[i][j];

}   

  

//destructor

Matrix::~Matrix(){

if(pA != 0){

for(int i = 0; i < nRows; i++)

delete[] pA[i];

delete[] pA;

}

}

void setData(int r, int c, double d){// set each position (r,c)its with a data value

Matrix:: data[r][c]=d;

}

int getRow() const // return private member: row

{

return Matrix::row;

}

int getCol() const; // return private member: col

{

return Matrix::col;

}

double getData(int r, int c,const Matrix & m1) const; // Get value inserted in position (r,c) of matrix

{

return Matrix::data[r][c];

}

Matrix add(const Matrix & m1, const Matrix & m2); // Add two matrices, return the sum

{

matrix temp;

temp.row=m1.row;

temp.col=m2.col;

for(int i=1; i<=temp.row; i++)

for(int j=1; j<=temp.col; j++)

{

  

   temp.a[i][j] = m1.a[i][j]+m2.a[i][j];

}

return temp;

}

Matrix multiply(const Matrix & m1, const Matrix & m2 )//Multiply two matrices, return the product

{

matrix temp;

temp.row=m1.row;

temp.col=m2.col;

for(int i=1; i<=temp.row; i++)

for(int j=1; j<=temp.col; j++)

{

   temp.a[i][j]=0;

   {   

for (int k=0; k<=temp.col; k++)

   temp.a[i][j] += m1.a[i][k]*m2.a[k][j];

   }

}

return temp;

}

Matrix transpose(const Matrix &PA){ //transpose of the matrix

Matrix B(nCols, nRows);

for(int i = 0; i < nCols; i++)

for(int j = 0; j < nRows; j++)

B.pA[i][j] = pA[j][i];

return B;

}

int main()

{

Matrix m;

//write functions which you want to examine

}