Saturday, August 27, 2011

Program to calculate multiplication of two matrix using C++.


//Program to calculate multiplication of two matrix using C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int a[10][10],b[10][10],c[10][10],i,j,k;
 int m, n, p, q;
 clrscr();
 cout<<endl<<"Enter the number of rows in A matrix: ";
 cin>>m;
 cout<<endl<<"Enter the number of columns in A matrix: ";
 cin>>n;
 cout<<endl<<"Enter the number of rows in B matrix: ";
 cin>>p;
 cout<<endl<<"Enter the number of rows in B matrix: ";
 cin>>q;
 if(n==p)
 {
  cout<<endl<<"Elements of a matrix:";
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
   {
    cin>>a[i][j];
   }
  }
  cout<<endl<<"Elements of b matrix:";
  for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
   {
    cin>>b[i][j];
   }
  }
  for(i=0;i<m;i++)
  {
   for(j=0;j<q;j++)
   {
    c[i][j]=0;
    for(k=0;k<n;k++)
    {
     c[i][j]+=a[i][k]*b[k][j];
    }
   }
  }
   cout<<endl<<" a matrix: "<<endl;
  for(i=0;i<m;i++)
  {
   cout<<endl;
   for(j=0;j<n;j++)
   {
    cout<<" "<<a[i][j];
   }
  }
  cout<<endl<<" b matrix:"<<endl;
  for(i=0;i<p;i++)
  {
   cout<<endl;
   for(j=0;j<q;j++)
   {
    cout<<" "<<b[i][j];
   }
  }
  cout<<endl<<" mul of two matrix"<<endl;
  for(i=0;i<m;i++)
  {
   cout<<endl;
   for(j=0;j<q;j++)
   {
    cout<<" "<<c[i][j];
   }
  }
 }
 else
 {
  cout<<endl<<"Multiplication is not possible ";
 }
 getch();
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More