16 CPP
16 CPP
OBJECTIVE
Create a menu driven C++ program which defines a class MATRIX to perform the
operations on a 4x4 integer matrixa)
b)
c)
d)
Display
Display
Display
Display
the upper and lower triangular matrix (about the major diagonal).
the upper and lower triangular matrix (about the minor diagonal).
both the diagonal elements.
the transpose of the matrix.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
class MATRIX
{
int a[4][4],s;
public:
void accept()
{
cout<<"Enter 16 elements: \n";
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
cin>>a[i][j];
}
void display()
{
cout<<"The matrix is: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
}
}
void triangular_major()
{
cout<<"The upper triangular elements about the major diagonal
are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i<=j)
cout<<a[i][j]<<"\t";
else cout<<"\t";
cout<<endl;
}
cout<<"The lower triangular elements about the major diagonal
are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i>=j)
cout<<a[i][j]<<"\t";
else cout<<"\t";
cout<<endl;
}
}
void triangular_minor()
{
cout<<"The upper triangular elements about the minor diagonal
are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i+j<=3)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
cout<<endl;
}
cout<<"The lower triangular elements about the minor diagonal
are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i+j>=3)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
cout<<endl;
}
}
void diagonal()
{
cout<<"The major diagonal elements are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i==j)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
cout<<endl;
}
cout<<"The minor diagonal elements are: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i+j==3)
cout<<a[i][j]<<"\t";
else cout<<"\t";
cout<<endl;
}
}
void transpose()
{
cout<<"The transpose of the matrix is: \n";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<a[j][i]<<"\t";
cout<<endl;
}
}
};
int main ()
{
MATRIX ob;
int d;
ob.accept();
ob.display();
start:
cout<<"\n1.Upper/Lower triangular elements about the major
diagonal\n";
cout<<"2.Upper/Lower triangular elements about the minor
diagonal\n";
cout<<"3.Major/Minor diagonals\n";
cout<<"4.Transpose\n";
cout<<"5.Exit";
cout<<"\n\nEnter choice: ";
cin>>d;
switch(d)
{
case 1 :ob.triangular_major();
break;
case 2 :ob.triangular_minor();
break;
case 3 :ob.diagonal();
break;
case 4 :ob.transpose();
break;
case 5 : exit(0);
default:cout<<"Wrong Choice";
goto start;
getch();
return 0;
OUTPUT
Enter 16 elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The matrix is:
1
2
3
5
6
7
9
10
11
13
14
15
4
8
12
16
Enter choice: 1
The upper triangular elements about the major diagonal are:
2
6
3
7
11
4
8
12
16
The lower triangular elements about the major diagonal are:
1
5
6
9
10
11
13
14
15
16
Enter choice: 2
The upper triangular elements about the minor diagonal are:
1
2
3
4
5
6
7
9
10
13
The lower triangular elements about the minor diagonal are:
4
7
8
10
11
12
13
14
15
16
Enter choice: 3
The major diagonal elements are:
1
6
11
16
The minor diagonal elements are:
4
7
10
13
Enter choice: 4
The transpose of the matrix is:
1
5
9
13
2
6
10
14
3
7
11
15
4
8
12
16