Friday 5 August 2011

Matrix Program for Addition,Substraction,Multiplication and Transpose

#include<iostream>
#include<cstdlib>
using namespace std;
class matrices
{
        int **matrix;
        int m,n;
        public:
        void setmatrix();
        void addition();
        void subtraction();
        void multiplication();
        void transpose();
        void display();
};
void matrices::setmatrix()
{
        cout<<"Enter the size of the matrix:";
        cin>>m>>n;
        matrix=new int*[m];
        for(int i=0;i<m;i++)
                matrix[i]=new int[n];
        cout<<"Enter the elements of the matrix:";
        for(int i=0;i<m;i++)
                for(int j=0;j<n;j++)
                        cin>>matrix[i][j];
}
void matrices::display()
{

        for(int i=0;i<m;i++)
        {
                for(int j=0;j<n;j++)
                        cout<<matrix[i][j]<<"\t";
                cout<<"\n";
        }
}
void matrices::addition()
{
        matrices ob1,ob2,result;
        cout<<"\nFirst Matrix:\n";
        ob1.setmatrix();
        cout<<"\nSecond Matrix:\n";
        ob2.setmatrix();
        if(ob1.m!=ob2.m||ob1.n!=ob2.n)
                cout<<"Addition cannot be performed.";
        else
        {
                result.matrix=new int*[ob1.m];
                result.m=ob1.m;
                result.n=ob1.n;
                for(int i=0;i<ob1.m;i++)
                        result.matrix[i]=new int[ob1.n];
                for(int i=0;i<ob1.m;i++)
                        for(int j=0;j<ob1.n;j++)
                                result.matrix[i][j]=ob1.matrix[i][j]+ob2.matrix[i][j];
                cout<<"Matrices sum is:\n";
                result.display();
        }
}
void matrices::subtraction()
{
         matrices ob1,ob2,result;
        cout<<"\nFirst Matrix:\n";
        ob1.setmatrix();
        cout<<"\nSecond Matrix:\n";
        ob2.setmatrix();
        if(ob1.m!=ob2.m||ob1.n!=ob2.n)
                cout<<"Subtraction cannot be performed.";
        else
        {
                result.matrix=new int*[ob1.m];
                result.m=ob1.m;
                result.n=ob1.n;
                for(int i=0;i<ob1.m;i++)
                        result.matrix[i]=new int[ob1.n];
                for(int i=0;i<ob1.m;i++)
                        for(int j=0;j<ob1.n;j++)
                                result.matrix[i][j]=ob1.matrix[i][j]-ob2.matrix[i][j];
                cout<<"Matrices difference is:\n";
                result.display();
        }
}
void matrices::multiplication()
{
        matrices ob1,ob2,result;
        int k,sum;
        cout<<"\nFirst Matrix:\n";
        ob1.setmatrix();
        cout<<"\nSecond Matrix:\n";
        ob2.setmatrix();
        if(ob1.n!=ob2.m)
                cout<<"Multiplication cannot be performed.";
        else
        {
                result.matrix=new int*[ob1.m];
                result.m=ob1.m;
                result.n=ob2.n;
                for(int i=0;i<ob1.m;i++)
                        result.matrix[i]=new int[ob1.n];
                for(int i=0;i<ob1.m;i++)
                        {
                                for(int j=0;j<ob2.n;j++)
                                {
                                        sum=0;
                                        k=0;
                                        while(k<ob1.n)
                                        {
                                                sum=sum+ob1.matrix[i][k]*ob2.matrix[k][j];
                                                k++;
                                        }
                                        result.matrix[i][j]=sum;
                                }
                        }
                cout<<"Matrices product is:\n";
                result.display();
        }
}
void matrices::transpose()
{
        matrices ob1,ob2;
        ob1.setmatrix();
        ob2.matrix=new int*[ob1.n];
        for(int i=0;i<ob1.n;i++)
                ob2.matrix[i]=new int[ob1.m];
        for(int i=0;i<ob1.m;i++)
                for(int j=0;j<ob1.n;j++)
                        ob2.matrix[j][i]=ob1.matrix[i][j];
        ob2.m=ob1.n;
        ob2.n=ob1.m;
        cout<<"Transpose is :\n";
        ob2.display();
}
int main()
{
        matrices ob;
        int option;
        while(1)
        {
                cout<<"\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Transpose\n5.Display\n6.Exit Program";
                cout<<"\n\nEnter your option:";
                cin>>option;
                switch(option)
                {
                        case 1:
                                ob.addition();
                                break;
                        case 2:
                                ob.subtraction();
                                break;
                        case 3:
                                ob.multiplication();
                                break;
                        case 4:
                                ob.transpose();
                                break;
                        case 5:
                                ob.display();
                                break;
                        case 6:
                                exit(1);
                }
        }
}

No comments:

Post a Comment