Hi, I'm a newbie trying to debug my program dealing with a 2x2 matrix and 2 datasets. The program has a function to calculate the determinant, a Boolean function to determine if the matrix is singular, a function to calculate the inverse of the matrix (if possible) and a print function. Below is the code:
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
private:
double value1; // Matrix values
double value2;
double value3;
double value4;
public:
double determ;
bool sing;
Matrix (double, double, double, double); // Constructor with defaults
Matrix (const Matrix&); // Copy Contributor
double det (double, double, double, double); // Member function to calculate the determinant
bool isSingular (double); // Member function to determine if the matrix is singular
void inverse (double, double, double, double); // Member function to create inverse matrix
void print_em (double, double, double, double, double, double, double, double); // Member function to print the matrix and its inverse
};
// Code for member functions defined outside the class
Matrix::Matrix (double v1, double v2, double v3, double v4) // Copy constructor
{
value1 = v1;
value2 = v2;
value3 = v3;
value4 = v4;
}
Matrix::Matrix(const Matrix& oldValue)
{
value1 = oldValue.value1;
value2 = oldValue.value2;
value3 = oldValue.value3;
value4 = oldValue.value4;
}
double Matrix::det(double value1, double value2, double value3, double value4) // Calculates the determinant
{
double determ;
determ = (value1 * value4) - (value2 * value3);
return determ;
}
bool Matrix::isSingular(double determ) //Determines if matrix is singular
{ //i.e. Is the determinant equal to zero?
bool sing; //Declares boolean variable
if (determ = 0)
sing = 1;
else
sing = 0;
return sing;
}
void Matrix::inverse(double valIn1, double valIn2, double valIn3, double valIn4)
{ // Calculates the inverse of the original matrix
valIn1 = (value4 * (1/determ);
valIn4 = (value1 * (1/determ);
valIn2 = (-1 * value2)*(1/determ);
valIn3 = (-1 * value3)*(1/determ);
}
void Matrix::print_em(double value1, double value2, double value3, double value4, double valIn1, double valIn2, double valIn3, double valIn4)
{ // Prints matrix and its inverse
cout << setiosflags(ios::fixed) << setprecision(1); // Sets outputs for one decimal
if (sing = 0)
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Inverse of Function: " << endl;
cout << "[ " << valIn1 << " " << valIn2 << " ]" << endl;
cout << "[ " << valIn3 << " " << valIn4 << " ]" << endl << endl << endl;
}
else
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Matrix has no inverse; determinant = 0!" << endl;
cout << "[ N/A N/A ]" << endl;
cout << "[ N/A N/A ]" << endl << endl << endl;
}
}
int main()
{
Matrix mat1 (1, 2, 3, 4);
Matrix mat2 (1, 2, 2, 4); //Two class variables
mat1.det(); //First matrix's determinant is calculated
mat1.isSingular(); //First matrix is checked for singularity
if (sing = 0) //If matrix 1 is not singular, calculates inverse
{
mat1.inverse(); //Then prints matrix and its inverse
mat1.print_em();
}
else
{
mat1.print_em(); //Otherwise, if first matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
mat2.det(); //Second matrix's determinant is calculated
mat2.isSingular(); //Second matrix is checked for singularity
if (sing = 0) //If matrix 2 is not singular, calculates inverse
{
mat2.inverse(); //Then prints matrix and its inverse
mat2.print_em();
}
else
{
mat2.print_em(); //otherwise, if second matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
return 0;
}
Any advice and direction is appreciated.