C++ Program to Check if a Matrix is a Triangular Matrix

This is a C++ Program to Find if an Array is a Triangular Matrix.

Problem Description

The program takes a matrix and checks if it is a triangular matrix. A triangular matrix is a square matrix which has all elements above or below the main diagonal as 0.

Problem Solution

1. The program takes the number of rows and columns of the matrix.
2. If they are not equal, it is not a square matrix. The program is exited.
3. Else, the matrix is checked if elements above or below teh main diagonal are zero.
4. The result is printed.
5. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if an Array is a Triangular Matrix. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int i, j, m, n, ut = 0, lt = 0, A[10][10];
  6.     cout << "Enter number of rows and columns : ";
  7.     cin >> m >> n;
  8.     if (m != n)
  9.     {
  10.         cout << "Matrix is not a square matrix!";
  11.         exit(0);
  12.     }
  13.     cout << "Enter elements of matrix : ";
  14.     for (i = 0; i < m; i++)
  15.     {
  16.         for (j = 0; j < n; j++)	
  17.         {
  18.             cin >> A[i][j];
  19.             if( j > i && A[i][j] != 0)
  20.                 ut = 1;
  21.             if( j < i && A[i][j] != 0)
  22.                 lt = 1;
  23.         }
  24.     }
  25.     if( ut == 0 || lt == 0)
  26.         cout << "The given matrix is a triangular matrix.\n ";
  27.     else
  28.         cout << "The given matrix is not a triangular matrix.\n ";
  29.     for (i = 0; i < m; i++)
  30.     {
  31.         for (j = 0; j < n; j++)	
  32.             cout << A[i][j] << " ";
  33.         cout << "\n ";
  34.     }
  35.     return 0;
  36. }
Program Explanation

1. The user is asked to enter the number of rows and columns.
2. If they are not equal, the array is not a square matrix, hence it cannot be a triangular matrix. The program is exited.
3. Else, the elements are asked to enter and stored in ‘A’. The variables ‘ut’ and ‘lt’ are initialized as 0.
4. The elements below and above the main diagonal in the matrix are checked using ut and lt.
5. If elements above the diagonal are not 1, then ut is equal to 1, else if elements below diagonal are not 0 then lt is 1.
6. If either ut or lt is equal to 0, then the matrix is triangular. It may be upper triangular or lower triangular matrix.
7. Else the entered array is not a triangular matrix.
8. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns : 3 3
Enter elements of matrix : 1 2 3 0 4 5 0 0 6
The given matrix is a triangular matrix.
 1 2 3
 0 4 5
 0 0 6
 
Case 2 :
Enter number of rows and columns : 3 1
Matrix is not a square matrix!
 
Case 3 :
Enter number of rows and columns : 2 2
Enter elements of matrix : 1 2 3 4
The given matrix is not a triangular matrix.
 1 2
 3 4

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.