#include<iostream>
using namespace std;
float*readmatrix(int,int);
void DisplayMatrix(float*,int,int); //to display the matrix
float*MultiplyMatrices(float*,int,int,float*,int,int);
int main()
{
    int frow=2,fcol=3,srow=3,scol=2;
    float*m1=readmatrix(frow,fcol);
    float*m2=readmatrix(srow,scol);
    DisplayMatrix(MultiplyMatrices(m1,frow,fcol,m2,srow,scol),frow,scol);
    return 0;
}
float *readmatrix(int row,int col)
{
    cout<<" enter a "<<row<<" by "<<col<<" matrix :"<<endl;
    float *matrix=new float[row*col];
    for(int p=0;p<row;p++)
    {
        cout<<"Enter "<<p+1<<" th row  :  ";
        for(int q=0;q<col;q++)             cin>>(*(matrix+p*col+q));
    }
    cout<<endl;
    return matrix;
}

void DisplayMatrix(float* Matrix,int row,int column)
{
    for(int p=0;p<row;p++)
    {
        cout<<"The  "<<p+1<<" th row  :  ";
        for(int q=0;q<column;q++)
            cout<<*(Matrix+p*column+q)<<" ";
        cout<<endl;
    }
}

float* MultiplyMatrices(float* FirstMatrix,int frow,int fcol,float* SecondMatrix,int srow,int scol)
{
    float*ProductMatrix=new float[frow*scol];
    for(int p=0;p<frow;p++)
    {
        for(int q=0;q<scol;q++)
        {
            *(ProductMatrix+frow*p+q)=0;
             for(int r=0;r<fcol;r++)
                  *(ProductMatrix+scol*p+q)+=((*(FirstMatrix+p*fcol+r))*(*(SecondMatrix+r*scol+q)));
        }
     }
     return ProductMatrix;
}
Advertisements