You are on page 1of 8

class Matrix

{
private:
int row, col, scalar;
int matrix[10][10];
int result[10][10];

public:
// Default Constructor.
Matrix()
{
row = 0;
col = 0;
scalar = 0;
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
matrix[i][j] = 0;
result[i][j] = 0;
} } }

// Constructor.
Matrix(int r, int c, int s)
{
row = r;
col = c;
scalar = s;
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
matrix[i][j] = 0;
result[i][j] = 0;
} } }

// Operator overload.
int &operator()(int row, int col)
{
return matrix[row][col];
}
// Get matrix size and populate all matrix elements.
void getInput()
{
do
{
cout<< "Rows = ";
cin>> row;
cout<< "Columns = ";
cin>> col;
if (row < 1 || col < 1 || row > 10 || col > 10)
{
cout<< "\nInvalid entries. Min size = 1x1 and max size = 10x10.\n";
}
}
while (row < 1 || col < 1 || row > 10 || col > 10);
cout<< "\nRemember to enter matrix entries from top-left to bottom-right.\n";
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
cout<< "Matrix Entry (" <<i<< "," << j << ") = ";
cin>> matrix[i][j];
} } }

// Output the full matrix to the screen in matrix representation.


void displayMatrix()
{
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
cout<<setw(8) << matrix[i][j];
}
cout<<endl;
} }

// Output the full resultant matrix to the screen in matrix representation.


void displayResultMatrix(int rows, int cols)
{
for (int i = 0; i< rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout<<setw(8) << result[i][j];
}
cout<<endl;
} }
// Perform scalar multiplication.
void scalarMultiplication()
{
cout<< "Enter a scalar to multiply the matrix by: ";
cin>> scalar;
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
matrix[i][j] *= scalar;
}
}
displayMatrix();
}

// Perform matrix addition.


void matrixAddition(Matrix m)
{
if (row == m.row && col == m.col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
matrix[i][j] += m(i,j);
}
}
displayMatrix();
}
else
{
cout<< "Cannot complete matrix addition with these matrices.\n";
}

}
// Transpose matrix.
void transpose()
{
int temp = row;
row = col;
col = temp;
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
result[i][j] = matrix[j][i];
}
}
displayResultMatrix(row, col);
}

// Perform matrix multiplication.


void matrixMultiplication(Matrix m)
{
if (col == m.row)
{
for (int i = 0; i< row; i++)
{
for (int j = 0; j < m.col; j++)
{
result[i][j]=0;
for (int k = 0; k <= col; k++)
{
result[i][j] += ( matrix[i][k] * matrix[k][j] );
}
}
}
displayResultMatrix(row, m.col);
}
else
{
cout<< "Cannot complete matrix multiplication with these matrices.\n";
}
}
// Finds the determinant of a matrix.

int determinantFinder()
{
if (col == row)
{
int determinant;
int determinantFinder(int n, int temp[10][10]);
int n, temp[10][10];
if (row == 1)
{
determinant = matrix[0][0];
}
else if (row == 2)
{
determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
}
else
{
for (int p = 0; p < row; p++)
{
int h = 0;
for (int i = 1; i < row; i ++)
{
int k=0;
for (int j = 0; j < col; j++)
{
if (j == p)
{
continue;
}
temp[h][k] = matrix[i][j];
k++;
}
if (k!=col-1)
{
cout << "Cannot find the determinant of this matrix. (Must be nxn)\n";
}
else
h++;
}
determinant = determinant + pow(-1, p) * matrix[0][p] * determinantFinder(n-1, temp);
}
}
return determinant;
cout << "The determinant of this matrix is: " << determinant << endl;
}
}

// Finds the determinant of a matrix.

int determinantFinder(int n, int matrix[10][10])


{
int det=0;
int h, k, temp[10][10];
if (row == 1)
{
det = matrix[0][0];
}
else if (row == 2)
{
det = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
}
else
{
for (int p = 0; p < n; p++)
{
h = 0;
for (int i = 1; i< n; i ++)
{
k=0;
for (int j = 0; j < n; j++)
{
if (j == p)
{
continue;
}
temp[h][k] = matrix[i][j];
k++;
}
h++;
}
det = det + ( pow(-1, p) * matrix[0][p] * determinantFinder(n-1, temp) );
}
}
return det;
cout<< "The determinant of this matrix is: " << det <<endl;
}
//Main function starts.

int main ()
{

cout << "\n\t\tWelcome to Matrix Calculator V2.0 By Suman KM!! :)\n\n"


<< "Start by making your first matrix!\n";
Matrix a;
a.getInput();
int choice;
string proceed, remakeMatrix;
cout << "Now that you have this matrix: \n";
a.displayMatrix();
do
{
cout << "\nWhat would you like to do?\n"
<< "Enter 1 for matrix scalar multiplication.\n"
<< "Enter 2 for matrix addition.\n"
<< "Enter 3 for matrix multiplication.\n"
<< "Enter 4 to transpose the matrix.\n"
<< "Enter 5 to find the matrix determinant.\n";
cout << "\n\nPlease enter your operation choice: ";
cin >> choice;
if (choice == 1)
{
a.scalarMultiplication();
}
else if (choice == 2)
{
cout << "\nThis will require a second matrix, please create one now.\n";
Matrix b;
b.getInput();
a.matrixAddition(b);
}
else if (choice == 3)
{
cout << "This will require a second matrix, please create one now.\n";
Matrix b;
b.getInput();
a.matrixMultiplication(b);
}
else if (choice == 4)
{
a.transpose();
}
else if (choice == 5)
{
a.determinantFinder();
}
OR
else if (choice == 5)
{
if (col != row)
{
cout<<"Cannot find the determinant of this matrix. (Must be nxn)\n";
}
else
{
n = row;
a.determinantFinder(n, matrix);
}

cout << "\nContinue? (Enter yes or no) \t";


cin >> proceed;
transform(proceed.begin(), proceed.end(), proceed.begin(), ::tolower);
if (proceed == "yes")
{
cout << "\nNow enter yes to create a new matrix or no to use current matrix.\n";
cin >> remakeMatrix;
transform(remakeMatrix.begin(), remakeMatrix.end(), remakeMatrix.begin(), ::tolower);
if (remakeMatrix == "yes")
{
a.getInput();
}
}
}
while(proceed == "yes");

system("PAUSE");
return EXIT_SUCCESS;
}

/*******************************************************************************
* Thank you for using my program *
*******************************************************************************/

You might also like