Write C++ menu driven program for following function: 1)To accept matrix 2)Display matrix 3)Sum of rows in matrix 4)Sum of columns in matrix 5)Transpose of matrix
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<conio>
#include<iostream>
class matrix
{
int a[20][20],i,j,r,c; // a=2d array , r= rows , c=columns
public:
void accept()
{
cout<<"\nEnter number of rows:";
cin>>r;
cout<<endl;
cout<<"\nEnter number of columns:";
cin>>c;
cout<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\nEnter elements : ";
cin>>a[i][j];
}
}
}
void sum() // sum=row sum
{
int s=0,ss=0;
for(i=0;i<r;i++)
{
s=0;
for(j=0;j<c;j++)
{
s+=a[i][j];
}
cout<<"\nThe sum of "<<"row"<<": "<<s<<endl;
}
}
void csum() // csum=column sum
{
int ss=0;
for(i=0;i<r;i++)
{
ss=0;
for(j=0;j<c;j++)
{
ss+=a[j][i];
}
cout<<"\nThe sum of "<<"column"<<": "<<ss<<endl;
}
}
void transpose(void);
void display()
{
cout<<"\nThe matrix is:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
};
int main()
{
int ch;
do
{
cout<<"******************************* Menu *********************************";
cout<<"\n1.Create Matrix\n2.Display Matrix\n3.Sum of rows\n4.Sum of columns\n5.Transpose of matrix\n6.Exit\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:matrix obj;
obj.accept();break;
case 2:obj.display();break;
case 3:obj.sum();break;
case 4:obj.csum();break;
case 5:obj.transpose();break;
}
}while(ch!=6);
return(0);
}
void matrix::transpose(void)
{
cout<<"\nTranspose of matrix:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
}
0 Comments