Pages

Saturday 19 March 2011

PROGRAM TO MULTIPLY TWO MATRICES

DESCRIPTION-: This program takes two matrices as input then calculate the multiplication of input  matrices and then generate the output matrix.The output matrix contains the multiplication of input matrices.
FOR EXAMPLE-:

If we have two input matrices as---------

              2   1                                       3   1   2
              1   3                                       1   2   3
              3   1
Then program generate output as--------
                      7   4   7
                      6   7   11
                     10  5   9

PROGRAM-:
#include<iostream.h> 
#include<conio.h>
void main()
{
clrscr();
int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10];
cout<<"enter row and column of matrix A";
cin>>m>>n;
cout<<"\n enter the row and column of matrix B";
cin>>p>>q;
if(n!=p)
{
cout<<"multiplication not possible";
}
else
{
cout<<"enter value of elements of matrix A"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter values of elements of matrix B"<<"\n";

for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
cout<<"values of elements of matrix A are"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"values of elements of matrix B are"<<"\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t";
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"multiplication  of two matrix is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<p;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
cout<<c[i][j]<<"\t";
}
 }
}
getch();
}

0 comments:

Post a Comment

 
Copyright (c) 2010 Concepts Of C++. Design by WPThemes Expert

Blogger Templates and RegistryBooster.