Operation on Matrix
Program
#include<iostream>#include<windows.h>
#include<stdio.h>
#include<conio.h>
using namespace std;
int a,b,c,d,sum1,sum2;
int entering();
class Dynamic{
public:
int **m1,**m2,**sum,**sub,**mul;
int Matrix1(int row,int colum);
int Matrix2(int row,int colum);
int Addition(int row,int colum);
int substraction(int row,int colum);
int multiplication(int row,int colum,int row1,int colum1);
int transpose(int row,int colum,int row1,int colum1);
int Displaym1(int row,int colum);
int Displaym2(int row,int colum);
};
Dynamic ss;
int menu(){
system("cls");
int choice;
cout<<"\n\n\t\t\t\t**************Lab Task**************"<<endl;
cout<<"\t\t*********************************************************************"<<endl;
cout<<"\t\t\t\t\t\tOperation Menu"<<endl;
cout<<"\t\t*********************************************************************"<<endl;
cout<<"\t\t\t1)Addition"<<endl;
Sleep(200);
cout<<"\t\t\t2)Substraction"<<endl;
Sleep(200);
cout<<"\t\t\t3)Transpose"<<endl;
Sleep(200);
cout<<"\t\t\t4)Multiplication"<<endl;
Sleep(200);
cout<<"\t\t\t5)Reenter Data"<<endl;
Sleep(200);
cout<<"\t\t\t6)Exit"<<endl;
Sleep(200);
cout<<"\t\t*********************************************************************"<<endl;
cout<<"Enter Operation that you perform:";
cin>>choice;
if(choice==1){
system("cls");
cout<<"\n\n\n\t\t\t\t**************Addition of 2D Matrix**************"<<endl;
cout<<"Matrix 1:"<<endl;
ss.Displaym1(a,b);
cout<<"Matrix 2:"<<endl;
ss.Displaym2(c,d);
cout<<"sum of two matrix is:\n";
if(sum1>sum2)
ss.Addition(a,b);
else
ss.Addition(c,d);
cout<<"Press any key to continue...";
getch();
menu();
}
else if(choice==2){
system("cls");
cout<<"\n\n\n\t\t\t\t**************Substraction of 2D Matrix**************"<<endl;
cout<<"Matrix 1:"<<endl;
ss.Displaym1(a,b);
cout<<"Matrix 2:"<<endl;
ss.Displaym2(c,d);
cout<<"Substraction of two matrix is:\n";
if(sum1>sum2)
ss.substraction(a,b);
else
ss.substraction(c,d);
cout<<"Press any key to continue...";
getch();
menu();
}
else if(choice==3){
system("cls");
cout<<"\n\n\n\t\t\t\t**************Transpose of 2D Matrix**************"<<endl;
cout<<"Matrix 1:"<<endl;
ss.Displaym1(a,b);
cout<<"Matrix 2:"<<endl;
ss.Displaym2(c,d);
cout<<"Matrix after transposing:\n";
ss.transpose(a,b,c,d);
cout<<"Press any key to continue...";
getch();
menu();
}
else if(choice==4){
system("cls");
cout<<"\n\n\n\t\t\t\t**************Multiplication of 2D Matrix**************"<<endl;
cout<<"Matrix 1:"<<endl;
ss.Displaym1(a,b);
cout<<"Matrix 2:"<<endl;
ss.Displaym2(c,d);
if(b!=c)
cout<<"Solution doesnot exist";
else
{
ss.multiplication(a,b,c,d);
}
cout<<"Press any key to continue...";
getch();
menu();
}
else if(choice==5)
entering();
}
Dynamic::Matrix1(int row,int colum){
m1=new int*[row];
for (int i = 0;i<row; i++)
m1[i] = new int[colum];
for(int i=0;i<colum;i++){
for(int j=0;j<row;j++)
cin>>m1[i][j];
}
}
Dynamic::Displaym1(int row,int colum){
//cout<<"Matrix 1:"<<endl;
for(int i=0;i<colum;i++){
for(int j=0;j<row;j++)
cout<<m1[i][j]<<" ";
cout<<endl;
}
}
Dynamic::Matrix2(int row,int colum){
m2=new int*[row];
for (int i = 0; i < row; i++)
m2[i] = new int[colum];
for(int i=0;i<colum;i++){
for(int j=0;j<row;j++)
cin>>m2[i][j];
}
}
Dynamic::Displaym2(int row,int colum){
// cout<<"Matrix 2:"<<endl;
for(int i=0;i<colum;i++){
for(int j=0;j<row;j++)
cout<<m2[i][j]<<" ";
cout<<endl;
}
}
Dynamic::transpose(int row,int colum,int row1,int colum1)
{
cout<<"matrix 1:\n";
for(int i=0;i<row;i++){
for(int j=0;j<colum;j++)
cout<<m1[j][i]<<" ";
cout<<endl;
}
cout<<"matrix 2:\n";
for(int i=0;i<row1;i++){
for(int j=0;j<colum1;j++)
cout<<m2[j][i]<<" ";
cout<<endl;
}
}
Dynamic::Addition(int row,int colum){
sum=new int*[row];
for (int i = 0; i < row; i++)
sum[i] = new int[colum];
for(int i=0;i<colum;i++)
{
for(int j=0;j<row;j++){
sum[i][j]=m1[i][j]+m2[i][j];
}
}
//cout<<"Sum of two matrix:"<<endl;
for(int i=0;i<colum;i++)
{
for(int j=0;j<row;j++){
cout<<sum[i][j]<<" ";
}
cout<<endl;
}
}
Dynamic::substraction(int row,int colum){
sub=new int*[row];
for (int i = 0; i < row; i++)
sub[i] = new int[colum];
for(int i=0;i<colum;i++)
{
for(int j=0;j<row;j++){
sub[i][j]=m1[i][j]-m2[i][j];
}
}
//cout<<"Sum of two matrix:"<<endl;
for(int i=0;i<colum;i++)
{
for(int j=0;j<row;j++){
cout<<sub[i][j]<<" ";
}
cout<<endl;
}
cout<<"Press any key to continue...";
getch();
menu();
}
Dynamic::multiplication(int row,int colum,int row1,int colum1){
mul=new int*[row];
for (int i = 0; i < row; i++)
mul[i] = new int[colum];
for(int i = 0; i < row; i++)
for(int j = 0; j < colum;j++){
mul[i][j]=0;
}
for(int i = 0; i < row; i++)
for(int j = 0; j < colum;j++)
for(int k = 0; k < colum1; k++)
{
mul[i][j] += m1[i][k] * m2[k][j];
}
cout<<"Multiplication of two matrix is:"<<endl;
for(int i = 0; i < row; i++){
for(int j = 0; j < colum;j++){
cout <<mul[i][j]<<" ";
}
cout<<endl;
}
//ttttttttttttttttttttttttttttt
}
int entering(){
system("cls");
cout<<"\n\n\t\t\t\t**************Entering Data**************"<<endl;
cout<<"Matrix 1:"<<endl;
cout<<"Enter no of rows for matrix 1:";
cin>>a;
cout<<"Enter no of column for matrix 1:";
cin>>b;
ss.Matrix1(a,b);
//ss.Displaym1(a,b);
sum1=a+b;
cout<<"Matrix 2:"<<endl;
cout<<"Enter no of rows for matrix 2:";
cin>>c;
cout<<"Enter no of column for matrix 2:";
cin>>d;
ss.Matrix2(c,d);
//ss.Displaym2(c,d);
sum2=c+d;
cout<<"Press any key to continue...";
getch();
menu();
}
int main()
{
system("color f1");
entering();
}
x
Comments
Post a Comment