Saturday, 28 June 2014

C++ program to find inverse of a matrix


Here is a C++ program to find inverse of a matrix


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    float a[10][10];
    for(int i=0;i<6;i++)
        for(int j=3;j<6;j++)
            a[i][j]=0;
    a[0][3]=a[1][4]=a[2][5]=1;
    cout<<"Enter coefficients of variables in matrix form: "<<endl;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
           cin>>a[i][j];
    for(int i=1;i<3;i++)
        for(int j=5;j>=0;j--)
             a[i][j]=a[i][j]-a[0][j]*(a[i][0]/a[0][0]);
    for(int i=0;i<3;i+=2)
        for(int j=5;j>=0;j--)
            a[i][j]=a[i][j]-a[1][j]*(a[i][1]/a[1][1]);
    for(int i=0;i<2;i++)
        for(int j=5;j>=0;j--)
            a[i][j]=a[i][j]-a[2][j]*(a[i][2]/a[2][2]);
    cout<<"Inverse of matrix is : "<<endl;
    for(int i=0;i<3;i++)
    {
        for(int j=3;j<6;j++)
           cout<<a[i][j]/a[i][i]<<"   ";
        cout<<endl;
    }
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment