Breaking News

Assignment No-2 Matrix Operations Program using C++ (Matrix.cpp)-Prof.B.S prasad

DATA STRUCTURES AND ALGORITHM PROGRAMMES


Problem Statement:

Write C/C++ program for storing matrix. Write functions for 
a) Check whether given matrix is upper triangular or not 
b) Compute summation of diagonal elements 
c) Compute transpose of matrix 
d) Add, subtract and multiply two matrices.

Problem Solution:
#include<iostream>
using namespace std;
class mat
{
int i,j,k,a[3][3],b[3][3],c[3][3];
int sum,count,ch,m,n,temp;
public:
void get();
void display();
int add();
int mul();
int trans_A();
int uppertri();
int lowertri();
int diagonal1();
int diagonal2();
};
void mat::get()
{

        cout<<"Enter the number of rows:"<<"\n";
cin>>m;


cout<<"Enter the number of columns:"<<"\n";
cin>>n;
   
   
cout<<"Enter first matrix="<<"\n";
        for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter second matrix"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}


cout<<"Entered first matrix is:"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}

         cout<<"Entered second matrix is:"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<"\t";

}
cout<<"\n";
}
}
int mat::add()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}

int mat::mul()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
int mat::trans_A()
{

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;}
}

cout<<"Transpose of A is="<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[j][i]<<"\t";

}
        cout<<"\n";

}
}

int mat::uppertri()
{
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
sum=sum+a[i][j];

}
}
cout<<"Sum of first upper tri matrix is (for A):"<<sum<<"\n";
}

int mat::lowertri()
{

sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
sum=sum+a[i][j];

}
}
cout<<"Sum of first lower tri matrix is (for A):"<<sum<<"\n";
}
int mat::diagonal1()
{
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)

sum=sum+a[i][j];
}
}
cout<<"Sum of diagonal elements is(left to right):"<<sum<<"\n";
}
int mat::diagonal2()
{
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if((i+j==2))

sum=sum+a[i][j];
}
}
cout<<"Sum of diagonal elements is(right to left) for A:"<<sum<<"\n";
}
void mat::display()
{

cout<<"solution is="<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";

}
        cout<<"\n";

}


}
int main()
{
mat m;
int ch;
char r,y;
do
{

   cout<<"\n 1.Addition \n 2.Multiplication \n 3.Transpose of A \n 4.Sum of Upper triangle for A \n 5.Sum of Lower triangle for A \n 6.Sum of diagonal elements(left to right) for A \n 7.Sum of diagonal elements(right to left) for A \n Enter ur choice \n";
cin>>ch;

switch(ch)
{
case 1:
m.get();
m.add();
m.display();
break;
case 2:
m.get();
m.mul();
m.display();
break;
case 3:
        m.get();
m.trans_A();
break;
case 4:
        m.get();
m.uppertri();
break;
case 5:
        m.get();
m.lowertri();
break;
case 6:
        m.get();
m.diagonal1();
break;
case 7:
        m.get();
m.diagonal2();
break;
default:
cout<<"wrong choice"<<"\n";
break;
}
 cout<<"Press y to continue...."<<"\n";
cin>>r;

}while(r=='y');
return 0;                                             

 }

*************************************OUTPUT************************************

No comments

Thank You For your Query !! Please Contact Us via Contact Form For any kind of Information.