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

please have the code typed (not on paper) The inverse of a square matrix A is th

ID: 3807587 • Letter: P

Question

please have the code typed (not on paper)

The inverse of a square matrix A is the matrix A^-1, such that AA^-1 = I. where I, is the Identity Matrix. A square matrix A has an inverse iff (if and only if) the determinant |A|notequalto 0. The matrix having an inverse is called a nonsingular, or invertible matrix. A = [a_11 a_12 a_13 a_21 a_22 a_23 a_31 a_32 a_33] (1) The inverse of the above matrix is given by: A^-1 = 1/det(A) [|a_22 a_23 a_32 a_33| |a_13 a_12 a_33 a_32| |a_12 a_13 a_22 a_23| |a_23 a_21 a_33 a_31| a_11 a_13 a_31 a_33| |a_13 a_11 a_23 a_23| |a_21 a_22 a_31 a_32| |a_12 a_11 a_32 a_31| |a_11 a_12 a_21 a_22| (2) b. Using equations 1 and 2, develop a C++ program using Two Dimensional arrays and pointers in a function named matrix inverse_ptr() to return the inverse of a nonsingular matrix. The program should have the capability of identifying a singular or non-singular matrix. Write all the functions using pointers only. a. Draw a detailed flow chart on how this program Ls designed, indicating the inputs, outputs and the algorithm you use. The flow chart should be included as a document file along with your program listing (source code).

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
#define N 4
//Function to calculate co-factor
void getCofactor(int A[N][N], int T[N][N], int p, int q, int n)
{
int i = 0, j = 0;

// Looping for each element of the matrix
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
// Copying into temporary matrix only those element
// which are not in given row and column
if (row != p && col != q)
{
T[i][j++] = A[row][col];

// Row is filled, so increase row index and reset col index
if (j == n - 1)
{
j = 0;
i++;
}//End of inner if
}//End of outer if
}//End of loop for column
}//End of loop for row
}//End of function

//Function to calculate and return determinant of a matrix
int determinant(int A[N][N], int n)
{
// Initialize result
int D = 0;

// Base case : if matrix contains single element
if (n == 1)
return A[0][0];

// To store cofactors
int T[N][N];

// To store sign multiplier
int sign = 1;

// Iterate for each element of first row
for (int f = 0; f < n; f++)
{
// Getting Cofactor of A[0][f]
getCofactor(A, T, 0, f, n);
D += sign * A[0][f] * determinant(T, n - 1);

// Terms are to be added with alternate sign
sign = -sign;
}//End of for loop

return D;
}//End of function

// Function to get adjoint matrix
void adjoint(int A[N][N],int adj[N][N])
{
if (N == 1)
{
adj[0][0] = 1;
return;
}//End of if

// Temp is used to store cofactors of A[][]
int sign = 1, T[N][N];
//Loops till number of rows
for (int i=0; i<N; i++)
{
//Loops till number of columns
for (int j=0; j<N; j++)
{
// Get cofactor of A[i][j]
getCofactor(A, T, i, j, N);

// Sign of adj[j][i] positive if sum of row and column indexes is even.
sign = ((i+j)%2==0)? 1: -1;

// Interchanging rows and columns to get the transpose of the cofactor matrix
adj[j][i] = (sign)*(determinant(T, N-1));
}//End of loop for column
}//End of loop for row
}//End of function

//Function for inverse operation
bool inverse(int A[N][N], float inverse[N][N])
{
// Find determinant of A[][]
int det = determinant(A, N);
if (det == 0)
{
cout << "Singular matrix, can't find its inverse";
return false;
}//End of if

// Find adjoint
int adj[N][N];
adjoint(A, adj);

// Find Inverse using formula "inverse(A) = adj(A)/det(A)"
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
inverse[i][j] = adj[i][j]/float(det);

return true;
}//End of function

// Generic function to display the matrix.
template<class T>
void show(T A[N][N])
{
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
cout <<setprecision(2)<<setw(8)<< A[i][j];
cout << endl;
}//End of for
}//End of function

// Main function
int main()
{
//Creates a matrix
int A[N][N] = { {12, 10, -2, 11},
{22, 0, 4, -3},
{20, 7, -5, 0},
{30, 4, 9, -4}};

// To store adjoint matrix
int adj[N][N];

// To store inverse matrix
float inv[N][N];

cout << "Input matrix is : ";
show(A);

cout << " The Adjoint is : ";
adjoint(A, adj);
show(adj);

cout << " The Inverse is : ";
if (inverse(A, inv))
show(inv);

return 0;
}//End of main

Output:

Input matrix is :
12 10 -2 11
22 0 4 -3
20 7 -5 0
30 4 9 -4

The Adjoint is :
-137 -1057 -42 416
230 3710 -994 -2150
-226 966 1092 -1346
-1306 -2044 1148 1256

The Inverse is :
0.01 0.08 0.0032 -0.031
-0.017 -0.28 0.075 0.16
0.017 -0.073 -0.082 0.1
0.099 0.15 -0.087 -0.095