-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Saturday 22 August 2015

Matrix multiplication in C++

You have often learnt the method of matrix multiplication in Mathematics.

Matrix multiplication


MAtrix multiplication

Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
void multiplication();

int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 3*3 matrix multiplication."<<endl<<endl;
cout<<"Press 2 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
multiplication();
break;
case 2:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}

goto x;
}
}
void multiplication()
{     
           cout<<"\nEnter values for first 3 x 3 matrix:\n";
           for ( i = 0 ; i <= 2 ; i++ )
                {
                   for (j = 0 ; j <= 2 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 3 x 3 matrix:\n";
                   for ( i = 0 ; i <= 2 ; i++ )
                        {
                            for ( j = 0 ; j <= 2 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 3 x 3 matrix entered by you is:\n";
                            for ( i = 0 ; i <= 2 ; i++ )
                                 {
                                       for ( j = 0 ; j <= 2 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 3 x 3 matrix entered :\n";
                                       for ( i = 0 ; i <= 2 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 2 ; j++ )
                                                       cout<< mat2[i][j]<<"\t" ;
                                                       cout<<endl<<endl;
                                            }
                                       for ( i = 0 ; i <= 2 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 2 ; j++ )
                                                      {
                                                          sum = 0;
                                                            for ( k = 0 ; k <=2 ; k++ )
                                                                   sum = sum + mat1 [i][k] * mat2[k][j];
                                                                   mat3[i][j] = sum ; 
                                                       }
                                             }
                                                                   cout<<"\nThe product of the above two matrices is:\n";
                                                                   for ( i = 0 ;i<= 2 ; i++ )
                                                                        {
                                                                             for ( j = 0 ; j <= 2 ; j++ )
                                                                                      cout<<mat3[i][j]<<"\t";
                                                                                      cout<<endl<<endl;
                                                                         }
                                                                     }

Output

Matrix multiplication in C++

                                                                    

No comments:

Post a Comment