Write a program to read two 3x3 matrices, compute the sum, the product of the tw
ID: 3713119 • Letter: W
Question
Write a program to read two 3x3 matrices, compute the sum, the product of the two matrices. Then, compute the transpose, cofactor
matrix and the determinant of the resultant matrix. Then find the inverse of the first matrix then multiply it by the first column
of the second matrix to get third matrix Also tested using the matrices given in class. Also using the diagonal element of the
first matrix and the second matrix compute the standard of deviation of the six elements
In this program write a different method to perform the operations and use proper notation to pass the arguments and to access the
elements.
Use files for input and output. Make sure to use class and objects. Use constructor and over load constructor to initialize the
object.
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<math.h>
#include<stdlib.h>
using namespace std;
// Class Matrix definition
class Matrix
{
// Pointer type matrix to store data
float **matrix;
// For row and column
int row, col;
public:
// Prototype of member function
Matrix();
Matrix(int, int);
void readFile(string, Matrix&);
void displayMatrix();
Matrix addMatrix(Matrix, Matrix);
Matrix productMatrix(Matrix, Matrix);
Matrix transposeMatrix(Matrix);
void cofactorMatrix(Matrix);
float determinantMatrix(Matrix m1, int);
void adjointMatrix(Matrix, Matrix);
void inverseMatrix(Matrix, Matrix, int);
};// End of class
// Default constructor
Matrix::Matrix()
{
// Allocate default size
row = col = 3;
// Dynamically allocates memory to row
matrix = new float*[row];
// Loops till number of rows
for(int r = 0; r < row; r++)
// Dynamically allocates memory to each column
matrix[r] = new float[col];
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
// Assigns zero
matrix[r][c] = 0;
}// End of for loop
}// End of constructor
Matrix::Matrix(int r, int c)
{
// Allocate row and column size
row = r;
col = c;
// Dynamically allocates memory to row
matrix = new float*[row];
// Loops till number of rows
for(int r = 0; r < row; r++)
// Dynamically allocates memory to each column
matrix[r] = new float[col];
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
// Assigns zero
matrix[r][c] = 0;
}// End of for loop
}// End of constructor
// Function to display matrix
void Matrix::displayMatrix()
{
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
// Displays matrix data with two decimal places
cout<<fixed<<setw(8)<<setprecision(2)<<matrix[r][c]<<" ";
cout<<endl;
}// End of for loop
}// End of function
// Function to read the contents of file and stores it in matrix
void Matrix::readFile(string fileName, Matrix &m1)
{
// ifstream object declared
ifstream rFile;
// Opens the file for reading
rFile.open(fileName.c_str());
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file! "<<fileName;
return;
}// End of if condition
// Reads number of rows and stores it in data member
rFile>>row;
// Reads number of columns and stores it in data member
rFile>>col;
// Allocates memory using parameterized constructor
Matrix temp(row, col);
// Assigns to Matrix m1
m1 = temp;
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < row; c++)
// Reads data and stores it in matrix
rFile>>m1.matrix[r][c];
}// End of for loop
}// End of function
// Function to add matrix
Matrix Matrix::addMatrix(Matrix m1, Matrix m2)
{
// Creates an object using parameterized constructor
Matrix result(m1.row, m1.col);
// Checks if matrix m1 row and matrix m2 row is same and matrix m1 column is equals to matrix m2 column is same
if (m1.row == m2.row && m1.col == m2.col)
{
// Loops till number of rows
for (int row = 0; row < m1.row; row++)
{
// Loops till number of columns
for (int col = 0; col < m1.col; col++)
// Adding process
result.matrix[row][col] = m1.matrix[row][col] + m2.matrix[row][col];
}// End of inner for loop
}// End of if condition
// Otherwise invalid
else
cout<<"addition not Possible required rows and columns of both matrix equals"<<endl;
// Returns result matrix
return result;
}// End of function
// Matrix to calculate product
Matrix Matrix::productMatrix(Matrix m1, Matrix m2)
{
// Creates an object using parameterized constructor
Matrix result(m1.row, m2.col);
// Checks if matrix m1 column and matrix m2 row are same
if (m1.col == m2.row)
{
// Loops till number of rows
for (int row = 0; row < m1.row; row++)
{
// Loops till number of columns
for (int colIndex = 0; colIndex <m2.col; colIndex++)
{
// Iterate rows index of second matrix
for (int col = 0; col <m2.row; col++)
// Product operation
result.matrix[row][colIndex] = result.matrix[row][colIndex] + m1.matrix[row][col] * m2.matrix[col][colIndex];
}// End of inner for loop
}// End of outer for loop
}// End of if condition
// Otherwise invalid
else
cout<<"addition not Possible required rows and columns of both matrix equals"<<endl;
// Returns the product matrix
return result;
}// End of function
// Function to transpose matrix
Matrix Matrix::transposeMatrix(Matrix m1)
{
// Creates an object using parameterized constructor
Matrix transpose(m1.row, m1.col);
// Loops till number of rows
for(int r = 0; r < m1.row; r++)
{
// Loops till number of rows
for(int c = 0; c < m1.col; c++)
transpose.matrix[r][c] = m1.matrix[c][r];
}// End of for loop
// Returns transpose matrix
return transpose;
}// End of function
// Function to calculate cofactor
void Matrix::cofactorMatrix(Matrix m1)
{
// Creates an object using parameterized constructor
Matrix temp(m1.row, m1.col);
// Creates an object using parameterized constructor
Matrix fac(m1.row, m1.col);
int p, q, m, n, i, j;
// Loops till number of rows
for (q = 0;q < m1.row; q++)
{
// Loops till number of rows
for (p = 0;p < m1.row; p++)
{
m = 0;
n = 0;
// Loops till number of rows
for (i = 0;i < m1.row; i++)
{
// Loops till number of rows
for (j = 0;j < m1.row; j++)
{
if (i != q && j != p)
{
temp.matrix[m][n] = m1.matrix[i][j];
if (n < (m1.row - 2))
n++;
else
{
n = 0;
m++;
}// End of else
}// End of if condition
}// End of for loop
}// End of for loop
fac.matrix[q][p] = pow(-1, q + p) * determinantMatrix(temp, m1.row - 1);
}// End of for loop
}// End of for loop
// Calls the function to find inverse
inverseMatrix(m1, fac, m1.row);
}// End of function
// Recursive function for finding determinant of matrix. n is current dimension of A[][].
float Matrix::determinantMatrix(Matrix m1, int k)
{
float s = 1, det = 0;
// Creates an object using parameterized constructor
Matrix temp(m1.row, m1.col);
int i, j, m, n, c;
if (k == 1)
return (m1.matrix[0][0]);
else
{
det = 0;
// Loops till number of rows
for (c = 0; c < k; c++)
{
m = 0;
n = 0;
// Loops till number of rows
for (i = 0;i < k; i++)
{
// Loops till number of rows
for (j = 0 ;j < k; j++)
{
// Assigns zero to each cell
temp.matrix[i][j] = 0;
// Checks if is not zero and j is not zero
if (i != 0 && j != c)
{
// Assigns m1 matrix cell data to temp matrix data
temp.matrix[m][n] = m1.matrix[i][j];
if (n < (k - 2))
n++;
else
{
n = 0;
m++;
}// End of else
}// End of if condition
}// End of for loop
}// End of for loop
det = det + s * (m1.matrix[0][c] * determinantMatrix(temp, k - 1));
s = -1 * s;
}// End of for loop
}// End of else
// Returns determinant
return det;
}// End of function
// Function to calculate and store inverse, returns false if matrix is singular
void Matrix::inverseMatrix(Matrix m1, Matrix m2, int r)
{
int i, j;
// Creates an object using parameterized constructor
Matrix temp(m1.row, m1.col);
// Creates an object using parameterized constructor
Matrix inverse(m1.row, m1.col);
float d;
// Calls the function to find transpose of matrix
temp = transposeMatrix(m2);
// Calls the function find determinant
d = determinantMatrix(m1, r);
// Loops till number of rows
for (i = 0;i < r; i++)
{
// Loops till number of columns
for (j = 0;j < r; j++)
// Divides each cell value of the matrix by determinant
inverse.matrix[i][j] = temp.matrix[i][j] / d;
}// End of for loop
cout<<" The inverse of matrix is : ";
// Loops till number of rows
for (i = 0;i < r; i++)
{
// Loops till number of columns
for (j = 0;j < r; j++)
// Displays matrix contents with two decimal places
cout<<fixed<<setw(8)<<setprecision(2)<<inverse.matrix[i][j];
cout<<endl;
}// End of for loop
}// End of function
int main()
{
// Creates an object using default constructor
Matrix m1, m2, addM, mulM, transposeM1, transposeM2, inverseM;
m1.readFile("matrix1.txt", m1);
m2.readFile("matrix2.txt", m2);
cout<<" Matrix - 1 ";
m1.displayMatrix();
cout<<" Matrix - 2 ";
m2.displayMatrix();
addM = m1.addMatrix(m1, m2);
cout<<" After addition ";
addM.displayMatrix();
mulM = m1.productMatrix(m1, m2);
cout<<" After addition ";
mulM.displayMatrix();
transposeM1 = m1.transposeMatrix(m1);
cout<<" After transpose matrix 1 ";
transposeM1.displayMatrix();
transposeM2 = m2.transposeMatrix(m2);
cout<<" After transpose matrix 2 ";
transposeM2.displayMatrix();
m1.cofactorMatrix(m1);
}
Sample output:
Matrix - 1
1.00 2.00 4.00
5.00 4.00 2.00
6.00 4.00 5.00
Matrix - 2
3.00 1.00 4.00
5.00 2.00 3.00
2.00 1.00 4.00
After addition
4.00 3.00 8.00
10.00 6.00 5.00
8.00 5.00 9.00
After addition
21.00 9.00 26.00
39.00 15.00 40.00
48.00 19.00 56.00
After transpose matrix 1
1.00 5.00 6.00
2.00 4.00 4.00
4.00 2.00 5.00
After transpose matrix 2
3.00 5.00 2.00
1.00 2.00 1.00
4.00 3.00 4.00
The inverse of matrix is :
-0.40 -0.20 0.40
0.43 0.63 -0.60
0.13 -0.27 0.20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.