12 June, 2013

C++ Program To Find & Print Inverse Of A Matrix.

This Post Contains A C++ Program To Find & Print Inverse Of A Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-Loop' & 'Nested-Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Find & Print Inverse Of A Matrix.
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[3][3], b[3][3]; c[10];
float d[3][3], det;
int i, j, l, m, t, k;

clrscr();

/* Accepting Values Of 2D Array [ Matrix ] */

       cout  <<  "  Enter  9  Values for 2D Array  :  ";

       for  (  i  =  0;  i  <  3;  i++  )

       {

                for  (  j  =  0;  j  <  3;  j++  )

                {

                         cin  >>  a [ i ][ j ];

                }

       }

/* Source Code For Computing Inverse Of A Matrix */

for(l=0; l<3; l++)

{

   for(m=0; m<3; m++)

   {

      t=0;

      for(i=0; i<3; i++)

      {

         for(j=0; j<3; j++)

         {

            if(i != l && j != m)

            {

               c[t]=a[i][j]; 

               t++;

            }

         }

      }

      b[l][m]=( c[0]*c[3] )-( c[1]*c[2] );

   }

}

   cout  <<  " \n Matrix A : ";

   for(i=0; i<3; i++)

   {

      cout  <<  "\n";

      for(j=0; j<3; j++)

      {

         cout  <<  a[i][j]<<"\t";

      }

   }

   cout  <<  " \n Adjoint Matrix : ";

   for(i=0; i<3; i++)

   {

      cout  <<  "\n";

      for(j=0; j<3; j++)

      {

         cout  <<  b[i][j]  <<  "\t";

      }

   }

   for(i=0; i<3; i++)

   {

      for(j=0; j<3; j++)

      {

         if(i+j==1 || i+j==3)

            a[i][j]=a[i]][j]*-l;

      }

   }

   cout  <<  " \n Values Of Matrix A : ";

   for(i=0; i<3; i++)

   {

      cout  <<  "\n";

      for(j=0; j<3; j++)

      {

         cout  <<  a[i][j]  <<  "\t";

      }

   }

   for(i=0; i<3; i++)

   {

      det=0;

      for(j=0; j<3; j++)

      {

         det=det+a[i][j]*b[i][j];

      }

   }

cout  <<  " \n Determinant : "  <<  det;

if(det != 0)

{

      for(i=0; i<3; i++)

      {

         for(j=0; j<3; j++)

         {

            d[i][j]=b[i][j]/det;

         }

      }

/* Printing The Output Onto The Screen/Console */

      cout  <<  " \n Inverse Of Matrix A : ";

      for(i=0; i<3; i++)

      {

         cout  <<  "\n";

         for(j=0; j<3; j++)

         {

            cout  <<  d[i][j]  <<  "\t";

         }

      }     

}

else

{

   cout  <<  " \n Inverse Of Matrix Is Not Possible";

}

getch();

}

/* End Of Main Program */




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive