Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to multiply two matrices

Posted By: Easy Tutor     Category: C++ Programming     Views: 14151

Write a program to multiply two matrices.

Code for Program to multiply two matrices in C++ Programming

 # include <iostream.h>
 # include   <stdlib.h>
 # include    <conio.h>

 /***********************************************************************///-----------------------------  Matrix  ------------------------------///***********************************************************************/class Matrix
 {
    private:
       float matrix_a[3][3];
       float matrix_b[3][3];
       float matrix_c[3][3];

    public:
       Matrix( );

       void get_matrix_a( );
       void get_matrix_b( );
       void multiply_matrices( );
       void show_result_Matrix( );
 };

 /***********************************************************************///----------------------------  Matrix( )  ----------------------------///***********************************************************************/

 Matrix::Matrix( )
 {
    for(int i=0;i<3;i++)
    {
       for(int j=0;j<3;j++)
       {
      matrix_a[i][j]=0;
      matrix_b[i][j]=0;
      matrix_c[i][j]=0;
       }
    }

    gotoxy(1,1);
    cout<<" ******************************************************************************"<<endl;
    cout<<" * * * * * * * * * * * * * *  Matrix  Multiplication  * * * * * * * * * * * * *"<<endl;
    cout<<" ******************************************************************************"<<endl;

    gotoxy(1,25);
    cout<<" ******************************************************************************";
 }

 /***********************************************************************///-------------------------  get_matrix_a( )  -------------------------///***********************************************************************/void Matrix::get_matrix_a( )
 {
    gotoxy(1,6);
    cout<<" Enter the values of the Matrix A row by row :\n "<<endl;

    cout<<"\t\t\t Ú               ¿"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t À               Ù"<<endl;

    gotoxy(18,10);
    cout<<" A  = "<<endl;

    int x=28;
    int y=9;

    for(int i=0;i<3;i++)
    {
       for(int j=0;j<3;j++)
       {
      gotoxy(x,y);
      cin>>matrix_a[i][j];

      x+=5;
       }

       x=28;
       y++;
    }
 }

 /***********************************************************************///-------------------------  get_matrix_b( )  -------------------------///***********************************************************************/void Matrix::get_matrix_b( )
 {
    gotoxy(1,15);
    cout<<" Enter the values of the Matrix B row by row :\n "<<endl;

    cout<<"\t\t\t Ú               ¿"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t À               Ù"<<endl;

    gotoxy(18,19);
    cout<<" B  = "<<endl;

    int x=28;
    int y=18;

    for(int i=0;i<3;i++)
    {
       for(int j=0;j<3;j++)
       {
      gotoxy(x,y);
      cin>>matrix_b[i][j];

      x+=5;
       }

       x=28;
       y++;
    }
 }

 /***********************************************************************///----------------------  multiply_matrices( )  -----------------------///***********************************************************************/void Matrix::multiply_matrices( )
 {
    for(int i=0;i<3;i++)
    {
       for(int j=0;j<3;j++)
       {
      floatvalue=0;
      float sum=0;

      for(int k=0;k<3;k++)
      {
         value=matrix_a[j][k]*matrix_b[k][j];
         sum+=value;
      }

      matrix_c[i][j]=sum;
       }
    }
 }

 /***********************************************************************///---------------------  show_result_Matrix( )  -----------------------///***********************************************************************/void Matrix::show_result_Matrix( )
 {
    clrscr( );

    gotoxy(1,1);
    cout<<" ******************************************************************************"<<endl;
    cout<<" * * * * * * * * * * * * * *  Matrix  Multiplication  * * * * * * * * * * * * *"<<endl;
    cout<<" ******************************************************************************"<<endl;

    gotoxy(1,6);
    cout<<" The values of Matrix A and B are :"<<endl;

    cout<<"\t\t Ú               ¿                  Ú               ¿"<<endl;
    cout<<"\t\t ³               ³                   ³               ³"<<endl;
    cout<<"\t\t ³               ³                   ³               ³"<<endl;
    cout<<"\t\t ³               ³                  ³               ³"<<endl;
    cout<<"\t\t À               Ù             À               Ù"<<endl;

    gotoxy(45,9);
    cout<<" B  = "<<endl;

    gotoxy(10,9);
    cout<<" A  = "<<endl;

    gotoxy(1,15);
    cout<<" The Product of Matrix A and B is :\n "<<endl;

    cout<<"\t\t\t Ú               ¿"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t ³               ³"<<endl;
    cout<<"\t\t\t À               Ù"<<endl;

    gotoxy(13,19);
    cout<<" A * B  = "<<endl;

    int x_1=20;
    int y_1=8;

    int x_2=55;
    int y_2=8;

    int x_3=28;
    int y_3=18;

    for(int i=0;i<3;i++)
    {
       for(int j=0;j<3;j++)
       {
      gotoxy(x_1,y_1);
      cout<<matrix_a[i][j];

      gotoxy(x_2,y_2);
      cout<<matrix_b[i][j];

      gotoxy(x_3,y_3);
      cout<<matrix_c[i][j];

      x_1+=5;
      x_2+=5;
      x_3+=5;
       }

       x_1=20;
       y_1++;

       x_2=55;
       y_2++;

       x_3=28;
       y_3++;
    }

    gotoxy(1,25);
    cout<<" ******************************************************************************";
 }

 int main( )
 {
    textmode(BW80);
    clrscr( );

    Matrix Obj;

    Obj.get_matrix_a( );
    Obj.get_matrix_b( );
    Obj.multiply_matrices( );
    Obj.show_result_Matrix( );

    getch( );
    return 0;
 }
  
Share: 


Didn't find what you were looking for? Find more on Program to multiply two matrices Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to multiply two matrices is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!