Pages

Subscribe:

Blogger templates

Wednesday, April 18, 2012

Matrix ( Add, Sub, Multiply ,Transpose ) through a FUNCTIONS.


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
int m,p,n,q,i,j;
//ADDITION
void add(int a[10][10],int b[][10])
{
         clrscr();

         int c[10][10];
         cout<<"\nORIGINAL MATRIX 1:\t\t\n";
         for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                   cout<<" "<<a[i][j];
                   }
                   cout<<"\n";
          }

         cout<<"\nORIGINAL MATRIX 2:\t\t\n";
         for(i=0;i<p;i++)
          {
                   for(j=0;j<q;j++)
                   {
                            cout<<" "<<b[i][j];
                   }
          cout<<"\n";
          }
for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                    {
                             c[i][j]=a[i][j]+b[i][j];
                            cout<<"\t"<<c[i][j];
                   }
                   cout<<"\n";
          }
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}

//SUBTRACTION
void sub(int a[][10],int b[][10])
{
         clrscr();
         int c[10][10];
         cout<<"\nORIGINAL MATRIX 1:\t\t\n";
         for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                            cout<<" "<<a[i][j];
                   }
                   cout<<"\n";
}

cout<<"\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
{
          for(j=0;j<q;j++)
          {
          cout<<" "<<b[i][j];
          }
cout<<"\n";
}
for(i=0;i<m;i++)
{
          for(j=0;j<n;j++)
           {
                   c[i][j]=a[i][j]-b[i][j];
                   cout<<"\t"<<c[i][j];
          }
cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
//MULTIPLY
void multiply(int a[][10],int b[][10])
{
         clrscr();
         int c[10][10];
         cout<<"\nORIGINAL MATRIX 1:\t\t\n";
         for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                   cout<<" "<<a[i][j];
                   }
cout<<"\n";
}
cout<<"\n\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
 {
          for(j=0;j<q;j++)
           {
                   cout<<" "<<b[i][j];
           }
          cout<<"\n";
}
cout<<"\nMatrix Multiplication:\n";
for(i=0;i<m;i++)
{
          for(j=0;j<q;j++)
          {
                    if(n==3||p==3)
                    {
                            c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j];
                    }
                   else
                   {
                            c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j];
                   }
           }

}

for(i=0;i<m;i++)
{
          for(j=0;j<q;j++)
         {
                   cout<<" "<<c[i][j];
          }
          cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
//TRANSPOSE
void trans(int a[][10],int b[][10])
{
         clrscr();
         cout<<"\nORIGINAL MATRIX 1:\t\t\n";
         for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                            cout<<" "<<a[i][j];
                   }
          cout<<"\n";
          }

         cout<<"\nTranspose of 1st Matrix:\n";
         for(i=0;i<n;i++)
          {
                   for(j=0;j<m;j++)
                    {
                            cout<<" "<<a[j][i];
                    }
          cout<<"\n";
          }
          cout<<"\n\nORIGINAL MATRIX 2:\t\t\n";
          for(i=0;i<p;i++)
          {
                   for(j=0;j<q;j++)
                   {
                            cout<<" "<<b[i][j];
                   }
                    cout<<"\n";
                  }
                   cout<<"\nTranspose of 2nd Matrix:\n";
                  for(i=0;i<q;i++)
                   {
                            for(j=0;j<p;j++)
                            {
                                      cout<<" "<<b[j][i];
                             }
                              cout<<"\n";
                   }
          cout<<"\n\nPress any key to go to main menu";
          getch();
          clrscr();

}
void main()
{
         clrscr();
         int a[10][10],b[10][10];

         char ch;
         cout<<"\nEnter the Size of MAtrix 1:\n";
         cin>>m>>n;
         cout<<"\nSize Entered: "<<m<<"X"<<n;

         cout<<"\nEnter the size of Matrix 2:\n";
         cin>>p>>q;
         cout<<"\nSize Entered: "<<p<<"X"<<q;
         cout<<"\n\nPress any key to Continue:";
         getch();

         clrscr();
         do{
                   cout<<"\n---------------------------------------------";
                   cout<<"\n\t\t\tMENU\t\t\n";
                   cout<<"\n\t1.ADDITION";
                   cout<<"\n\t2.SUBTRACTION";
                   cout<<"\n\t3.MULTIPLY";
                   cout<<"\n\t4.TRANSPOSE";
                   cout<<"\n\t5.EXIT";
                   cout<<"\n----------------------------------------------------------";
                   cout<<"\nEnter your choice:";
                   cin>>ch;
                   clrscr();

                                                                                                                               //Conditions
                   if(ch=='1')
                   {
                            if(m==p &&n==q)
                           {
                                      cout<<"\nMatrix are Comparable";
                                     cout<<"\nEnter MATRIX 1:\n";
                                      for(i=0;i<m;i++)
                                      {
                                              for(j=0;j<n;j++)
                                              {
                                                       cin>>a[i][j];
                                              }
                                      }
                             cout<<"\nEnter MATRIX 2:\n";
                             for(i=0;i<p;i++)
                             {
                                     for(j=0;j<n;j++)
                                     {
                                              cin>>b[i][j];
                                     }
                    }
add(a,b);
}


else

{
cout<<"\nSORRY! MATRICES ARE NOT COMPARABLE";
cout<<"\n\nPlease try Again";
}
}


else if(ch=='2')

{
if(m==p &&n==q)
{
cout<<"\nMatrix are Comparable";
cout<<"\nEnter MATRIX 1\n:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
sub(a,b);
}
else
{
cout<<"\nSORRY! MATRICES ARE NOT COMPARABLE";
cout<<"\n\nPlease try Again";
}


}
else if(ch=='3')
{
if(n==p)
{
cout<<"\nMatrix are Multiplicable";
cout<<"\nEnter the Matrix:";
cout<<"\nEnter MATRIX 1\n:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
multiply(a,b);
}
else {
cout<<"\nSorry! Matrices are not Multiplicable! TRY AGAIN";
}
}
if(ch=='4')
{
cout<<"\nEnter the Matrix:";
cout<<"\nEnter MATRIX 1:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
trans(a,b);
}



else if(ch=='5')
{
cout<<"\n\nThanks for Watching :-)";
cout<<"\n\n\t\tABORTING!\n\t\t";
for(i=0;i<9;i++)
{
delay(500);
cout<<".";
}

exit(0);
}
}while(1);

getch();
}