Multiplying two 3×3 Matrix in Cplusplus

By | January 31, 2013
3x3matricTwo Matrics are to be multiplied. If A is an nxm matrix and B is an mxp matrix, the result AB of their multiplicaiotn is an nxp matrix defined only if the number of columns m in A is equal to the number of rows m in B.
Basically when we multiply matrices, the number of the rows in the first matrix are multiplied with corresponding columns in the second matrix. One may compute each entry in the third matrix one at a time.

Source Code

#include
using namespace std;
void main()
{
            intq,w,e,s,d,f,x,c,v;
            inta[3][3];
            intb[3][3];
            cout<<” enter values of matrices A “<<endl;
            for(int i=0;i<3;i++)
            {
                        for(int j=0;j<3;j++)
            {
                        cin>>a[i][j];
            }
            }
            cout<<”   matrices A “<<endl;
            for(int i=0;i<3;i++)
            {
                        cout<<”[  ";
                        for(int j=0;j<3;j++)
                        { cout<<a[i][j]<<”    “;
                        }
                        cout<<”]   \n”;
            }
cout<<endl;
cout<<”plz enter value of matrices B”<<endl;
            for(int i=0;i<3;i++)
            {
                        for(int j=0;j<3;j++)
            {
                        cin>>b[i][j];
            }
            }
cout<<”  matrices B “<<endl;
            for(int i=0;i<3;i++)
            {
                        cout<<”[  ";
                        for(int j=0;j<3;j++)
                        { cout<<b[i][j]<<”    “;
                        }
                        cout<<”]   \n\n”;
            }
            cout<<”   resultant”<<endl;
            q=((a[0][0]*b[0][0])+(a[0][1]*b[1][0])+(a[0][2]*b[2][0]));
            w=((a[0][0]*b[0][1])+(a[0][1]*b[1][1])+(a[0][2]*b[2][1]));
            e=((a[0][0]*b[0][2])+(a[0][1]*b[1][2])+(a[0][2]*b[2][2]));
            s=((a[1][0]*b[0][0])+(a[1][1]*b[1][0])+(a[1][2]*b[2][0]));
            d=((a[1][0]*b[0][1])+(a[1][1]*b[1][1])+(a[1][2]*b[2][1]));
            f=((a[1][0]*b[0][2])+(a[1][1]*b[1][2])+(a[1][2]*b[2][2]));
            x=((a[2][0]*b[0][0])+(a[2][1]*b[1][0])+(a[2][2]*b[0][0]));
            c=((a[2][0]*b[0][1])+(a[2][1]*b[1][1])+(a[2][2]*b[2][1]));
            v=((a[2][0]*b[0][2])+(a[2][1]*b[1][2])+(a[2][2]*b[2][2]));
            intr[3][3]={{q,w,e},{s,d,f},{x,c,v}};
            for(int i=0;i<3;i++)
            {
                        cout<<”[  ";
                        for(int j=0;j<3;j++)
                        { cout<<r[i][j]<<”    “;
                        }
                        cout<<”]   \n”;
            }
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS