#include<iostream.h>
#include<conio.h>
#include<process.h>
void rowsum(int a[10][10],int m,int n)
{int i,j,s;
for(i=0;i<m;i++)
{s=0;
for(j=0;j<n;j++)
s+=a[i][j];
cout<<"\nrow sum of "<<i<<"row ="<<s;
cout<<"\n";}
}
void columnsum(int a[10][10],int m,int n)
{int i,j,s;
for(j=0;j<n;j++)
{s=0;
for(i=0;i<m;i++)
s+=a[i][j];
cout<<"\ncolumn sum of"<<j<<"column ="<<s;
cout<<"\n";}
}
void readmatrix(int a[10][10],int m,int n)
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void dispmatrix(int a[10][10],int m,int n)
{ int i,j;
for(i=0;i<m;i++)
{cout<<"\n";
for(j=0;j<n;j++)
{cout<<a[i][j];
cout<<"\t";
}
}
}
void summatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
void diffmatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
int smd(int a[10][10],int n)
{int i,j,sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j)
sum+=a[i][j];
return(sum);
}
int sod(int a[10][10], int n)
{int i,j,sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i+j==n-1)
sum+=a[i][j];
return(sum);
}
int sut(int a[10][10],int n)
{int i,j,sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(j>i)
sum+=a[i][j];
return(sum);
}
int slt(int a[10][10],int n)
{int i,j,sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i>j)
sum+=a[i][j];
return(sum);
}
void sumposneg(int a[10][10],int m,int n)
{int i,j,sp=0,sn=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i][j]<0)
sn+=a[i][j];
else
sp+=a[i][j];
cout<<"\nsum of positive no ="<<sp;
cout<<"\nsum of negative no ="<<sn;
}
void transpose(int a[10][10],int b[10][10],int m,int n)
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[j][i]=a[i][j];
}
void main()
{clrscr();
int A[10][10],B[10][10],C[10][10],m,n,p,q;
int a[10][10],sum;
char choice,ch1;
do{
cout<<"this is a menu driven program to access..various functions on 2d arrays\n";
cout<<"enter a to find the rowsum of a 2d array\n";
cout<<"enter b to find the columnsum of a 2d array\n";
cout<<"enter c to find the sum of two matrixes\n";
cout<<"enter d to find the difference of two matrixes\n";
cout<<"enter e to find the sum of main diagonal elements of a matrix\n";
cout<<"enter f to find the sum of off diagonal elements of a matrix\n";
cout<<"enter g to find the sum of upper triangle elements of a matrix\n";
cout<<"enter h to find the sum of lower triangle elements ot a matrix\n";
cout<<"enter i to find the sum of positive and negative no's of a 2d array separately\n";
cout<<"enter j to find the transpose of a matrix\n";
cout<<"enter k to exit this program\n";
cin>>choice;
switch(choice)
{
case 'a':
cout<<"enter m and n\n";
cin>>m>>n;
cout<<"enter an array\n";
readmatrix(a,m,n);
dispmatrix(a,m,n);
rowsum(a,m,n);
break;
case 'b':cout<<"enter m and n\n";
cin>>m>>n;
cout<<"enter an array\n";
readmatrix(a,m,n);
dispmatrix(a,m,n);
columnsum(a,m,n);
break;
case 'c':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);
summatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"summatrix";
cout<<"\n";
dispmatrix(C,m,n);
}
break;
case 'd':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);
diffmatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"diffmatrix";
cout<<"\n";
dispmatrix(C,m,n);
}
break;
case 'e':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);
if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=smd(a,n);
cout<<"sum ="<<sum; }
break;
case 'f':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);
if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sod(a,n);
cout<<"sum ="<<sum; }
break;
case 'g':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);
if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sut(a,n);
cout<<"sum ="<<sum;}
break;
case 'h':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);
if(m!=n)
cout<<"sum not possible";
else{
dispmatrix(a,m,n);
sum=slt(a,n);
cout<<"sum ="<<sum;}
break;
case 'i':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter an array";
readmatrix(a,m,n);
dispmatrix(a,m,n);
sumposneg(a,m,n);
break;
case 'j':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter an array";
readmatrix(a,m,n);
dispmatrix(a,m,n);
transpose(a,B,m,n);
dispmatrix(B,n,m);
break;
case 'k': exit(0);
break;
default:cout<<"enter a valid choice\n";
}
cout<<"\nenter y to repeat n to exit";
cin>>ch1;
if(ch1=='n'||ch1=='N')
exit(0);
}
while(ch1=='y'||ch1=='Y');
getch();
}