// operations with 2-D matrix
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int matx1[10][10],
matx2[10][10], matx3[10][10], i, j, k, row1,row2,
col1, col2, choice,
opt1, num;
char opt2, opt3;
do
{
clrscr();
cout<<"\t\t
Operations on matrix"
<<"\n\n
Menu"
<<"\n
1. Sum "
<<"\n
2. Difference "
<<"\n
3. product "
<<"\n
4. transvers"
<<"\n
5. row sum & column sum"
<<"\n
6. sum of elements above and below the diagonal"
<<"\n
7. Exit";
int sum_row_col(int
[10][10], int, int, char);
int sum_dia(int
[10][10], int );
cout<<"\n
Enter your choice";
cin>>choice;
cout<<"\n
Enter the number of rows and column in first matrix";
cin>>row1>>col1;
cout<<"\n Enter
the element in the first matrix";
for (i=0;i<row1;++i)
{
for
(j=0;j<col1;++j)
cin>>matx1[i][j];
}
cout<<"\n
Enter the number of rows and column in second matrix";
cin>>row2>>col2;
cout<<"\n
Enter the element in the second matrix";
for (i=0;i<row2;++i)
{
for
(j=0;j<col2;++j)
cin>>matx2[i][j];
}
switch(choice)
{
case 1:
if(row1==row2&&col1==col2)
{
cout<<"\nsum of the two matrix
is :";
for (i=0;i<row1;++i)
{
for (j=0;j<col1;++j)
{
matx3[i][j]=matx1[i][j]+matx2[i][j];
cout<<matx3[i][j]<<" ";
}
cout<<endl;
}
}
else
{
cout<<"the
matrix is not computable for addition";
}
break;
case 2:
if(row1==row2&&col1==col2)
{
cout<<"\ndifference of the two
matrix";
for (i=0;i<row1;++i)
{
for
(j=0;j<col1;++j)
{
matx3[i][j]=matx1[i][j]-matx2[i][j];
cout<<matx3[i][j]<<" ";
}
cout<<endl;
}
}
else
{
cout<<"the
matrix is not computable for subtraction";
}
break ;
case 3:
if(col1==row2)
{
cout<<"\n product of two matrix
is"<<endl;
for (i=0;i<row1;++i)
{
for
(j=0;j<col2;++j)
{
matx3[i][j]=0;
for (k=0;k<col1;++k)
{
matx3[i][j]=matx3[i][j]+matx1[i][k]*matx2[k][j];
}
cout<<matx3[i][j]<<" ";
}
cout<<endl;
}
}
else
{
cout<<"the martix is not
compatible for multiplication"<<endl;
}
break ;
case 4:
cout<<"\n enter the matrix
number whose transvers you need";
cin>>opt1;
if (opt1==1)
{
cout<<"\n
transvers of the first matrix is ";
for
(i=0;i<row1;++i)
{
for (j=0;j<col1;++j)
{
matx3[i][j]=matx1[j][i];
cout<<matx3[i][j]<<" ";
}
cout<<endl;
}
}
if (opt1==2)
{
cout<<"\n
transvers of the second matrix is";
for
(i=0;i<row2;++i)
{
for (j=0;j<col2;++j)
{
matx3[i][j]=matx2[j][i];
cout<<matx3[i][j]<<" ";
}
}
}
break;
case 5:
cout<<"\n enter the matrix
number whose row sum or column sum you need";
cin>>opt1;
cout<<"\n Enter r/c to find the
sum of row or column ";
cin>>opt2;
cout<<"\n Enter the row or column
number whose sum you need";
cin>>num;
if (opt1==1)
{
if
(opt2=='r')
{
sum_row_col(matx1, row1, num, opt2);
}
if
(opt2=='c')
{
sum_row_col(matx1, col1, num, opt2);
}
}
if (opt2==2)
{
if
(opt2=='r')
{
sum_row_col(matx2, row2, num, opt2);
}
if
(opt2=='c')
{
sum_row_col(matx2, col2, num, opt2);
}
}
break;
case 6:
cout<<"\n Enter 1/2 to find the
sum of element above and below the diagonal of matrix 1/2";
cin>>opt1;
if (opt1==1)
{
cout<<"\n
For first matrix";
sum_dia(matx1,
row1);
}
else if(opt1==2)
{
cout<<"\n
For second matrix";
sum_dia(matx2,
row2);
}
else
cout<<"please
enter 1 or 2 as per your use";
break;
case 7:
exit(0);
break;
}
cout<<"\n\n
Enter (y/n) if you want to continue the program or not :";
cin>>opt3;
}while (opt3=='y');
return 0;
}
int sum_dia(int a[10][10], int row)
{
int i, j, asum=0,
bsum=0;
for (i=0;i<row;++i)
{
for
(j=0;j<row;++j)
{
if ((i+j)==(row-2))
asum=asum+a[i][j];
if ((i+j)==row)
bsum=bsum+a[i][j];
}
}
cout<<"\n Sum
of element above the diagonal is "<<asum;
cout<<"\n Sum
of element below the diagonal is "<<bsum;
return 0;
}
int sum_row_col(int a[10][10], int row, int num, char opt2)
{
int i, j, rsum=0,
csum=0;
num=num-1;
if (opt2=='r')
{
for
(i=0;i<row;++i)
{
rsum=rsum+a[num][i];
}
cout<<"\n sum
of element in the "<<num+1<<" row is :"<<rsum;
}
if (opt2=='c')
{
for
(j=0;j<row;++j)
{
csum=csum+a[j][num];
}
cout<<"\n sum
of element in the "<<num+1<<" column is
:"<<csum;
}
return 0;
}