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

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

Problem Description

The program takes an array and checks if it is a diagonal matrix. A diagonal matrix is a square matrix which has non zero elements only in the main diagonal.

Problem Solution

1. The program takes the number of rows and columns of the matrix.
2. If they are not equal, it cannot be a diagonal matrix. The program is exited.
3. Else, the matrix is checked if only the diagonal elements are non 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 Diagonal Matrix. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int i, j, m, n, flag = 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.         for (j = 0; j < n; j++)	
  16.             cin >> A[i][j];
  17.     for (i = 0; i < m; i++)
  18.         for (j = 0; j < n; j++)
  19.             if (i == j)
  20.             {
  21.                 if (A[i][j] == 0)
  22.                     flag = 1;
  23.             }
  24.             else
  25.             {
  26.                 if (A[i][j] != 0)
  27.                     flag = 1;
  28.             }
  29.     if (flag == 1)
  30.         cout << "The given array is not a diagonal matrix.";
  31.     else
  32.     {
  33.         cout << "The entered array is a diagonal matrix.\n ";	
  34.         for (i = 0; i < m; i++)
  35.         {
  36.             for (j = 0; j < n; j++)	
  37.                 cout << A[i][j] << " ";
  38.             cout << "\n ";
  39.         }
  40.     }
  41.     return 0;
  42. }
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 diagonal matrix. The program is exited.
3. Else, the elements are asked to enter and stored in ‘A’.
4. The diagonal elements of the matrix are checked. If they are 0, then a temporary variable ‘flag’ is assigned 1.
5. If the non-diagonal elements are not 0 also, flag is assigned 1.
6. If flag is equal to 1, the entered array is not a diagonal matrix, else it is a diagonal matrix.
7. The result is then printed.

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

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

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

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.