Wednesday, February 3, 2016

C++ program for matrix multiplication

//c++ program to multiply two matrices (max 6x6 size *here)
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int r1,r2,c1,c2,a[6][6],b[6][6],c[6][6],i,j; //size of array can be changed
cout<<”Enter the number of rows and columns of first matrix\n”;
cin>>r1>>c1;
cout<<”Enter the number of rows and columns of second matrix\n”;
cin>>r2>>c2;
if (c1!=r2)
                cout<<”\n matrix multiplication is not possible”;
else
                {
                cout<<”\n matrix multiplication is possible”;
                cout<<”\nEnter the elements in first matrix row wise\n”;
                for(i=0;i<r1;++i)
                                {
                                for(j=0;j<c1;++j)
                                                cin>>a[i][j];
             }
                cout<<”\nEnter the elements in second matrix row wise\n”;
                for(i=0;i<r2;++i)
                                {
                                for(j=0;j<c2;++j)
                                                cin>>b[i][j];
             }
                cout<<”\nThe result is\n”;
                for(i=0;i<r1;++i)
                                {
                                for (j=0;j<c2;++j)
                                                {
                                                c[i][j]=0;
                                                For(int k=0;k<c1;++k)
                                                                c[i][j]+=(a[i][k]*b[k][j]);
                     }
              }
                for(i=0;i<r1;++i)
                                {
                                for(j=0;j<c2;++j)
                                                cout<<c[i][j];<<”\t”;
                                cout<<”\n”;
              }
}
getch();
return0;
}


No comments:

Post a Comment

Featured Post

Words from the Prophet Muhammad (pbuh)'s last sermon

“Remember, one day you will appear before Allah and answer for your deeds. So beware, do not astray from the path of righteousness aft...