Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

23 Aug 2013

MULTIPLICATION OF TWO MATRICES

Write a program for multiplication  of two matrices
                        OR
Write a program to multiply two matrices

#include<iostream.h>
#include<conio.h>
void main()
{  
 clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q,k,x[10];
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of colomns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of colomn 2nd matrix\n";
 cin>>q;
 if(n==p)
 {
  cout<<"\n\nenter the elements of 1st matrix\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\nfirst matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<b[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<q;j++)
   {
   c[i][j]=0;
    {
     for(k=0;k<n;k++)
      {  
        x[k]=0;
        x[k]=a[i][k]*b[k][j];
        c[i][j]=c[i][j]+x[k];
      }
    }}
   }
  cout<<"\n\nmultiplication of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<q;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be multiplied";
     }

getch();
}

==OUTPUT==


enter no of rows of 1st matrix
2
enter no of column of 1st matrix
2
enter no of rows of 1st matrix
2
enter no of column of 1st matrix
2

enter the elements of 1st matrix
a[1]=1
a[2]=2
a[3]=2
a[4]=3

first matrix u have entered is:=
1 2
2 3

enter the elements of 2nd matrix
b[1]=2
b[2]=3
b[3]=2
b[4]=1

2nd matrix u have entered is
2 3
2 1

multiplication of two matrices is
6 5
8 9

No comments:

Post a Comment