Pages

Tuesday, August 20, 2013

Program to multiply the 2 matrices

//Program to multiply the 2 matrices
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

class matrix
{
   int a[4][4],b[4][4],c[4][4];
   int m,n,p,q;
   public:
   void getdata()
   {
    cout<<"Enter the size of matrix 1";
    cin>>m>>n;

    cout<<"Enter the size of matrix 2";
    cin>>p>>q;
   }
   void showdata();
};


void matrix::showadata()
{
int i,j,k;

 if (n==p)
     {

    cout<<"\nEnter matrix 1:\n";
    for(i=0;i<m;i++)
     {
       for(j=0;j<n;j++)
        cin>>a[i][j]);
     }

       cout<<"\nEnter matrix 2:\n";
       for(i=0;i<p;i++)
      {
        for(j=0;j<q;j++)
        cin>>b[i][j];
      }

       cout<<"The matrices were:\n";
       cin<<"Matrix 1=\n";
       for(i=0;i<m;i++)
     {
       for(j=0;j<n;j++)
       cout<<a[i][j]<<"\t";
       cout<<"\n";
      }

    cout<<"\nmatrix 2=\n";
    for(i=0;i<p;i++)
     {
       for(j=0;j<q;j++)
       cout<<b[i][j]<<"\t";;
       printf("\n");
     }

     cout<<"\nThe mulptiplication of matrices is :\n";
     for(i=0;i<m;i++)
     {
       for(j=0;j<p;j++)
    {
    c[i][j]=0;
      {
       for(k=0;k<p;k++)
          c[i][j]+=(a[i][k]*b[k][j]);
          cout<<c[i][j]<<"\t";
       }
    }
          cout<<"\n";
     }
}
else
cout<<"\aORDER MISMATCHED! Matrix cannot be multiplied";
}

int main()
{
matrix m;
//clrscr();
m.getdata();
m.showdata();
getch();
}

No comments:

Post a Comment