facebook like button

05 September, 2013

Vector & Matrix Algebra


#include<iostream.h>  
#include<conio.h>
#include <cstdlib>
#include <math.h>  
class vector  
 {   
 public:  
 int x,y,z;  
 void read()  
 {  
 cout<<"\n\nEnter the magnitude of i : ";  
 cin>>x;  
 cout<<"\n\nEnter the magnitude of j : ";  
 cin>>y;  
 cout<<"\n\nEnter the magnitude of k : ";  
 cin>>z;  
 }  
 vector operator -()  
 {  
 x=-x;  
 y=-y;  
 z=-z;  
 }  
 vector operator +(vector b)  
 {  
 vector c;  
 c.x=x+b.x;  
 c.y=y+b.y;  
 c.z=z+b.z;  
 return c;  
 }  
 vector operator *(vector b)  
 {  
 int prod;  
 vector c;  
 c.x=x*b.x;  
 c.y=y*b.y;  
 c.z=z*b.z;  
 prod=c.x+c.y+c.z;
 cout<<"\nInner Product = ";  
 cout<<prod;
 vector d;  
 d.x=(y*b.z)-(z*b.y);  
 d.y=(x*b.z)-(z*b.x);  
 d.z=(x*b.y)-(y*b.x);  
 return d;  
 }  
 void display()  
 {  
 cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";  
 }  
 }; 

using namespace std;

void vecmat_mult()
{
    int mat[10][10];
    int vec[3];
    int out[10];
    int c,d,m,n,i,j;
cout<<"Enter the number of rows and columns of matrix"<<endl;
  cin>>m;
  cin>>n;
  cout<<"Enter the elements of matrix"<<endl;
  for ( c = 0 ; c < m ; c++ )
    {
 for ( d = 0 ; d < n ; d++ )
  {
 cout<<"Enter elements mat"<<c+1<<d+1<<" ";
 cin>>mat[c][d];
  }
    }

    cout<<"Enter Vector Component"<<endl;
    
  for(i=0;i<3;i++)
{
cin>>vec[i];
}

 
    for (i = 0; i < m; i++ )
{
out[i] = 0;
      for ( int j = 0; j < n; j++ )
  {
 out[i] += mat[i][j] * vec[j];
  }
      }
cout<<"Result Product is\n\n";
    for (i=0;i<m;i++)
{
cout<<out[i]<<"  "<<endl;
    }
}

void matrixmult()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    cout<<"Enter number of rows and columns of first matrix\n";
    cin>>r1>>c1;
    cout<<"Enter number of rows and columns of second matrix\n";
    cin>>r2>>c2;
    if(r2==c1)
    {
        cout<<"Enter rows and columns of First matrix \n";
        for(i=0;i<r1;i++)
            for(j=0;j<c1;j++)
                cin>>m1[i][j];
        cout<<"First Matrix is :\n";
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                cout<<m1[i][j]<<"\t";
            cout<<"\n";
        }
        cout<<"Enter rows and columns of Second matrix \n";
        for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
                cin>>m2[i][j];
        cout<<"Second Matrix is:\n";
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                cout<<m2[i][j]<<"\t";
            cout<<"\n";
        }
        cout<<"Multiplication of the Matrices:\n";
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j];
                cout<<mult[i][j]<<"\t";
            }
            cout<<"\n";
        }
    }
    else
    {
        cout<<"Matrix multiplication cannot be done";
    }
}
void matrixtranspose()
{
int a[10][10], trans[10][10], r, c, i, j;
cout<<"Enter rows and column of matrix: "<<endl;
cin>>r>>c;
cout<<"\nEnter elements of matrix:\n";
for(i=0; i<r; ++i)
{
 for(j=0; j<c; ++j)
  {
 cout<<"Enter elements a"<<i+1<<j+1<<" ";
 cin>>a[i][j]; 
}
}
 /*Displaying the matrix a[][] */
 cout<<"\nEntered Matrix: \n"; 
 for(i=0;i<r; ++i)
  {
 for(j=0; j<c; ++j)
     
 cout<<a[i][j]<<"\t";
 if(j==c-1)
 cout<<"\n\n"; 
    }
  }
 
/* Finding transpose of matrix a[]
[] and storing it in array trans[][]. */ 

for(i=0; i<r; ++i)
{
 for(j=0;j<c; ++j)
  { 
    trans[j][i]=a[i][j];
       }
    }
/* Displaying the transpose,i.e, Displaying array trans[][].*/ 

cout<<"\nTranspose of Matrix:\n"; 
for(i=0; i<c; ++i)
{
 for(j=0; j<r; ++j)
  {
 printf("%d ",trans[i][j]); if(j==r-1) printf("\n\n");
  }
}
}

int main()  
 {  
 vector v1,v2,v3;  
 int choice,cont;  
 do  
 {  
 cout<<"\n\tVECTORS & MaTRIX Algebra\n\n1.Vector Addition\n\n2.Vector Product\n\n3.Product Matrix-Vector\n\n4.Product Matrix-Matrix\n\n5.Transpose Matrix\n\n6.Exit";  
 cout<<"\n\nEnter your choice : ";  
 cin>>choice;  
 switch(choice)  
 {  

 case 1 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1+v2;  
            cout<<"\n\nThe Sum of Vectors : ";  
            v3.display();  
            break;  
 case 2 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1*v2;  
            cout<<"\n\nCross Product = ";  
            v3.display();
            break;  
 case 3 : vecmat_mult();
   break; 

 case 4 : matrixmult();
     break;

 case 5 : matrixtranspose();
     break;

 case 6 : exit(0);
 default : cout<<"\n\nInvalid Choice";  
 }  
 cout<<"\n\n\nDo you want to continue?(1-YES,0-NO)\n";  
 cin>>cont;  
 }while(cont==1);  
 return 0;
 }