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

I\'m trying to write a matrix class in c++ that can calculate the determinant .

ID: 3920912 • Letter: I

Question

I'm trying to write a matrix class in c++ that can calculate the determinant . Please help me correct four compiler errors that I'm getting in Visual Studio 2015.

C2059 - incorrect use of *

C2143

C2988

here is my header file

#pragma once

#ifndef MATRIX_H

#define MATRIX_H

#include<iostream>

using namespace std;

template<class t, const int ROWS = 2, const int COLUMNS = 2>

class matrix

{

public:

int getRow();

int getCol();

t** getMatrix2D();

//t determinantOfMatrix(); //This is just a getter function.

matrix();

matrix(t** mtrx);

t determinant;

//~matrix();

bool operator==(const t & mtrx) const;

bool operator!=(const t & mtrx) const;

//bool operator<(const t & mtrx) const;

//bool operator>(const t & mtrx) const;

//bool operator<=(const t & mtrx) const;

//bool operator>=(const t & mtrx) const;

//matrix operator+(const t & mtrx);

//matrix operator-(const t & mtrx);

//matrix operator*(const t & mtrx);

private:

void getCof(t** mtrx);

t calculateDeterminant(t** mtrx);

//void checkROWSandCOLUMNS();

int row = ROWS, col = COLUMNS;

t** matrixArray2D = new t[row][col];

t* cofactors;

t* matrixArray = new t[row * col];

};

#endif // !MATRIX_H

Here is the CPP file:

#include"matrix.h"

using namespace std;

template<class t, int ROWS, int COLUMNS>

int matrix<t, ROWS, COLUMNS>::getRow()

{

return row;

}

template<class t, int ROWS, int COLUMNS>

int matrix<t, ROWS, COLUMNS>::getCol()

{

return col;

}

template<class t, int ROWS, int COLUMNS>

t ** matrix<t, ROWS, COLUMNS>::getMatrix2D()

{

return matrixArray2D;

}

//template<class t, int ROWS, int COLUMNS>

//t matrix<t, ROWS, COLUMNS>::determinantOfMatrix()

//{

// return determinant;

//}

template<class t, int ROWS, int COLUMNS>

matrix<t, ROWS = 2, COLUMNS = 2>::matrix()

{

}

template<class t, int ROWS, int COLUMNS>

matrix<t, ROWS, COLUMNS>::matrix(t** mtrx)

{

//checkROWSandCOLUMNS();

//int width = sizeof(mtrx*[0]) / sizeof(mtrx[0][0]); // number of columns(x);

//int height = sizeof(mtrx**) / sizeof(mtrx*[0]); // number of rows(y)

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

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

matrixArray2D[i][j] = mtrx[i][j]; //input array 2d to member array 2d

matrixArray[i*col + j] = mtrx[i][j]; // input array 2d to member array 1d

}

}

determinant = calculateDeterminant(mtrx)

}

template<class t, int ROWS, int COLUMNS>

bool matrix<t, ROWS, COLUMNS>::operator==(const t & mtrx) const

{

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

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

if (mtrx[i][j] != mtrx[i][j]) return false;

return true;

}

template<class t, int ROWS, int COLUMNS>

bool matrix<t, ROWS, COLUMNS>::operator!=(const t & mtrx) const

{

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

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

if (mtrx[i][j] == mtrx[i][j]) return false;

return true;

}

template<class t, int ROWS, int COLUMNS>

t matrix<t, ROWS, COLUMNS>::calculateDeterminant(t** mtrx) {

if (row != col) return -1;

else if (ROWS == 2 && COLUMNS == 2) {

return mtrx[0][0] * mtrx[1][1] - mtrx[0][1] * mtrx[1][0];

}

else {

getCof(mtrx);

t** tempMatrix = new t[row - 1][col - 1];

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

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

tempMatrix[i][j] = mtrx[i + 1][j + 1];

}

}

t sum = 0;

matrix<t, row - 1, col - 1> temp(tempMatrix);

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

sum += cofactors[i] * calculateDeterminant(temp.getMatrix2D());

}

return sum;

}

}

//template<class t, int ROWS, int COLUMNS>

//void matrix<t, ROWS, COLUMNS>::checkROWSandCOLUMNS()

//{

//}

template<class t, int ROWS, int COLUMNS>

void matrix<t, ROWS, COLUMNS>::getCof(t** mtrx)

{

t* cofactorrArray = new t[col];

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

cofactorrArray[j] = mtrx[0][j] ? j % 2 == 0 : mtrx[0][j] * -1;

}

