0% found this document useful (0 votes)
18 views

16 CPP

The document describes a C++ program that defines a MATRIX class to perform operations on a 4x4 integer matrix. The class contains methods to accept user input for the matrix, display the matrix, and display the upper/lower triangular elements about the major/minor diagonals, the diagonal elements, and the transpose of the matrix. The main function uses a menu system to call the various class methods and operate on a sample 4x4 matrix input by the user.

Uploaded by

Anudeep Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

16 CPP

The document describes a C++ program that defines a MATRIX class to perform operations on a 4x4 integer matrix. The class contains methods to accept user input for the matrix, display the matrix, and display the upper/lower triangular elements about the major/minor diagonals, the diagonal elements, and the transpose of the matrix. The main function uses a menu system to call the various class methods and operate on a sample 4x4 matrix input by the user.

Uploaded by

Anudeep Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM- 16

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

1.Upper/Lower triangular elements about the major diagonal


2.Upper/Lower triangular elements about the minor diagonal
3.Major/Minor diagonals
4.Transpose
5.Exit

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

You might also like