multiply of complex numbers matrix

sahasrara 0 Tallied Votes 2K Views Share

Dear entourage
Here is a solution that read two matrix of complex numbers
( including imaginary an real ) and add and multiply these together.
I wrote it by Borland 5.02 .

Do joy !!!

//multiply of complex numbers matrix
//programming by : Erfan Nasoori
//Email : ketn68@yahoo.com
//Date of sent : 2009/1/21


#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct complex
{
 float real;
 float imag;
};

void plusmat(const complex[][10],const complex[][10],complex[][10],int,int);
void prodmat(const complex[][10],const complex[][10],complex[][10],int,int,int); 

void main()
{
 complex m1[10][10],m2[10][10],m3[10][10],k,s;

 int r1,r2,c1,c2,i,j;

 cout<<"matrix1: rows=? , cols=?"<<endl;
 cout<<"r1:";   cin>>r1;
 cout<<"c1:";   cin>>c1;

 cout<<"matrix1 is:"<<endl;
 for( i=0;i<r1;++i)
  for(j=0;j<c1;++j)
  {
   cout<<"real:";   cin>>m1[i][j].real;
   cout<<"imag:";   cin>>m1[i][j].imag;
  }


 cout<<"matrix2: rows=? , cols=?"<<endl;
 cout<<"r2:";   cin>>r2;
 cout<<"c2:";   cin>>c2;

 cout<<"matrix2 is:"<<endl;
 for(i=0;i<r2;++i)
  for(j=0;j<c2;++j)
  {
   cout<<"real:";   cin>>m2[i][j].real;
   cout<<"imag:";   cin>>m2[i][j].imag;
  }


 if(r1==r2 && c1==c2)
 {
  plusmat(m1,m2,m3,r1,r2);
  cout<<"matrix1+matrix2="<<endl;

  for(i=0;i<r1;++i)
  {
   for(j=0;j<c1;++j)
    cout<<setw(15)<<setprecision(10)<<m3[i][j].real<<"+i("<<m3[i][j].imag<<")";
   cout<<endl;
  }
}

 else
  cout<<"Both of matrixes aren't match to adding together!"<<endl;

 if(c1==r2)
 {
  prodmat(m1,m2,m3,r1,c1,c2);

  cout<<"matrix1 * matrix2="<<endl;
  for(i=0;i<r1;++i)
  {
   for(j=0;j<c2;++j)
    cout<<setw(15)<<setprecision(10)<<m3[i][j].real<<"+i("<<m3[i][j].imag<<")";
   cout<<endl;
  }
 }

 else
  cout<<"Both of matrixes aren't match to multipling together!"<<endl;
}


void plusmat(const complex a[][10],const complex b[][10],complex p[][10],int r1,int c2)
{
 int i,j;
 for(i=0;i<r1;++i)
  for(j=0;j<c2;++j)
  {
   p[i][j].real=a[i][j].real+b[i][j].real;
   p[i][j].imag=a[i][j].imag+b[i][j].imag;
  }
}


void prodmat(const complex a[][10],const complex b[][10],complex z[][10],int r1,int c1r2,int c2)
{
 int i,j;
 for(i=0;i<r1;++i)
  for( j=0;j<c2;++j)
  {
   z[i][j].real=z[i][j].imag=0;
   for(int n=0;n<c1r2;++n)
   {
    z[i][j].real+=(a[i][n].real * b[n][j].real)-(a[i][n].imag * b[n][j].imag);
    z[i][j].imag+=(a[i][n].real * b[n][j].imag)+(a[i][n].imag * b[n][j].real);
   }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.