Most Commonly Asked Programs In C++ Language

Most Commonly Asked Programs In C++ Language


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

Posted: 11 Jun 2013 09:38 PM PDT

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>
/* 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 */

C++ Program For Sorting Of A Matrix By Rows.

Posted: 11 Jun 2013 09:31 PM PDT

This Post Contains A C++ Program For Sorting Of A Matrix By Rows 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-Loops', 'If...else' & '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 For Sorting Of A Matrix By Rows.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int a[10][10], i, j, r, c, t;
clrscr();

      /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }
/* Source Code For Sorting Of A Matrix By Rows */
   for(i=0; i<r; i++)
   {
      for(j=0; j<c; j++)
      {
         for(k=j+1; k<q; k++)
         {
            if(a[i][j]>a[i][k])
            {
               t=a[i][j];
               a[i][j]=a[i][k];
               a[i][k]=t;
            }
         }
      }
   }

/* Printing The Output Onto The Screen/Console */   
   cout<<" \n After Sorting Elements Of Matrix A : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
   }
getch();
}
/* End Of Main Program */


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
9  8  7  6  5  4  3  2  1

After Sorting Elements Of Matrix A :
7  8  9
4  5  6
1  2  3

C++ Program For Sorting All Elements Of A Matrix.

Posted: 11 Jun 2013 09:21 PM PDT

This Post Contains A C++ Program For Sorting All Elements 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', 'If...else' & '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 For Sorting All Elements Of A Matrix.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int a[10][10], b[30], i, j, r, c, s, t;
clrscr();

/* Asking For The Input From User */
cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
cin  >>  r  >>  c ;
      
//  Accepting Values Of 2D Array [ Matrix ]
cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
t=0;
for  (  i  =  0;  i  <  r;  i++  )
{
      for  (  j  =  0;  j  <  c;  j++  )
     {
            cin  >>  a [ i ][ j ];
            b[t]=a[i][j];
            t++
     }
}
/* Source Code For Sorting All Elements Of A Matrix */
for(i=0; i<t; i++)
{
      for(j=i+1; j<t; j++)
      {
         if(b[i]>b[j])
           s=b[i];
           b[i]=b[j];
           b[j]=s;
      }
}
  
cout<<" \n Elements Of Matrix A : ";
for(i=0; i<r; i++)
{
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
}

/* Assigning Sorted Elements */
t=0;
for(i=0; i<r; i++)
{
      for(j=0; j<c; j++)
      {
         a[i][j]=b[t];
         t++;
      }
}
/* Printing The Output Onto The Screen/Console */
cout<<" \n After Sorting Elements Of Matrix A : ";
for(i=0; i<r; i++)
{
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
}

getch();
}
/* End Of Main Program */


Output :
Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  4  9
2  7  8
5  6  3

After Sorting Elements Of Matrix A :
1  2  3
4  5  6
7  8  9


Post a Comment