cofactors = cofactorrArray;

}

Thanks in advance.

Explanation / Answer

If you have any doubts, please give me comment...

#pragma once

#ifndef MATRIX_H

#define MATRIX_H

#include <iostream>

using namespace std;

template <class t, const int ROWS = 2, const int COLUMNS = 2>

class matrix

{

public:

int getRow();

int getCol();

t **getMatrix2D();

//t determinantOfMatrix(); //This is just a getter function.

matrix();

matrix(t **mtrx);

t determinant;

//~matrix();

bool operator==(const t &mtrx) const;

bool operator!=(const t &mtrx) const;

//bool operator<(const t & mtrx) const;

//bool operator>(const t & mtrx) const;

//bool operator<=(const t & mtrx) const;

//bool operator>=(const t & mtrx) const;

//matrix operator+(const t & mtrx);

//matrix operator-(const t & mtrx);

//matrix operator*(const t & mtrx);

private:

void getCof(t **mtrx);

t calculateDeterminant(t **mtrx);

//void checkROWSandCOLUMNS();

int row = ROWS, col = COLUMNS;

t **matrixArray2D = new t[row][col];

t *cofactors;

t *matrixArray = new t[row * col];

};

#endif

matrix.cpp

#include "matrix.h"

using namespace std;

template <class t, int ROWS, int COLUMNS>

int matrix<t, ROWS, COLUMNS>::getRow()

{

return row;

}

template <class t, int ROWS, int COLUMNS>

int matrix<t, ROWS, COLUMNS>::getCol()

{

return col;

}

template <class t, int ROWS, int COLUMNS>

t **matrix<t, ROWS, COLUMNS>::getMatrix2D()

{

return matrixArray2D;

}

//template<class t, int ROWS, int COLUMNS>

//t matrix<t, ROWS, COLUMNS>::determinantOfMatrix()

//{

// return determinant;

//}

template <class t, int ROWS, int COLUMNS>

matrix<t, ROWS, COLUMNS>::matrix()

{

}

template <class t, int ROWS, int COLUMNS>

matrix<t, ROWS, COLUMNS>::matrix(t **mtrx)

{

//checkROWSandCOLUMNS();

//int width = sizeof(mtrx*[0]) / sizeof(mtrx[0][0]); // number of columns(x);

//int height = sizeof(mtrx**) / sizeof(mtrx*[0]); // number of rows(y)

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

{

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

{

matrixArray2D[i][j] = mtrx[i][j]; //input array 2d to member array 2d

matrixArray[i * col + j] = mtrx[i][j]; // input array 2d to member array 1d

}

}

determinant = calculateDeterminant(mtrx);

}

template <class t, int ROWS, int COLUMNS>

bool matrix<t, ROWS, COLUMNS>::operator==(const t &mtrx) const

{

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

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

if (mtrx[i][j] != mtrx[i][j])

return false;

return true;

}

template <class t, int ROWS, int COLUMNS>

bool matrix<t, ROWS, COLUMNS>::operator!=(const t &mtrx) const

{

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

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

if (mtrx[i][j] == mtrx[i][j])

return false;

return true;

}

template <class t, const int ROWS,const int COLUMNS>

t matrix<t, ROWS, COLUMNS>::calculateDeterminant(t **mtrx)

{

if (row != col)

return -1;

else if (ROWS == 2 && COLUMNS == 2)

{

return mtrx[0][0] * mtrx[1][1] - mtrx[0][1] * mtrx[1][0];

}

else

{

getCof(mtrx);

t **tempMatrix = new t[row - 1][col - 1];

for (int i = 0; i < row - 1; i++)

{

for (int j = 0; j < col - 1; j++)

{

tempMatrix[i][j] = mtrx[i + 1][j + 1];

}

}

t sum = 0;

matrix<t, ROWS-1, COLUMNS-1> temp(tempMatrix);

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

{

sum += cofactors[i] * calculateDeterminant(temp.getMatrix2D());

}

return sum;

}

}

//template<class t, int ROWS, int COLUMNS>

//void matrix<t, ROWS, COLUMNS>::checkROWSandCOLUMNS()

//{

//}

template <class t, int ROWS, int COLUMNS>

void matrix<t, ROWS, COLUMNS>::getCof(t **mtrx)

{

t *cofactorrArray = new t[col];

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

{

cofactorrArray[j] = mtrx[0][j] ? j % 2 == 0 : mtrx[0][j] * -1;

}

cofactors = cofactorrArray;

}