CPP Program for implementing various Matrix Operations

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void matread(int m[10][10],int r,int c)
{
   int i,j;
   cout<<endl<<"Enter the Elements of the Matrix : ";
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  cin>>m[i][j];
}
void matdisplay(int m[10][10],int r,int c)
{
   int i,j;
   cout<<"The Resultant Matrix : ";
   for(i=0;i<r;i++)
   {
      cout<<endl;
      for(j=0;j<c;j++)
  cout<<m[i][j]<<" ";
   }
}
void matadd(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m3[i][j]=m1[i][j]+m2[i][j];
}
void matsub(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m3[i][j]=m1[i][j]-m2[i][j];
}
void matprod(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c, int rc)
{
   int i,j,k;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
      {
  m3[i][j]=0;
  for(k=0;k<rc;k++)
     m3[i][j]+=m1[i][k]*m2[k][j];
      }
}
void transpose(int m1[10][10],int m2[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m2[i][j]=m1[j][i];
}
void rowsum(int m[10][10], int r, int c)
{
   int i,j,rs;
   cout<<endl<<"Sum of each Row elements";
   cout<<endl<<"------------------------";
   for(i=0;i<r;i++)
   {
      rs=0;
      for(j=0;j<c;j++)
  rs+=m[i][j];
      cout<<endl<<rs;
   }
   cout<<endl<<"========================";
}
void colsum(int m[10][10], int r, int c)
{
   int i,j,cs;
   cout<<endl<<"Sum of each Column elements";
   cout<<endl<<"---------------------------";
   for(j=0;j<c;j++)
   {
      cs=0;
      for(i=0;i<r;i++)
  cs+=m[i][j];
      cout<<endl<<cs;
   }
   cout<<endl<<"===========================";
}
void maindiag(int m[10][10], int r, int c)
{
   int i,mds=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
  mds+=m[i][i];
      cout<<endl<<"The Sum of Mian Diagonal Elements = "<<mds;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void offdiag(int m[10][10], int r, int c)
{
   int i,j,ods=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i+j == r-1)
        ods+=m[i][j];
      }
      cout<<endl<<"The Sum of Off Diagonal Elements = "<<ods;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void uptrisum(int m[10][10], int r, int c)
{
   int i,j,uts=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i<j)
        uts+=m[i][j];
      }
      cout<<endl<<"The Sum of Upper Triangular Elements = "<<uts;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void lowtrisum(int m[10][10], int r, int c)
{
   int i,j,lts=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i>j)
        lts+=m[i][j];
      }
      cout<<endl<<"The Sum of Lower Triangular Elements = "<<lts;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void main()
{
   clrscr();
   int r1,c1,r2,c2,ch;
   int A[10][10],B[10][10],C[10][10];
   char yn;
   cout<<endl<<"Enter the order of the First Matrix : ";
   cin>>r1>>c1;
   matread(A,r1,c1);
   cout<<endl<<"Enter the order of the Second Matrix : ";
   cin>>r2>>c2;
   matread(B,r2,c2);
   do
   {
      clrscr();
      cout<<endl<<" Menu";
      cout<<endl<<"------";
      cout<<endl<<" 1. Matrix Addition"
   <<endl<<" 2. Matrix Subtraction"
   <<endl<<" 3. Matrix Multiplication"
   <<endl<<" 4. Transpose of Matrix"
   <<endl<<" 5. Sum of Elements in Each Row"
   <<endl<<" 6. Sum of Elements in Each Column"
   <<endl<<" 7. Sum of Mian Diagonal Elements"
   <<endl<<" 8. Sum of Off Diagonal Elements"
   <<endl<<" 9. Sum of Upper Triangular Elements"
   <<endl<<"10. Sum of Lower Triangular Elements"
   <<endl<<endl<<"Enter Your Choice : ";
      cin>>ch;
      switch (ch)
      {
  case 1: if((r1==r2)&&(c1==c2))
   {
     matadd(A,B,C,r1,c1);
     matdisplay(C,r1,c1);
   }
   else
     cout<<"Matrices are not confirmable for Addition";
   break;
  case 2: if((r1==r2)&&(c1==c2))
   {
     matsub(A,B,C,r1,c1);
     matdisplay(C,r1,c1);
   }
   else
   cout<<"Matrices are not confirmable for subtraction";
   break;
  case 3:if(c1==r2)
   {
     matprod(A,B,C,r1,c2,c1);
     matdisplay(C,r1,c2);
   }
   else
     cout<<"Matrices are not confirmable for multiplication";
   break;
  case 4: transpose(A,C,r1,c1);
   matdisplay(C,r1,c1);
   break;
  case 5: rowsum(A,r1,c1);
   break;
  case 6: colsum(A,r1,c1);
   break;
  case 7: maindiag(A,r1,c1);
   break;
  case 8: offdiag(A,r1,c1);
   break;
  case 9: uptrisum(A,r1,c1);
   break;
  case 10:lowtrisum(A,r1,c1);
   break;
      }
   cout<<endl<<endl<<"To Continue...(Y/y) : ";
   cin>>yn;
   }
   while(toupper(yn)=='Y');
}

No comments:

Post a Comment