PROGRAM :
/*To write a C/C++ program to find the inverse of a matrix using Gauss Jordan Method.*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, j, k, n;
float a[10][10] = { 0 }, d;
cout << "No of rows ";
cin >> n;
cout << "\nEnter the elements of the matrix row-wise:\n";
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
cin >> a[i][j];
for (i = 1; i <= n; i++)
for (j = 1; j <= 2 * n; j++)
if (j == (i + n))
a[i][j] = 1;
for (i = n; i > 1; i--)
{
if (a[i - 1][1] < a[i][1])
for (j = 1; j <= n * 2; j++)
{
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
}
}
cout << "pivoted output: " << endl;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
if (j != i)
{
d = a[j][i] / a[i][i];
for (k = 1; k <= n * 2; k++)
a[j][k] -= a[i][k] * d;
}
}
for (i = 1; i <= n; i++)
{
d = a[i][i];
for (j = 1; j <= n * 2; j++)
a[i][j] = a[i][j] / d;
}
cout <<"\nInverse of the matrix is as follows :\n";
for (i = 1; i <= n; i++)
{
for (j = n + 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
OUTPUT :
No of rows 3
Enter the elements of the matrix row-wise:
2 2 3
2 1 1
1 3 5
pivoted output:
2 2 3 1 0 0
2 1 1 0 1 0
1 3 5 0 0 1
Inverse of the matrix is as follows :
2 -1 -1
-9 7 4
5 -4 -2
/*To write a C/C++ program to find the inverse of a matrix using Gauss Jordan Method.*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, j, k, n;
float a[10][10] = { 0 }, d;
cout << "No of rows ";
cin >> n;
cout << "\nEnter the elements of the matrix row-wise:\n";
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
cin >> a[i][j];
for (i = 1; i <= n; i++)
for (j = 1; j <= 2 * n; j++)
if (j == (i + n))
a[i][j] = 1;
for (i = n; i > 1; i--)
{
if (a[i - 1][1] < a[i][1])
for (j = 1; j <= n * 2; j++)
{
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
}
}
cout << "pivoted output: " << endl;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
if (j != i)
{
d = a[j][i] / a[i][i];
for (k = 1; k <= n * 2; k++)
a[j][k] -= a[i][k] * d;
}
}
for (i = 1; i <= n; i++)
{
d = a[i][i];
for (j = 1; j <= n * 2; j++)
a[i][j] = a[i][j] / d;
}
cout <<"\nInverse of the matrix is as follows :\n";
for (i = 1; i <= n; i++)
{
for (j = n + 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
OUTPUT :
No of rows 3
Enter the elements of the matrix row-wise:
2 2 3
2 1 1
1 3 5
pivoted output:
2 2 3 1 0 0
2 1 1 0 1 0
1 3 5 0 0 1
Inverse of the matrix is as follows :
2 -1 -1
-9 7 4
5 -4 -2