Wednesday, October 12, 2011

Program code for multiplication of two metrices(Using CPP)

#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,mat1[3][3],mat2[3][3],mul[3][3],k;
    clrscr();
    cout<<"\n__________________________________";
    cout<<"\n Enter the value of First matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" Enter the value of matrix1["<<i<<"]["<<j<<"] =>";
            cin>>mat1[i][j];
        }
    }
    cout<<"\n The matrix is\n";
    cout<<"_________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" "<<mat1[i][j];
        }
        cout<<"\n";
    }
    cout<<"\n__________________________________";
    cout<<"\n Enter the value of Secound matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" Enter the value of matrix2["<<i<<"]["<<j<<"]=>";
            cin>>mat2[i][j];
        }
    }
    cout<<"\n The matrix is\n";
    cout<<"_________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" "<<mat2[i][j];
        }
        cout<<"\n";
    }
    cout<<"\n__________________________________";
    cout<<"\n Multiplication of the both matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            mul[i][j]=0;
            for(k=1;k<4;k++)
            {
                mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
            }
            cout<<" "<<mul[i][j];
        }
        cout<<"\n";
    }
    getch();
}
                     OUTPUT
__________________________________
 Enter the value of First matrix
__________________________________

 Enter the value of matrix1[1][1] =>1
 Enter the value of matrix1[1][2] =>2
 Enter the value of matrix1[1][3] =>3
 Enter the value of matrix1[2][1] =>4
 Enter the value of matrix1[2][2] =>5
 Enter the value of matrix1[2][3] =>4
 Enter the value of matrix1[3][1] =>3
 Enter the value of matrix1[3][2] =>2
 Enter the value of matrix1[3][3] =>1

 The matrix is
_________________

 1 2 3
 4 5 4
 3 2 1

__________________________________
 Enter the value of Secound matrix
__________________________________

 Enter the value of matrix2[1][1] =>4
 Enter the value of matrix2[1][2] =>5
 Enter the value of matrix2[1][3] =>3
 Enter the value of matrix2[2][1] =>2
 Enter the value of matrix2[2][2] =>5
 Enter the value of matrix2[2][3] =>4
 Enter the value of matrix2[3][1] =>2
 Enter the value of matrix2[3][2] =>3
 Enter the value of matrix2[3][3] =>2

 The matrix is
_________________

 4 5 3
 2 5 4
 2 3 2

__________________________________
 Multiplication of the both matrix
__________________________________

 14 24 17
 32 54 38
 18 28 19