//Downloaded From theonlinetutorials.com
//Matrix Manipulation
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void matrixdisp(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
cout<<endl;
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"";
}
}
}
void matrixtrans(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
cout<<endl;
for(j=0;j<n;j++)
{
cout<<a[j][i];
}}
}
void rowsum(int a[10][10],int m,int n)
{
int i,j,s=0,k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
s+=a[i][j];
}
cout<<"\nSum of"<<i+1<<"th row:"<<s;
s=0;
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
k+=a[i][j];
}
cout<<"\nSum of"<<j+1<<"th column:"<<k;
k=0;
}
}
void trngl(int a[10][10],int m,int n)
{
int i,j,p,q;
for(i=0;i<m;i++)
{
cout<<endl;
for(j=0;j<n;j++)
{
if((i+j)<m)
cout<<a[i][j];
else
cout<<"-";
}}
cout<<endl;
for(p=0;p<m;p++)
{
cout<<endl;
for(q=0;q<n;q++)
{
if(p<=q)
cout<<a[p][q];
else
cout<<"";
}}}
void ltrngl(int a[10][10],int m,int n)
{
int r,s,x,y;
for(r=0;r<m;r++)
{
cout<<endl;
for(s=0;s<n;s++)
{
if(s<=r)
cout<<a[r][s];
else
cout<<" ";
}}
cout<<endl;
for(x=0;x<m;x++)
{
cout<<endl;
for(y=0;y<n;y++)
{
if((x+y)<=n+1)
cout<<a[x][y];
else
cout<<" ";
}}}
void dsum(int a[10][10],int m,int n)
{
int i,j,s=0,k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
s+=a[i][j];
}
}
cout<<"Sum of left diagonal:"<<s;
for(int x=0;x<m;x++)
{
for(int y=0;y<n;y++)
{
if((x+y)==(m-1))
k+=a[x][y];
}
}
cout<<"Sum of right diagonal:"<<k;
}
void main()
{
clrscr();
int i,j,m,n;
int opt;
char chr;
int a[10][10];
cout<<"Enter the no of rows:";
cin>>m;
cout<<"Enter the no of columns:";
cin>>n;
cout<<"\nInput the elements of the matrix:";
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
do
{
clrscr();
cout<<"MENU\n";
cout<<"1.Display Matrix\n2.Display Transpose\n3.Display sum of rows and columns\n4.Display Upper Triangles\n5.Display Lower triangle\n6.Sum of the diagonals\n";
cout<<"Enter option:";
cin>>opt;
switch(opt)
{
case 1:cout<<"The matrix is:";
matrixdisp(a,m,n);
break;
case 2:cout<<"The transpose is:";
matrixtrans(a,m,n);
break;
case 3:cout<<"The sum of columns and rows:";
rowsum(a,m,n);
break;
case 4:if(m==n)
cout<<"Upper triangular matrices are:";
trngl(a,m,n);
break;
case 5:if(m==n)
cout<<"Lower triangular matrices are:";
ltrngl(a,m,n);
break;
case 6:if(m==n)
cout<<"Sum of the diagonals :";
dsum(a,m,n);
break;
default:cout<<"Wrong option!!"<<endl;
}
cout<<"\n\nDo you want to continue?(y/n):";
cin>>chr;
}
while((chr=='y')||(chr=='Y'));
getch();
}
Not Satisfied ? Just search & get the result
Related posts:
- Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
- Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.
- C program to accept a matrix of given order and interchange any two rows and columns in the original matrix
- C program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace.
- C program to accept a matrix of order M x N and find the sum of each row and each column of a matrix
